Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.stream.Collectors;

import org.keycloak.cluster.ClusterProvider;
import org.keycloak.connections.jpa.JpaConnectionProvider;
import org.keycloak.connections.jpa.updater.liquibase.conn.LiquibaseConnectionProvider;
import org.keycloak.connections.jpa.updater.liquibase.custom.CustomCreateIndexChange;
import org.keycloak.models.KeycloakSession;
Expand All @@ -60,6 +61,8 @@
import liquibase.precondition.core.OrPrecondition;
import liquibase.precondition.core.TableExistsPrecondition;
import liquibase.sqlgenerator.SqlGeneratorFactory;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.jboss.logging.Logger;

/**
Expand All @@ -81,13 +84,16 @@ public class DatabaseIndexChecker implements Runnable {
private final Supplier<Connection> connectionSupplier;
private final KeycloakSessionFactory factory;
private final String dbSchema;
// The configured schema resolved to the name as stored in the database, which may differ in case
private final String resolvedSchema;
private final boolean autoCreate;
private final int migrationTimeoutSeconds;

public DatabaseIndexChecker(Supplier<Connection> connectionSupplier, KeycloakSessionFactory factory, String dbSchema, boolean autoCreate, int migrationTimeoutSeconds) {
this.connectionSupplier = Objects.requireNonNull(connectionSupplier);
this.factory = Objects.requireNonNull(factory);
this.dbSchema = dbSchema;
this.resolvedSchema = resolveSchema(factory, dbSchema);
this.autoCreate = autoCreate;
this.migrationTimeoutSeconds = migrationTimeoutSeconds;
}
Expand Down Expand Up @@ -284,8 +290,8 @@ static String addOnlineSyntax(String sql, boolean isPostgres, boolean isOracle,
private String qualifyPostgresIdentifier(String identifier) {
// PostgreSQL folds unquoted identifiers to lowercase
String quoted = "\"" + identifier.toLowerCase().replace("\"", "\"\"") + "\"";
if (dbSchema != null) {
return "\"" + dbSchema.replace("\"", "\"\"") + "\"." + quoted;
if (resolvedSchema != null) {
return "\"" + resolvedSchema.replace("\"", "\"\"") + "\"." + quoted;
}
return quoted;
}
Expand Down Expand Up @@ -357,11 +363,26 @@ private Map<String, IndexInfo> getExpectedIndexesFromLiquibase(Connection connec
return expectedIndexes;
}

// Ask Hibernate how it resolves the configured schema, so the lookup targets the schema Hibernate uses (including its case folding).
private static String resolveSchema(KeycloakSessionFactory factory, String dbSchema) {
if (dbSchema == null) {
return null;
}
IdentifierHelper identifierHelper;
try (var session = factory.create()) {
identifierHelper = session.getProvider(JpaConnectionProvider.class).getEntityManager()
.getEntityManagerFactory().unwrap(SessionFactoryImplementor.class)
.getJdbcServices().getJdbcEnvironment().getIdentifierHelper();
}
var schema = identifierHelper.toMetaDataSchemaName(identifierHelper.toIdentifier(dbSchema));
return schema != null ? schema : dbSchema;
Comment thread
ahus1 marked this conversation as resolved.
Resolved
}

private Set<String> getExistingIndexesFromDatabase(DatabaseMetaData metaData, Collection<String> tables) throws SQLException {
var existingIndexes = new HashSet<String>();

for (var table : tables) {
try (var rs = metaData.getIndexInfo(null, dbSchema, table, false, true)) {
try (var rs = metaData.getIndexInfo(null, resolvedSchema, table, false, true)) {
while (rs.next()) {
var indexName = rs.getString("INDEX_NAME");
if (indexName != null) {
Expand All @@ -380,12 +401,12 @@ private Set<String> getInvalidIndexNames(Connection connection, DatabaseMetaData
return Set.of();
}
var invalidIndexes = new HashSet<String>();
String sql = dbSchema != null
String sql = resolvedSchema != null
? "SELECT UPPER(c.relname) FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = ? AND NOT i.indisvalid"
: "SELECT UPPER(c.relname) FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = current_schema AND NOT i.indisvalid";
try (var ps = connection.prepareStatement(sql)) {
if (dbSchema != null) {
ps.setString(1, dbSchema);
if (resolvedSchema != null) {
ps.setString(1, resolvedSchema);
}
try (var rs = ps.executeQuery()) {
while (rs.next()) {
Expand Down
Loading