Obtaining the resource version using native SQL during startup (#11264)
Closes #11263
This commit is contained in:
parent
2cb5d8d972
commit
15b2f8e26d
1 changed files with 19 additions and 20 deletions
|
@ -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
|
||||
|
@ -126,12 +126,27 @@ public class QuarkusJpaConnectionProviderFactory extends AbstractJpaConnectionPr
|
|||
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 {
|
||||
|
@ -143,9 +158,7 @@ public class QuarkusJpaConnectionProviderFactory extends AbstractJpaConnectionPr
|
|||
} 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -442,25 +455,11 @@ public class QuarkusJpaConnectionProviderFactory extends AbstractJpaConnectionPr
|
|||
}
|
||||
}
|
||||
|
||||
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());
|
||||
|
|
Loading…
Reference in a new issue