Search in sources :

Example 1 with ChangeLogHistoryService

use of liquibase.changelog.ChangeLogHistoryService in project iaf by ibissource.

the class LiquibaseMigrator method doValidate.

private void doValidate() throws LiquibaseException, SQLException {
    try (Liquibase liquibase = createMigrator()) {
        Database database = liquibase.getDatabase();
        LockService lockService = LockServiceFactory.getInstance().getLockService(database);
        lockService.waitForLock();
        List<RanChangeSet> alreadyExecutedChangeSets = database.getRanChangeSetList();
        for (RanChangeSet ranChangeSet : alreadyExecutedChangeSets) {
            CheckSum checkSum = ranChangeSet.getLastCheckSum();
            if (checkSum != null && checkSum.getVersion() < CheckSum.getCurrentVersion()) {
                migrationLog.warn("checksum [" + checkSum + "] for changeset [" + ranChangeSet + "] is outdated and will be updated");
            }
        }
        DatabaseChangeLog changeLog;
        try {
            changeLog = liquibase.getDatabaseChangeLog();
            ChangeLogHistoryService changeLogHistoryService = ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
            changeLogHistoryService.init();
            // Validate old checksums and update if required
            changeLogHistoryService.upgradeChecksums(changeLog, contexts, labelExpression);
            changeLogHistoryService.reset();
            // Validate the new (updated) checksums
            changeLog.validate(database, contexts, labelExpression);
        } finally {
            try {
                lockService.releaseLock();
            } catch (LockException e) {
                log.warn("unable to clean up Liquibase Lock", e);
            }
            LockServiceFactory.getInstance().resetAll();
            ChangeLogHistoryServiceFactory.getInstance().resetAll();
            Scope.getCurrentScope().getSingleton(ExecutorService.class).reset();
            liquibase.setChangeExecListener(null);
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) ChangeLogHistoryService(liquibase.changelog.ChangeLogHistoryService) LockService(liquibase.lockservice.LockService) LockException(liquibase.exception.LockException) CheckSum(liquibase.change.CheckSum) Database(liquibase.database.Database) ExecutorService(liquibase.executor.ExecutorService) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) RanChangeSet(liquibase.changelog.RanChangeSet)

Example 2 with ChangeLogHistoryService

use of liquibase.changelog.ChangeLogHistoryService in project liquibase by liquibase.

the class InternalHistoryCommandStep method run.

@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
    PrintWriter output = new PrintWriter(resultsBuilder.getOutputStream());
    CommandScope commandScope = resultsBuilder.getCommandScope();
    DeploymentHistory deploymentHistory = new DeploymentHistory();
    Database database = commandScope.getArgumentValue(DATABASE_ARG);
    ChangeLogHistoryService historyService = ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
    output.println("Liquibase History for " + database.getConnection().getURL());
    output.println("");
    DeploymentDetails deployment = null;
    for (RanChangeSet ranChangeSet : historyService.getRanChangeSets()) {
        final String thisDeploymentId = ranChangeSet.getDeploymentId();
        if (deployment == null || !Objects.equals(thisDeploymentId, deployment.getDeploymentId())) {
            if (deployment != null) {
                deployment.printReport(output);
            }
            deployment = new DeploymentDetails(commandScope);
            deploymentHistory.deployments.add(deployment);
        }
        deployment.changeSets.add(ranChangeSet);
    }
    if (deployment == null) {
        output.println("No changesets deployed");
    } else {
        deployment.printReport(output);
    }
    resultsBuilder.addResult(DEPLOYMENTS_RESULT, deploymentHistory);
    output.flush();
}
Also used : ChangeLogHistoryService(liquibase.changelog.ChangeLogHistoryService) Database(liquibase.database.Database) PrintWriter(java.io.PrintWriter) RanChangeSet(liquibase.changelog.RanChangeSet)

Example 3 with ChangeLogHistoryService

use of liquibase.changelog.ChangeLogHistoryService in project liquibase by liquibase.

the class InternalDropAllCommandStep method checkLiquibaseTables.

protected void checkLiquibaseTables(Database database) throws LiquibaseException {
    ChangeLogHistoryService changeLogHistoryService = ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
    changeLogHistoryService.init();
    LockServiceFactory.getInstance().getLockService(database).init();
}
Also used : ChangeLogHistoryService(liquibase.changelog.ChangeLogHistoryService)

Example 4 with ChangeLogHistoryService

use of liquibase.changelog.ChangeLogHistoryService in project liquibase by liquibase.

the class DropAllCommand method checkLiquibaseTables.

protected void checkLiquibaseTables(boolean updateExistingNullChecksums, DatabaseChangeLog databaseChangeLog, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
    ChangeLogHistoryService changeLogHistoryService = ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
    changeLogHistoryService.init();
    if (updateExistingNullChecksums) {
        changeLogHistoryService.upgradeChecksums(databaseChangeLog, contexts, labelExpression);
    }
    LockServiceFactory.getInstance().getLockService(database).init();
}
Also used : ChangeLogHistoryService(liquibase.changelog.ChangeLogHistoryService)

Example 5 with ChangeLogHistoryService

use of liquibase.changelog.ChangeLogHistoryService 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

ChangeLogHistoryService (liquibase.changelog.ChangeLogHistoryService)6 RanChangeSet (liquibase.changelog.RanChangeSet)4 Database (liquibase.database.Database)4 ExecutorService (liquibase.executor.ExecutorService)3 ArrayList (java.util.ArrayList)2 LabelExpression (liquibase.LabelExpression)2 ChangeSet (liquibase.changelog.ChangeSet)2 Executor (liquibase.executor.Executor)2 LoggingExecutor (liquibase.executor.LoggingExecutor)2 SnapshotControl (liquibase.snapshot.SnapshotControl)2 SqlStatement (liquibase.statement.SqlStatement)2 AddColumnStatement (liquibase.statement.core.AddColumnStatement)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 PrintWriter (java.io.PrintWriter)1 Liquibase (liquibase.Liquibase)1 CheckSum (liquibase.change.CheckSum)1