Increase column size for keys that refer to entities that can be stored in different storages (foreign keys)
Closes #11329
This commit is contained in:
parent
45b203d74c
commit
f48d468641
8 changed files with 65 additions and 10 deletions
|
@ -22,6 +22,8 @@ import java.util.List;
|
|||
|
||||
import liquibase.database.Database;
|
||||
import liquibase.database.core.PostgresDatabase;
|
||||
import liquibase.datatype.DataTypeFactory;
|
||||
import liquibase.datatype.DatabaseDataType;
|
||||
import liquibase.sql.Sql;
|
||||
import liquibase.sql.UnparsedSql;
|
||||
import liquibase.sqlgenerator.SqlGenerator;
|
||||
|
@ -89,8 +91,9 @@ public class GeneratedColumnSqlGenerator extends AddColumnGenerator {
|
|||
protected void handleGeneratedColumn(final GeneratedColumnStatement statement, final Database database, final StringBuilder sqlBuilder) {
|
||||
if (database instanceof PostgresDatabase) {
|
||||
// assemble the GENERATED ALWAYS AS section of the query using the json property selection function.
|
||||
DatabaseDataType columnType = DataTypeFactory.getInstance().fromDescription(statement.getColumnType(), database).toDatabaseDataType(database);
|
||||
sqlBuilder.append(" GENERATED ALWAYS AS ((").append(statement.getJsonColumn()).append("->>'").append(statement.getJsonProperty())
|
||||
.append("')::").append(statement.getColumnType()).append(") stored");
|
||||
.append("')::").append(columnType).append(") stored");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright 2022 Red Hat, Inc. and/or its affiliates
|
||||
* and other contributors as indicated by the @author tags.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.keycloak.models.map.storage.jpa.liquibase.extension;
|
||||
|
||||
import liquibase.change.core.LoadDataChange;
|
||||
import liquibase.database.Database;
|
||||
import liquibase.datatype.DataTypeInfo;
|
||||
import liquibase.datatype.DatabaseDataType;
|
||||
import liquibase.datatype.LiquibaseDataType;
|
||||
import liquibase.datatype.core.VarcharType;
|
||||
|
||||
/**
|
||||
* A {@link LiquibaseDataType} used in columns that reference an entity that can be external to the JPA storage. In other
|
||||
* words, they reference objects that might be stored in different storages, with different id formats than those used
|
||||
* by the JPA storage.
|
||||
* <p/>
|
||||
* Its usage is encouraged to bring consistency to all columns that might reference external entities.
|
||||
*
|
||||
* @author <a href="mailto:sguilhen@redhat.com">Stefan Guilhen</a>
|
||||
*/
|
||||
@DataTypeInfo(name="kc_key", minParameters = 0, maxParameters = 0, priority = LiquibaseDataType.PRIORITY_DEFAULT)
|
||||
public class KeycloakKeyDataType extends LiquibaseDataType {
|
||||
|
||||
@Override
|
||||
public DatabaseDataType toDatabaseDataType(Database database) {
|
||||
// convert the type into a varchar of size 4000.
|
||||
VarcharType varcharType = new VarcharType();
|
||||
varcharType.addParameter("4000");
|
||||
return varcharType.toDatabaseDataType(database);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoadDataChange.LOAD_DATA_TYPE getLoadTypeName() {
|
||||
return LoadDataChange.LOAD_DATA_TYPE.STRING;
|
||||
}
|
||||
}
|
|
@ -36,7 +36,7 @@ limitations under the License.
|
|||
</createTable>
|
||||
<ext:addGeneratedColumn tableName="kc_auth_root_session">
|
||||
<ext:column name="entityversion" type="INTEGER" jsonColumn="metadata" jsonProperty="entityVersion"/>
|
||||
<ext:column name="realmid" type="VARCHAR(36)" jsonColumn="metadata" jsonProperty="fRealmId"/>
|
||||
<ext:column name="realmid" type="KC_KEY" jsonColumn="metadata" jsonProperty="fRealmId"/>
|
||||
<ext:column name="timestamp" type="BIGINT" jsonColumn="metadata" jsonProperty="fTimestamp"/>
|
||||
<ext:column name="expiration" type="BIGINT" jsonColumn="metadata" jsonProperty="fExpiration"/>
|
||||
</ext:addGeneratedColumn>
|
||||
|
|
|
@ -36,7 +36,7 @@ limitations under the License.
|
|||
</createTable>
|
||||
<ext:addGeneratedColumn tableName="kc_client_scope">
|
||||
<ext:column name="entityversion" type="INTEGER" jsonColumn="metadata" jsonProperty="entityVersion"/>
|
||||
<ext:column name="realmid" type="VARCHAR(36)" jsonColumn="metadata" jsonProperty="fRealmId"/>
|
||||
<ext:column name="realmid" type="KC_KEY" jsonColumn="metadata" jsonProperty="fRealmId"/>
|
||||
<ext:column name="name" type="VARCHAR(255)" jsonColumn="metadata" jsonProperty="fName"/>
|
||||
</ext:addGeneratedColumn>
|
||||
<createIndex tableName="kc_client_scope" indexName="client_scope_entityVersion">
|
||||
|
|
|
@ -36,9 +36,9 @@ limitations under the License.
|
|||
</createTable>
|
||||
<ext:addGeneratedColumn tableName="kc_client">
|
||||
<ext:column name="entityversion" type="INTEGER" jsonColumn="metadata" jsonProperty="entityVersion"/>
|
||||
<ext:column name="realmid" type="VARCHAR(36)" jsonColumn="metadata" jsonProperty="fRealmId"/>
|
||||
<ext:column name="realmid" type="KC_KEY" jsonColumn="metadata" jsonProperty="fRealmId"/>
|
||||
<ext:column name="clientid" type="VARCHAR(255)" jsonColumn="metadata" jsonProperty="fClientId"/>
|
||||
<ext:column name="protocol" type="VARCHAR(36)" jsonColumn="metadata" jsonProperty="fProtocol"/>
|
||||
<ext:column name="protocol" type="VARCHAR(255)" jsonColumn="metadata" jsonProperty="fProtocol"/>
|
||||
<ext:column name="enabled" type="BOOLEAN" jsonColumn="metadata" jsonProperty="fEnabled"/>
|
||||
</ext:addGeneratedColumn>
|
||||
<createIndex tableName="kc_client" indexName="client_entityVersion">
|
||||
|
|
|
@ -36,9 +36,9 @@ limitations under the License.
|
|||
</createTable>
|
||||
<ext:addGeneratedColumn tableName="kc_group">
|
||||
<ext:column name="entityversion" type="INTEGER" jsonColumn="metadata" jsonProperty="entityVersion"/>
|
||||
<ext:column name="realmid" type="VARCHAR(36)" jsonColumn="metadata" jsonProperty="fRealmId"/>
|
||||
<ext:column name="realmid" type="KC_KEY" jsonColumn="metadata" jsonProperty="fRealmId"/>
|
||||
<ext:column name="parentid" type="KC_KEY" jsonColumn="metadata" jsonProperty="fParentId"/>
|
||||
<ext:column name="name" type="VARCHAR(255)" jsonColumn="metadata" jsonProperty="fName"/>
|
||||
<ext:column name="parentid" type="VARCHAR(36)" jsonColumn="metadata" jsonProperty="fParentId"/>
|
||||
</ext:addGeneratedColumn>
|
||||
<createIndex tableName="kc_group" indexName="group_entityVersion">
|
||||
<column name="entityversion"/>
|
||||
|
|
|
@ -36,9 +36,9 @@ limitations under the License.
|
|||
</createTable>
|
||||
<ext:addGeneratedColumn tableName="kc_role">
|
||||
<ext:column name="entityversion" type="INTEGER" jsonColumn="metadata" jsonProperty="entityVersion"/>
|
||||
<ext:column name="realmid" type="VARCHAR(36)" jsonColumn="metadata" jsonProperty="fRealmId"/>
|
||||
<ext:column name="realmid" type="KC_KEY" jsonColumn="metadata" jsonProperty="fRealmId"/>
|
||||
<ext:column name="clientid" type="KC_KEY" jsonColumn="metadata" jsonProperty="fClientId"/>
|
||||
<ext:column name="name" type="VARCHAR(255)" jsonColumn="metadata" jsonProperty="fName"/>
|
||||
<ext:column name="clientid" type="VARCHAR(255)" jsonColumn="metadata" jsonProperty="fClientId"/>
|
||||
<ext:column name="description" type="VARCHAR(255)" jsonColumn="metadata" jsonProperty="fDescription"/>
|
||||
</ext:addGeneratedColumn>
|
||||
<createIndex tableName="kc_role" indexName="role_entityVersion">
|
||||
|
|
|
@ -15,4 +15,5 @@
|
|||
# limitations under the License.
|
||||
#
|
||||
|
||||
org.keycloak.models.map.storage.jpa.liquibase.extension.JsonDataType
|
||||
org.keycloak.models.map.storage.jpa.liquibase.extension.JsonDataType
|
||||
org.keycloak.models.map.storage.jpa.liquibase.extension.KeycloakKeyDataType
|
Loading…
Reference in a new issue