Search in sources :

Example 1 with ExecutorService

use of liquibase.executor.ExecutorService in project keycloak by keycloak.

the class LiquibaseJpaUpdaterProvider method outputChangeLogTableCreationScript.

private void outputChangeLogTableCreationScript(Liquibase liquibase, final Writer exportWriter) throws DatabaseException {
    Database database = liquibase.getDatabase();
    ExecutorService executorService = Scope.getCurrentScope().getSingleton(ExecutorService.class);
    Executor oldTemplate = executorService.getExecutor(LiquibaseConstants.JDBC_EXECUTOR, database);
    LoggingExecutor loggingExecutor = new LoggingExecutor(oldTemplate, exportWriter, database);
    executorService.setExecutor(LiquibaseConstants.JDBC_EXECUTOR, database, loggingExecutor);
    loggingExecutor.comment("*********************************************************************");
    loggingExecutor.comment("* Keycloak database creation script - apply this script to empty DB *");
    loggingExecutor.comment("*********************************************************************" + StreamUtil.getLineSeparator());
    loggingExecutor.execute(new CreateDatabaseChangeLogTableStatement());
    // DatabaseChangeLogLockTable is created before this code is executed and recreated if it does not exist automatically
    // in org.keycloak.connections.jpa.updater.liquibase.lock.CustomLockService.init() called indirectly from
    // KeycloakApplication constructor (search for waitForLock() call). Hence it is not included in the creation script.
    loggingExecutor.comment("*********************************************************************" + StreamUtil.getLineSeparator());
    executorService.setExecutor(LiquibaseConstants.JDBC_EXECUTOR, database, oldTemplate);
}
Also used : CreateDatabaseChangeLogTableStatement(liquibase.statement.core.CreateDatabaseChangeLogTableStatement) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) LoggingExecutor(liquibase.executor.LoggingExecutor) Database(liquibase.database.Database) ExecutorService(liquibase.executor.ExecutorService)

Example 2 with ExecutorService

use of liquibase.executor.ExecutorService in project liquibase by liquibase.

the class StandardLockServiceTest method before.

@Before
public void before() throws Exception {
    Executor executor = Mockito.mock(Executor.class);
    Mockito.when(executor.queryForList(Mockito.any())).thenReturn(sampleLockData());
    ExecutorService executorService = Mockito.mock(ExecutorService.class);
    Mockito.when(executorService.getExecutor(Mockito.any(), Mockito.any())).thenReturn(executor);
    Map<String, Object> scopeValues = new TreeMap<>();
    scopeValues.put(ExecutorService.class.getName(), executorService);
    mockedScope = Mockito.mock(Scope.class);
    Mockito.when(mockedScope.getSingleton(ExecutorService.class)).thenReturn(executorService);
    lockService = new StandardLockService() {

        @Override
        public boolean hasDatabaseChangeLogLockTable() throws DatabaseException {
            return true;
        }
    };
}
Also used : Executor(liquibase.executor.Executor) Scope(liquibase.Scope) ExecutorService(liquibase.executor.ExecutorService) TreeMap(java.util.TreeMap) DatabaseException(liquibase.exception.DatabaseException) Before(org.junit.Before)

Example 3 with ExecutorService

use of liquibase.executor.ExecutorService in project keycloak by keycloak.

the class QuarkusJpaUpdaterProvider method outputChangeLogTableCreationScript.

private void outputChangeLogTableCreationScript(Liquibase liquibase, final Writer exportWriter) throws DatabaseException {
    Database database = liquibase.getDatabase();
    ExecutorService executorService = Scope.getCurrentScope().getSingleton(ExecutorService.class);
    Executor oldTemplate = executorService.getExecutor(LiquibaseConstants.JDBC_EXECUTOR, database);
    LoggingExecutor loggingExecutor = new LoggingExecutor(executorService.getExecutor(LiquibaseConstants.JDBC_EXECUTOR, database), exportWriter, database);
    executorService.setExecutor(LiquibaseConstants.JDBC_EXECUTOR, database, loggingExecutor);
    loggingExecutor.comment("*********************************************************************");
    loggingExecutor.comment("* Keycloak database creation script - apply this script to empty DB *");
    loggingExecutor.comment("*********************************************************************" + StreamUtil.getLineSeparator());
    loggingExecutor.execute(new CreateDatabaseChangeLogTableStatement());
    // DatabaseChangeLogLockTable is created before this code is executed and recreated if it does not exist automatically
    // in org.keycloak.connections.jpa.updater.liquibase.lock.CustomLockService.init() called indirectly from
    // KeycloakApplication constructor (search for waitForLock() call). Hence it is not included in the creation script.
    loggingExecutor.comment("*********************************************************************" + StreamUtil.getLineSeparator());
    executorService.setExecutor(LiquibaseConstants.JDBC_EXECUTOR, database, oldTemplate);
}
Also used : CreateDatabaseChangeLogTableStatement(liquibase.statement.core.CreateDatabaseChangeLogTableStatement) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) LoggingExecutor(liquibase.executor.LoggingExecutor) Database(liquibase.database.Database) ExecutorService(liquibase.executor.ExecutorService)

