Search in sources :

Example 26 with LockService

use of liquibase.lockservice.LockService in project liquibase by liquibase.

the class AbstractIntegrationTest method testRerunDiffChangeLogAltSchema.

@Test
public void testRerunDiffChangeLogAltSchema() throws Exception {
    assumeNotNull(this.getDatabase());
    if (database.getShortName().equalsIgnoreCase("mssql")) {
        // not possible on MSSQL.
        return;
    }
    if (!database.supportsSchemas()) {
        return;
    }
    Liquibase liquibase = createLiquibase(includedChangeLog);
    database.setDefaultSchemaName("lbschem2");
    clearDatabase();
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    lockService.forceReleaseLock();
    liquibase.update(includedChangeLog);
    DatabaseSnapshot originalSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
    CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(CatalogAndSchema.DEFAULT, new CatalogAndSchema("lbcat2", null)) }, originalSnapshot.getSnapshotControl().getTypesToInclude());
    DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(database, null, compareControl);
    File tempFile = File.createTempFile("liquibase-test", ".xml");
    FileOutputStream output = new FileOutputStream(tempFile);
    try {
        new DiffToChangeLog(diffResult, new DiffOutputControl()).print(new PrintStream(output));
        output.flush();
    } finally {
        output.close();
    }
    liquibase = createLiquibase(tempFile.getName());
    clearDatabase();
    // run again to test changelog testing logic
    Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
    try {
        executor.execute(new DropTableStatement("lbcat2", null, database.getDatabaseChangeLogTableName(), false));
    } catch (DatabaseException e) {
    // ok
    }
    try {
        executor.execute(new DropTableStatement("lbcat2", null, database.getDatabaseChangeLogLockTableName(), false));
    } catch (DatabaseException e) {
    // ok
    }
    database.commit();
    DatabaseConnection connection = new JdbcConnection(testSystem.getConnection());
    database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
    database.setDefaultSchemaName("lbschem2");
    liquibase = createLiquibase(tempFile.getName());
    try {
        liquibase.update(this.contexts);
    } catch (ValidationFailedException e) {
        e.printDescriptiveError(System.out);
        throw e;
    }
    tempFile.deleteOnExit();
    DatabaseSnapshot finalSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
    CompareControl finalCompareControl = new CompareControl();
    finalCompareControl.addSuppressedField(Column.class, "autoIncrementInformation");
    DiffResult finalDiffResult = DiffGeneratorFactory.getInstance().compare(originalSnapshot, finalSnapshot, finalCompareControl);
    new DiffToReport(finalDiffResult, System.out).print();
    assertTrue("running the same change log two times against an alternative schema should produce " + "equal snapshots.", finalDiffResult.areEqual());
}
Also used : LockService(liquibase.lockservice.LockService) DiffOutputControl(liquibase.diff.output.DiffOutputControl) JdbcConnection(liquibase.database.jvm.JdbcConnection) Executor(liquibase.executor.Executor) ValidationFailedException(liquibase.exception.ValidationFailedException) ExecutorService(liquibase.executor.ExecutorService) DiffToReport(liquibase.diff.output.report.DiffToReport) CompareControl(liquibase.diff.compare.CompareControl) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) DatabaseConnection(liquibase.database.DatabaseConnection) DiffResult(liquibase.diff.DiffResult) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) DropTableStatement(liquibase.statement.core.DropTableStatement) DatabaseException(liquibase.exception.DatabaseException) Test(org.junit.Test)

Example 27 with LockService

use of liquibase.lockservice.LockService 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)

Aggregations

LockService (liquibase.lockservice.LockService)27 IOException (java.io.IOException)10 CommandScope (liquibase.command.CommandScope)9 DatabaseConnection (liquibase.database.DatabaseConnection)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)8 BufferedLogService (liquibase.logging.core.BufferedLogService)8 InvalidExampleException (liquibase.snapshot.InvalidExampleException)8 LockException (liquibase.exception.LockException)7 HubChangeExecListener (liquibase.hub.listener.HubChangeExecListener)7 Connection (liquibase.hub.model.Connection)7 Operation (liquibase.hub.model.Operation)7 CompositeLogService (liquibase.logging.core.CompositeLogService)7 LiquibaseException (liquibase.exception.LiquibaseException)6 Database (liquibase.database.Database)4 ExecutorService (liquibase.executor.ExecutorService)4 InputStreamList (liquibase.resource.InputStreamList)4 Liquibase (liquibase.Liquibase)3 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)3 JdbcConnection (liquibase.database.jvm.JdbcConnection)3 ChangeLogParseException (liquibase.exception.ChangeLogParseException)3