Fix DB Migration Script to 13.0.0

Closes #9138
This commit is contained in:
vramik 2021-12-14 17:23:07 +01:00 committed by Hynek Mlnařík
parent c6312e3308
commit b4d720d615

View file

@ -48,26 +48,26 @@ public class JpaUpdate13_0_0_MigrateDefaultRoles extends CustomKeycloakTask {
// create new default role // create new default role
new InsertStatement(null, null, database.correctObjectName("KEYCLOAK_ROLE", Table.class)) new InsertStatement(null, null, database.correctObjectName("KEYCLOAK_ROLE", Table.class))
.addColumnValue("ID", id) .addColumnValue("ID", id)
.addColumnValue("CLIENT_REALM_CONSTRAINT", entry.getValue()) .addColumnValue("CLIENT_REALM_CONSTRAINT", entry.getKey())
.addColumnValue("CLIENT_ROLE", Boolean.FALSE) .addColumnValue("CLIENT_ROLE", Boolean.FALSE)
.addColumnValue("DESCRIPTION", "${role_" + roleName + "}") .addColumnValue("DESCRIPTION", "${role_" + roleName + "}")
.addColumnValue("NAME", roleName) .addColumnValue("NAME", roleName)
.addColumnValue("REALM_ID", entry.getValue()) .addColumnValue("REALM_ID", entry.getKey())
.addColumnValue("REALM", entry.getValue()) .addColumnValue("REALM", entry.getKey())
); );
statements.add( statements.add(
// assign the role to the realm // assign the role to the realm
new UpdateStatement(null, null, database.correctObjectName("REALM", Table.class)) new UpdateStatement(null, null, database.correctObjectName("REALM", Table.class))
.addNewColumnValue("DEFAULT_ROLE", id) .addNewColumnValue("DEFAULT_ROLE", id)
.setWhereClause("REALM.ID=?") .setWhereClause("REALM.ID=?")
.addWhereParameter(entry.getValue()) .addWhereParameter(entry.getKey())
); );
statements.add( statements.add(
// copy data from REALM_DEFAULT_ROLES to COMPOSITE_ROLE // copy data from REALM_DEFAULT_ROLES to COMPOSITE_ROLE
new RawSqlStatement("INSERT INTO " + compositeRoleTable + " (COMPOSITE, CHILD_ROLE) " + new RawSqlStatement("INSERT INTO " + compositeRoleTable + " (COMPOSITE, CHILD_ROLE) " +
"SELECT '" + id + "', ROLE_ID FROM " + getTableName("REALM_DEFAULT_ROLES") + "SELECT '" + id + "', ROLE_ID FROM " + getTableName("REALM_DEFAULT_ROLES") +
" WHERE REALM_ID = '" + database.escapeStringForDatabase(entry.getValue()) + "'") " WHERE REALM_ID = '" + database.escapeStringForDatabase(entry.getKey()) + "'")
); );
statements.add( statements.add(
// copy data from CLIENT_DEFAULT_ROLES to COMPOSITE_ROLE // copy data from CLIENT_DEFAULT_ROLES to COMPOSITE_ROLE
@ -75,7 +75,7 @@ public class JpaUpdate13_0_0_MigrateDefaultRoles extends CustomKeycloakTask {
"SELECT '" + id + "', " + clientDefaultRolesTable + ".ROLE_ID FROM " + "SELECT '" + id + "', " + clientDefaultRolesTable + ".ROLE_ID FROM " +
clientDefaultRolesTable + " INNER JOIN " + clientTable + " ON " + clientDefaultRolesTable + " INNER JOIN " + clientTable + " ON " +
clientTable + ".ID = " + clientDefaultRolesTable + ".CLIENT_ID AND " + clientTable + ".ID = " + clientDefaultRolesTable + ".CLIENT_ID AND " +
clientTable + ".REALM_ID = '" + database.escapeStringForDatabase(entry.getValue()) + "'") clientTable + ".REALM_ID = '" + database.escapeStringForDatabase(entry.getKey()) + "'")
); );
} }
} }