Example 4 with ExecutorService

use of liquibase.executor.ExecutorService in project keycloak by keycloak.

the class QuarkusJpaUpdaterProvider method updateChangeSet.

protected void updateChangeSet(Liquibase liquibase, Writer exportWriter) throws LiquibaseException {
    String changelog = liquibase.getChangeLogFile();
    Database database = liquibase.getDatabase();
    Table changelogTable = SnapshotGeneratorFactory.getInstance().getDatabaseChangeLogTable(new SnapshotControl(database, false, Table.class, Column.class), database);
    if (changelogTable != null) {
        boolean hasDeploymentIdColumn = changelogTable.getColumn(DEPLOYMENT_ID_COLUMN) != null;
        // create DEPLOYMENT_ID column if it doesn't exist
        if (!hasDeploymentIdColumn) {
            ChangeLogHistoryService changelogHistoryService = ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
            changelogHistoryService.generateDeploymentId();
            String deploymentId = changelogHistoryService.getDeploymentId();
            logger.debugv("Adding missing column {0}={1} to {2} table", DEPLOYMENT_ID_COLUMN, deploymentId, changelogTable.getName());
            List<SqlStatement> statementsToExecute = new ArrayList<>();
            statementsToExecute.add(new AddColumnStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), changelogTable.getName(), DEPLOYMENT_ID_COLUMN, "VARCHAR(10)", null));
            statementsToExecute.add(new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), changelogTable.getName()).addNewColumnValue(DEPLOYMENT_ID_COLUMN, deploymentId));
            statementsToExecute.add(new SetNullableStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), changelogTable.getName(), DEPLOYMENT_ID_COLUMN, "VARCHAR(10)", false));
            ExecutorService executorService = Scope.getCurrentScope().getSingleton(ExecutorService.class);
            Executor executor = executorService.getExecutor(LiquibaseConstants.JDBC_EXECUTOR, liquibase.getDatabase());
            for (SqlStatement sql : statementsToExecute) {
                executor.execute(sql);
                database.commit();
            }
        }
    }
    List<ChangeSet> changeSets = getLiquibaseUnrunChangeSets(liquibase);
    if (!changeSets.isEmpty()) {
        List<RanChangeSet> ranChangeSets = liquibase.getDatabase().getRanChangeSetList();
        if (ranChangeSets.isEmpty()) {
            logger.infov("Initializing database schema. Using changelog {0}", changelog);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debugv("Updating database from {0} to {1}. Using changelog {2}", ranChangeSets.get(ranChangeSets.size() - 1).getId(), changeSets.get(changeSets.size() - 1).getId(), changelog);
            } else {
                logger.infov("Updating database. Using changelog {0}", changelog);
            }
        }
        if (exportWriter != null) {
            if (ranChangeSets.isEmpty()) {
                outputChangeLogTableCreationScript(liquibase, exportWriter);
            }
            liquibase.update(null, new LabelExpression(), exportWriter, false);
        } else {
            liquibase.update((Contexts) null);
        }
        logger.debugv("Completed database update for changelog {0}", changelog);
    } else {
        logger.debugv("Database is up to date for changelog {0}", changelog);
    }
    // Needs to restart liquibase services to clear ChangeLogHistoryServiceFactory.getInstance().
    // See https://issues.jboss.org/browse/KEYCLOAK-3769 for discussion relevant to why reset needs to be here
    resetLiquibaseServices(liquibase);
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement) Table(liquibase.structure.core.Table) ArrayList(java.util.ArrayList) SqlStatement(liquibase.statement.SqlStatement) CustomChangeLogHistoryService(org.keycloak.connections.jpa.updater.liquibase.conn.CustomChangeLogHistoryService) ChangeLogHistoryService(liquibase.changelog.ChangeLogHistoryService) SetNullableStatement(liquibase.statement.core.SetNullableStatement) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) Column(liquibase.structure.core.Column) Database(liquibase.database.Database) ExecutorService(liquibase.executor.ExecutorService) LabelExpression(liquibase.LabelExpression) SnapshotControl(liquibase.snapshot.SnapshotControl) RanChangeSet(liquibase.changelog.RanChangeSet) ChangeSet(liquibase.changelog.ChangeSet) AddColumnStatement(liquibase.statement.core.AddColumnStatement) RanChangeSet(liquibase.changelog.RanChangeSet)

Example 5 with ExecutorService

use of liquibase.executor.ExecutorService in project keycloak by keycloak.

the class LiquibaseJpaUpdaterProvider method updateChangeSet.

