Search in sources :

Example 1 with MigrationModel

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

the class QuarkusJpaConnectionProviderFactory method postInit.

@Override
public void postInit(KeycloakSessionFactory factory) {
    this.factory = factory;
    Instance<EntityManagerFactory> instance = CDI.current().select(EntityManagerFactory.class);
    if (!instance.isResolvable()) {
        throw new RuntimeException("Failed to resolve " + EntityManagerFactory.class + " from Quarkus runtime");
    }
    emf = instance.get();
    KeycloakSession session = factory.create();
    boolean schemaChanged;
    try (Connection connection = getConnection()) {
        createOperationalInfo(connection);
        addSpecificNamedQueries(session, connection);
        schemaChanged = createOrUpdateSchema(getSchema(), connection, session);
    } catch (SQLException cause) {
        throw new RuntimeException("Failed to update database.", cause);
    } finally {
        session.close();
    }
    if (schemaChanged || Environment.isImportExportMode()) {
        runJobInTransaction(factory, this::initSchema);
    } 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();
    }
}
Also used : MigrationModel(org.keycloak.migration.MigrationModel) SQLException(java.sql.SQLException) EntityManagerFactory(javax.persistence.EntityManagerFactory) Connection(java.sql.Connection)

Example 2 with MigrationModel

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

the class MigrationModelTest method test.

@Test
public void test() {
    inComittedTransaction(1, (session, i) -> {
        String currentVersion = Version.VERSION_KEYCLOAK.replaceAll("^(\\d+(?:\\.\\d+){0,2}).*$", "$1");
        JpaConnectionProvider p = session.getProvider(JpaConnectionProvider.class);
        EntityManager em = p.getEntityManager();
        List<MigrationModelEntity> l = em.createQuery("select m from MigrationModelEntity m ORDER BY m.updatedTime DESC", MigrationModelEntity.class).getResultList();
        Assert.assertEquals(1, l.size());
        Assert.assertTrue(l.get(0).getId().matches("[\\da-z]{5}"));
        Assert.assertEquals(currentVersion, l.get(0).getVersion());
        MigrationModel m = session.getProvider(DeploymentStateProvider.class).getMigrationModel();
        Assert.assertEquals(currentVersion, m.getStoredVersion());
        Assert.assertEquals(m.getResourcesTag(), l.get(0).getId());
        Time.setOffset(-60000);
        session.getProvider(DeploymentStateProvider.class).getMigrationModel().setStoredVersion("6.0.0");
        em.flush();
        Time.setOffset(0);
        l = em.createQuery("select m from MigrationModelEntity m ORDER BY m.updatedTime DESC", MigrationModelEntity.class).getResultList();
        Assert.assertEquals(2, l.size());
        Logger.getLogger(MigrationModelTest.class).info("MigrationModelEntity entries: ");
        Logger.getLogger(MigrationModelTest.class).info("--id: " + l.get(0).getId() + "; " + l.get(0).getVersion() + "; " + l.get(0).getUpdateTime());
        Logger.getLogger(MigrationModelTest.class).info("--id: " + l.get(1).getId() + "; " + l.get(1).getVersion() + "; " + l.get(1).getUpdateTime());
        Assert.assertTrue(l.get(0).getId().matches("[\\da-z]{5}"));
        Assert.assertEquals(currentVersion, l.get(0).getVersion());
        Assert.assertTrue(l.get(1).getId().matches("[\\da-z]{5}"));
        Assert.assertEquals("6.0.0", l.get(1).getVersion());
        m = session.getProvider(DeploymentStateProvider.class).getMigrationModel();
        Assert.assertEquals(l.get(0).getId(), m.getResourcesTag());
        Assert.assertEquals(currentVersion, m.getStoredVersion());
        em.remove(l.get(1));
        return null;
    });
}
Also used : EntityManager(javax.persistence.EntityManager) MigrationModel(org.keycloak.migration.MigrationModel) MigrationModelEntity(org.keycloak.models.jpa.entities.MigrationModelEntity) JpaConnectionProvider(org.keycloak.connections.jpa.JpaConnectionProvider) DeploymentStateProvider(org.keycloak.models.DeploymentStateProvider) Test(org.junit.Test)

Aggregations

MigrationModel (org.keycloak.migration.MigrationModel)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 EntityManager (javax.persistence.EntityManager)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 Test (org.junit.Test)1 JpaConnectionProvider (org.keycloak.connections.jpa.JpaConnectionProvider)1 DeploymentStateProvider (org.keycloak.models.DeploymentStateProvider)1 MigrationModelEntity (org.keycloak.models.jpa.entities.MigrationModelEntity)1