diff --git a/connections/jpa-liquibase/src/main/java/org/keycloak/connections/jpa/updater/liquibase/custom/AddRealmCodeSecret.java b/connections/jpa-liquibase/src/main/java/org/keycloak/connections/jpa/updater/liquibase/custom/AddRealmCodeSecret.java index 76a02a7e5b..48941d3981 100644 --- a/connections/jpa-liquibase/src/main/java/org/keycloak/connections/jpa/updater/liquibase/custom/AddRealmCodeSecret.java +++ b/connections/jpa-liquibase/src/main/java/org/keycloak/connections/jpa/updater/liquibase/custom/AddRealmCodeSecret.java @@ -7,8 +7,11 @@ import liquibase.exception.CustomChangeException; import liquibase.exception.SetupException; import liquibase.exception.ValidationErrors; import liquibase.resource.ResourceAccessor; +import liquibase.snapshot.SnapshotGeneratorFactory; import liquibase.statement.SqlStatement; import liquibase.statement.core.UpdateStatement; +import liquibase.structure.core.Schema; +import liquibase.structure.core.Table; import org.keycloak.models.utils.KeycloakModelUtils; import java.sql.Connection; @@ -29,25 +32,28 @@ public class AddRealmCodeSecret implements CustomSqlChange { sb.append("Generated codeSecret for realms: "); Connection connection = ((JdbcConnection) (database.getConnection())).getWrappedConnection(); - ResultSet resultSet = connection.createStatement().executeQuery("SELECT ID FROM REALM WHERE CODE_SECRET IS NULL"); - ArrayList statements = new ArrayList(); - while (resultSet.next()) { - String id = resultSet.getString(1); - UpdateStatement statement = new UpdateStatement(null, null, "REALM") - .addNewColumnValue("CODE_SECRET", KeycloakModelUtils.generateCodeSecret()) - .setWhereClause("ID='" + id + "'"); - statements.add(statement); + String correctedTableName = database.correctObjectName("REALM", Table.class); + if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(correctedTableName), database)) { + ResultSet resultSet = connection.createStatement().executeQuery("SELECT ID FROM REALM WHERE CODE_SECRET IS NULL"); + while (resultSet.next()) { + String id = resultSet.getString(1); - if (!resultSet.isFirst()) { - sb.append(", "); + UpdateStatement statement = new UpdateStatement(null, null, correctedTableName) + .addNewColumnValue("CODE_SECRET", KeycloakModelUtils.generateCodeSecret()) + .setWhereClause("ID='" + id + "'"); + statements.add(statement); + + if (!resultSet.isFirst()) { + sb.append(", "); + } + sb.append(id); } - sb.append(id); - } - if (!statements.isEmpty()) { - confirmationMessage = sb.toString(); + if (!statements.isEmpty()) { + confirmationMessage = sb.toString(); + } } return statements.toArray(new SqlStatement[statements.size()]);