Skip to content

Commit

Permalink
Obtaining the resource version using native SQL during startup (keycl…
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroigor authored Apr 20, 2022
1 parent 2cb5d8d commit 15b2f8e
Showing 1 changed file with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class QuarkusJpaConnectionProviderFactory extends AbstractJpaConnectionPr

public static final String QUERY_PROPERTY_PREFIX = "kc.query.";
private static final Logger logger = Logger.getLogger(QuarkusJpaConnectionProviderFactory.class);
private static final String SQL_GET_LATEST_VERSION = "SELECT VERSION FROM %sMIGRATION_MODEL ORDER BY UPDATE_TIME DESC";
private static final String SQL_GET_LATEST_VERSION = "SELECT ID, VERSION FROM %sMIGRATION_MODEL ORDER BY UPDATE_TIME DESC";

enum MigrationStrategy {
UPDATE, VALIDATE, MANUAL
Expand Down Expand Up @@ -126,12 +126,27 @@ public void postInit(KeycloakSessionFactory factory) {
this.factory = factory;

KeycloakSession session = factory.create();
String id = null;
String version = null;
String schema = getSchema();
boolean schemaChanged;

try (Connection connection = getConnection()) {
try {
try (Statement statement = connection.createStatement()) {
try (ResultSet rs = statement.executeQuery(String.format(SQL_GET_LATEST_VERSION, getSchema(schema)))) {
if (rs.next()) {
id = rs.getString(1);
version = rs.getString(2);
}
}
}
} catch (SQLException ignore) {
// migration model probably does not exist so we assume the database is empty
}
createOperationalInfo(connection);
addSpecificNamedQueries(session);
schemaChanged = createOrUpdateSchema(getSchema(), connection, session);
schemaChanged = createOrUpdateSchema(schema, version, connection, session);
} catch (SQLException cause) {
throw new RuntimeException("Failed to update database.", cause);
} finally {
Expand All @@ -143,9 +158,7 @@ public void postInit(KeycloakSessionFactory factory) {
} else if (System.getProperty("keycloak.import") != null) {
importRealms();
} else {
//KEYCLOAK-19521 - We should think about a solution which doesn't involve another db lookup in the future.
MigrationModel model = session.getProvider(DeploymentStateProvider.class).getMigrationModel();
Version.RESOURCES_VERSION = model.getResourcesTag();
Version.RESOURCES_VERSION = id;
}
}

Expand Down Expand Up @@ -442,25 +455,11 @@ private void createOperationalInfo(Connection connection) {
}
}

private boolean createOrUpdateSchema(String schema, Connection connection, KeycloakSession session) {
private boolean createOrUpdateSchema(String schema, String version, Connection connection, KeycloakSession session) {
MigrationStrategy strategy = getMigrationStrategy();
boolean initializeEmpty = config.getBoolean("initializeEmpty", true);
File databaseUpdateFile = getDatabaseUpdateFile();

String version = null;

try {
try (Statement statement = connection.createStatement()) {
try (ResultSet rs = statement.executeQuery(String.format(SQL_GET_LATEST_VERSION, getSchema(schema)))) {
if (rs.next()) {
version = rs.getString(1);
}
}
}
} catch (SQLException ignore) {
// migration model probably does not exist so we assume the database is empty
}

JpaUpdaterProvider updater = session.getProvider(JpaUpdaterProvider.class);

boolean requiresMigration = version == null || !version.equals(new ModelVersion(Version.VERSION_KEYCLOAK).toString());
Expand Down

0 comments on commit 15b2f8e

Please sign in to comment.