KEYCLOAK-885 Support Liquibase updateSQL

This commit is contained in:
Stian Thorgersen 2014-12-29 14:40:00 +01:00
parent 9d63482759
commit 2eb2ec50e3

View file

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