Search in sources :

Example 1 with ModelVersion

use of org.keycloak.migration.ModelVersion 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 ModelVersion

use of org.keycloak.migration.ModelVersion in project keycloak by keycloak.

the class MigrationVersionTest method testRHSSOVersionToKeycloakVersionConversion.

@Test
public void testRHSSOVersionToKeycloakVersionConversion() {
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.0.0.GA"), is(equalTo(RHSSO_VERSION_7_0_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.0.1.GA"), is(equalTo(RHSSO_VERSION_7_0_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.0.2.GA"), is(equalTo(RHSSO_VERSION_7_0_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.1.0.GA"), is(equalTo(RHSSO_VERSION_7_1_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.1.1.GA"), is(equalTo(RHSSO_VERSION_7_1_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.1.2.GA"), is(equalTo(RHSSO_VERSION_7_1_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.2.0.GA"), is(equalTo(RHSSO_VERSION_7_2_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.2.1.GA"), is(equalTo(RHSSO_VERSION_7_2_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.2.2.GA"), is(equalTo(RHSSO_VERSION_7_2_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.3.0.GA"), is(equalTo(RHSSO_VERSION_7_3_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.3.1.GA"), is(equalTo(RHSSO_VERSION_7_3_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.3.2.GA"), is(equalTo(RHSSO_VERSION_7_3_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.3.10.GA"), is(equalTo(RHSSO_VERSION_7_3_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.4.0.GA"), is(equalTo(RHSSO_VERSION_7_4_KEYCLOAK_VERSION)));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.4.15.GA"), is(equalTo(RHSSO_VERSION_7_4_KEYCLOAK_VERSION)));
    // check the conversion doesn't change version for keycloak
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7.0.0"), is(nullValue()));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("8.0.0"), is(nullValue()));
    // check for CD releases
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("6"), is(equalTo(new ModelVersion("6.0.0"))));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("7"), is(equalTo(new ModelVersion("7.0.0"))));
    Assert.assertThat(convertRHSSOVersionToKeycloakVersion("10"), is(equalTo(new ModelVersion("10.0.0"))));
}
Also used : ModelVersion(org.keycloak.migration.ModelVersion) Test(org.junit.Test)

Example 3 with ModelVersion

use of org.keycloak.migration.ModelVersion in project keycloak by keycloak.

the class MigrationVersionTest method testVersion.

@Test
public void testVersion() {
    ModelVersion version_100Beta1 = new ModelVersion("1.0.0.Beta1-SNAPSHOT");
    Assert.assertEquals(1, version_100Beta1.getMajor());
    Assert.assertEquals(0, version_100Beta1.getMinor());
    Assert.assertEquals(0, version_100Beta1.getMicro());
    ModelVersion version_100CR1 = new ModelVersion("1.0.0.CR1");
    ModelVersion version_100 = new ModelVersion("1.0.0");
    ModelVersion version_110Beta1 = new ModelVersion("1.1.0.Beta1");
    ModelVersion version_110CR1 = new ModelVersion("1.1.0.CR1");
    ModelVersion version_110 = new ModelVersion("1.1.0");
    ModelVersion version_120CR1 = new ModelVersion("1.2.0.CR1");
    ModelVersion version_130Beta1 = new ModelVersion("1.3.0.Beta1");
    ModelVersion version_130 = new ModelVersion("1.3.0");
    ModelVersion version_140 = new ModelVersion("1.4.0");
    ModelVersion version_211CR1 = new ModelVersion("2.1.1.CR1");
    Assert.assertEquals(2, version_211CR1.getMajor());
    Assert.assertEquals(1, version_211CR1.getMinor());
    Assert.assertEquals(1, version_211CR1.getMicro());
    Assert.assertEquals("CR1", version_211CR1.getQualifier());
    ModelVersion version_211 = new ModelVersion("2.1.1");
    ModelVersion version50Snapshot = new ModelVersion("5.0.0-SNAPSHOT");
    Assert.assertEquals(5, version50Snapshot.getMajor());
    Assert.assertEquals(0, version50Snapshot.getMinor());
    Assert.assertEquals(0, version50Snapshot.getMicro());
    Assert.assertNull(version50Snapshot.getQualifier());
    Assert.assertFalse(version_100Beta1.lessThan(version_100Beta1));
    Assert.assertTrue(version_100Beta1.lessThan(version_100CR1));
    Assert.assertTrue(version_100Beta1.lessThan(version_100));
    Assert.assertTrue(version_100Beta1.lessThan(version_110Beta1));
    Assert.assertTrue(version_100Beta1.lessThan(version_110CR1));
    Assert.assertTrue(version_100Beta1.lessThan(version_110));
    Assert.assertFalse(version_110Beta1.lessThan(version_100CR1));
    Assert.assertFalse(version_130Beta1.lessThan(version_120CR1));
    Assert.assertTrue(version_130Beta1.lessThan(version_130));
    Assert.assertTrue(version_130Beta1.lessThan(version_140));
    Assert.assertFalse(version_211CR1.lessThan(version_140));
    Assert.assertTrue(version_140.lessThan(version_211CR1));
    Assert.assertFalse(version_211.lessThan(version_110CR1));
    Assert.assertTrue(version_211CR1.lessThan(version50Snapshot));
    ModelVersion versionPipeline = new ModelVersion("8.0.2-REL-20200130-143126");
    Assert.assertEquals(8, versionPipeline.getMajor());
    Assert.assertEquals(0, versionPipeline.getMinor());
    Assert.assertEquals(2, versionPipeline.getMicro());
    Assert.assertNull(versionPipeline.getQualifier());
    ModelVersion versionPipeline2 = new ModelVersion("9.1.2-SNAPSHOT-stage-20191125-003440");
    Assert.assertEquals(9, versionPipeline2.getMajor());
    Assert.assertEquals(1, versionPipeline2.getMinor());
    Assert.assertEquals(2, versionPipeline2.getMicro());
    Assert.assertNull(versionPipeline2.getQualifier());
    ModelVersion versionProduct = new ModelVersion("7.0.0.redhat-00002");
    Assert.assertEquals(7, versionProduct.getMajor());
    Assert.assertEquals(0, versionProduct.getMinor());
    Assert.assertEquals(0, versionProduct.getMicro());
    Assert.assertNull(versionProduct.getQualifier());
}
Also used : ModelVersion(org.keycloak.migration.ModelVersion) Test(org.junit.Test)

Aggregations

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