protected void updateChangeSet(Liquibase liquibase, Writer exportWriter) throws LiquibaseException, SQLException {
    String changelog = liquibase.getChangeLogFile();
    Database database = liquibase.getDatabase();
    Table changelogTable = SnapshotGeneratorFactory.getInstance().getDatabaseChangeLogTable(new SnapshotControl(database, false, Table.class, Column.class), database);
    if (changelogTable != null) {
        boolean hasDeploymentIdColumn = changelogTable.getColumn(DEPLOYMENT_ID_COLUMN) != null;
        // create DEPLOYMENT_ID column if it doesn't exist
        if (!hasDeploymentIdColumn) {
            ChangeLogHistoryService changelogHistoryService = ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
            changelogHistoryService.generateDeploymentId();
            String deploymentId = changelogHistoryService.getDeploymentId();
            logger.debugv("Adding missing column {0}={1} to {2} table", DEPLOYMENT_ID_COLUMN, deploymentId, changelogTable.getName());
            List<SqlStatement> statementsToExecute = new ArrayList<>();
            statementsToExecute.add(new AddColumnStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), changelogTable.getName(), DEPLOYMENT_ID_COLUMN, "VARCHAR(10)", null));
            statementsToExecute.add(new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), changelogTable.getName()).addNewColumnValue(DEPLOYMENT_ID_COLUMN, deploymentId));
            statementsToExecute.add(new SetNullableStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), changelogTable.getName(), DEPLOYMENT_ID_COLUMN, "VARCHAR(10)", false));
            ExecutorService executorService = Scope.getCurrentScope().getSingleton(ExecutorService.class);
            Executor executor = executorService.getExecutor(LiquibaseConstants.JDBC_EXECUTOR, liquibase.getDatabase());
            for (SqlStatement sql : statementsToExecute) {
                executor.execute(sql);
                database.commit();
            }
        }
    }
    List<ChangeSet> changeSets = getLiquibaseUnrunChangeSets(liquibase);
    if (!changeSets.isEmpty()) {
        List<RanChangeSet> ranChangeSets = liquibase.getDatabase().getRanChangeSetList();
        if (ranChangeSets.isEmpty()) {
            logger.infov("Initializing database schema. Using changelog {0}", changelog);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debugv("Updating database from {0} to {1}. Using changelog {2}", ranChangeSets.get(ranChangeSets.size() - 1).getId(), changeSets.get(changeSets.size() - 1).getId(), changelog);
            } else {
                logger.infov("Updating database. Using changelog {0}", changelog);
            }
        }
        if (exportWriter != null) {
            if (ranChangeSets.isEmpty()) {
                outputChangeLogTableCreationScript(liquibase, exportWriter);
            }
            liquibase.update(null, new LabelExpression(), exportWriter, false);
        } else {
            liquibase.update((Contexts) null);
        }
        logger.debugv("Completed database update for changelog {0}", changelog);
    } else {
        logger.debugv("Database is up to date for changelog {0}", changelog);
    }
    // Needs to restart liquibase services to clear ChangeLogHistoryServiceFactory.getInstance().
    // See https://issues.jboss.org/browse/KEYCLOAK-3769 for discussion relevant to why reset needs to be here
    resetLiquibaseServices(liquibase);
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement) Table(liquibase.structure.core.Table) ArrayList(java.util.ArrayList) SqlStatement(liquibase.statement.SqlStatement) CustomChangeLogHistoryService(org.keycloak.connections.jpa.updater.liquibase.conn.CustomChangeLogHistoryService) ChangeLogHistoryService(liquibase.changelog.ChangeLogHistoryService) SetNullableStatement(liquibase.statement.core.SetNullableStatement) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) Column(liquibase.structure.core.Column) Database(liquibase.database.Database) ExecutorService(liquibase.executor.ExecutorService) LabelExpression(liquibase.LabelExpression) SnapshotControl(liquibase.snapshot.SnapshotControl) RanChangeSet(liquibase.changelog.RanChangeSet) ChangeSet(liquibase.changelog.ChangeSet) AddColumnStatement(liquibase.statement.core.AddColumnStatement) RanChangeSet(liquibase.changelog.RanChangeSet)

Aggregations

Executor (liquibase.executor.Executor)5 ExecutorService (liquibase.executor.ExecutorService)5 Database (liquibase.database.Database)4 LoggingExecutor (liquibase.executor.LoggingExecutor)4 ArrayList (java.util.ArrayList)2 LabelExpression (liquibase.LabelExpression)2 ChangeLogHistoryService (liquibase.changelog.ChangeLogHistoryService)2 ChangeSet (liquibase.changelog.ChangeSet)2 RanChangeSet (liquibase.changelog.RanChangeSet)2 SnapshotControl (liquibase.snapshot.SnapshotControl)2 SqlStatement (liquibase.statement.SqlStatement)2 AddColumnStatement (liquibase.statement.core.AddColumnStatement)2 CreateDatabaseChangeLogTableStatement (liquibase.statement.core.CreateDatabaseChangeLogTableStatement)2 SetNullableStatement (liquibase.statement.core.SetNullableStatement)2 UpdateStatement (liquibase.statement.core.UpdateStatement)2 Column (liquibase.structure.core.Column)2 Table (liquibase.structure.core.Table)2 CustomChangeLogHistoryService (org.keycloak.connections.jpa.updater.liquibase.conn.CustomChangeLogHistoryService)2 TreeMap (java.util.TreeMap)1 Scope (liquibase.Scope)1