Search in sources :

Example 1 with ServerStartupError

use of org.keycloak.ServerStartupError in project keycloak by keycloak.

the class QuarkusJpaConnectionProviderFactory method createOrUpdateSchema.

private boolean createOrUpdateSchema(String schema, 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());
    session.setAttribute(VERIFY_AND_RUN_MASTER_CHANGELOG, requiresMigration);
    JpaUpdaterProvider.Status status = updater.validate(connection, schema);
    if (status == JpaUpdaterProvider.Status.VALID) {
        logger.debug("Database is up-to-date");
    } else if (status == JpaUpdaterProvider.Status.EMPTY) {
        if (initializeEmpty) {
            update(connection, schema, session, updater);
        } else {
            switch(strategy) {
                case UPDATE:
                    update(connection, schema, session, updater);
                    break;
                case MANUAL:
                    export(connection, schema, databaseUpdateFile, session, updater);
                    throw new ServerStartupError("Database not initialized, please initialize database with " + databaseUpdateFile.getAbsolutePath(), false);
                case VALIDATE:
                    throw new ServerStartupError("Database not initialized, please enable database initialization", false);
            }
        }
    } else {
        switch(strategy) {
            case UPDATE:
                update(connection, schema, session, updater);
                break;
            case MANUAL:
                export(connection, schema, databaseUpdateFile, session, updater);
                throw new ServerStartupError("Database not up-to-date, please migrate database with " + databaseUpdateFile.getAbsolutePath(), false);
            case VALIDATE:
                throw new ServerStartupError("Database not up-to-date, please enable database migration", false);
        }
    }
    return requiresMigration;
}
Also used : SQLException(java.sql.SQLException) ServerStartupError(org.keycloak.ServerStartupError) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) JpaUpdaterProvider(org.keycloak.connections.jpa.updater.JpaUpdaterProvider) ModelVersion(org.keycloak.migration.ModelVersion) File(java.io.File)

Example 2 with ServerStartupError

use of org.keycloak.ServerStartupError in project keycloak by keycloak.

the class DefaultJpaConnectionProviderFactory method migration.

void migration(MigrationStrategy strategy, boolean initializeEmpty, String schema, File databaseUpdateFile, Connection connection, KeycloakSession session) {
    JpaUpdaterProvider updater = session.getProvider(JpaUpdaterProvider.class, LiquibaseJpaUpdaterProviderFactory.PROVIDER_ID);
    JpaUpdaterProvider.Status status = updater.validate(connection, schema);
    if (status == JpaUpdaterProvider.Status.VALID) {
        logger.debug("Database is up-to-date");
    } else if (status == JpaUpdaterProvider.Status.EMPTY) {
        if (initializeEmpty) {
            update(connection, schema, session, updater);
        } else {
            switch(strategy) {
                case UPDATE:
                    update(connection, schema, session, updater);
                    break;
                case MANUAL:
                    export(connection, schema, databaseUpdateFile, session, updater);
                    throw new ServerStartupError("Database not initialized, please initialize database with " + databaseUpdateFile.getAbsolutePath(), false);
                case VALIDATE:
                    throw new ServerStartupError("Database not initialized, please enable database initialization", false);
            }
        }
    } else {
        switch(strategy) {
            case UPDATE:
                update(connection, schema, session, updater);
                break;
            case MANUAL:
                export(connection, schema, databaseUpdateFile, session, updater);
                throw new ServerStartupError("Database not up-to-date, please migrate database with " + databaseUpdateFile.getAbsolutePath(), false);
            case VALIDATE:
                throw new ServerStartupError("Database not up-to-date, please enable database migration", false);
        }
    }
}
Also used : ServerStartupError(org.keycloak.ServerStartupError) JpaUpdaterProvider(org.keycloak.connections.jpa.updater.JpaUpdaterProvider)

Aggregations

ServerStartupError (org.keycloak.ServerStartupError)2 JpaUpdaterProvider (org.keycloak.connections.jpa.updater.JpaUpdaterProvider)2 File (java.io.File)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ModelVersion (org.keycloak.migration.ModelVersion)1