Search in sources :

Example 26 with CompareControl

use of liquibase.diff.compare.CompareControl in project liquibase by liquibase.

the class AbstractIntegrationTest method testDiffExternalForeignKeys.

/**
 * Test that diff is capable to detect foreign keys to external schemas that doesn't appear in the changelog
 */
@Test
// Successful execution qualifies as test success.
@SuppressWarnings("squid:S2699")
public void testDiffExternalForeignKeys() throws Exception {
    assumeNotNull(this.getDatabase());
    clearDatabase();
    Liquibase liquibase = createLiquibase(externalfkInitChangeLog);
    liquibase.update(contexts);
    DiffResult diffResult = liquibase.diff(database, null, new CompareControl());
    DiffResultAssert.assertThat(diffResult).containsMissingForeignKeyWithName("fk_person_country");
}
Also used : CompareControl(liquibase.diff.compare.CompareControl) DiffResult(liquibase.diff.DiffResult) Test(org.junit.Test)

Example 27 with CompareControl

use of liquibase.diff.compare.CompareControl in project liquibase by liquibase.

the class AbstractIntegrationTest method testRerunDiffChangeLog.

@Test
public void testRerunDiffChangeLog() throws Exception {
    assumeNotNull(this.getDatabase());
    for (int run = 0; run < 2; run++) {
        // run once outputting data as insert, once as csv
        boolean outputCsv = run == 1;
        runCompleteChangeLog();
        SnapshotControl snapshotControl = new SnapshotControl(database);
        DatabaseSnapshot originalSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, snapshotControl);
        CompareControl compareControl = new CompareControl();
        // database returns different data even if the same
        compareControl.addSuppressedField(Column.class, "defaultValue");
        // database returns different data even if the same
        compareControl.addSuppressedField(Column.class, "autoIncrementInformation");
        if (database instanceof OracleDatabase) {
            // database returns different nvarchar2 info even though they are the same
            compareControl.addSuppressedField(Column.class, "type");
            // database returns different nullable on views, e.g. v_person.id
            compareControl.addSuppressedField(Column.class, "nullable");
        }
        if (database instanceof PostgresDatabase) {
            // database returns different nvarchar2 info even though they are the same
            compareControl.addSuppressedField(Column.class, "type");
        }
        DiffOutputControl diffOutputControl = new DiffOutputControl();
        File tempFile = tempDirectory.getRoot().createTempFile("liquibase-test", ".xml");
        if (outputCsv) {
            diffOutputControl.setDataDir(new File(tempFile.getParentFile(), "liquibase-data").getCanonicalPath().replaceFirst("\\w:", ""));
        }
        DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(database, null, compareControl);
        FileOutputStream output = new FileOutputStream(tempFile);
        try {
            new DiffToChangeLog(diffResult, new DiffOutputControl()).print(new PrintStream(output));
            output.flush();
        } finally {
            output.close();
        }
        Liquibase liquibase = createLiquibase(tempFile.getName());
        clearDatabase();
        DatabaseSnapshot emptySnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
        // run again to test changelog testing logic
        liquibase = createLiquibase(tempFile.getName());
        try {
            liquibase.update(this.contexts);
        } catch (ValidationFailedException e) {
            e.printDescriptiveError(System.out);
            throw e;
        }
        DatabaseSnapshot migratedSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
        DiffResult finalDiffResult = DiffGeneratorFactory.getInstance().compare(originalSnapshot, migratedSnapshot, compareControl);
        try {
            assertTrue("recreating the database from the generated change log should cause both 'before' and " + "'after' snapshots to be equal.", finalDiffResult.areEqual());
        } catch (AssertionError e) {
            new DiffToReport(finalDiffResult, System.err).print();
            throw e;
        }
        // diff to empty and drop all
        DiffResult emptyDiffResult = DiffGeneratorFactory.getInstance().compare(emptySnapshot, migratedSnapshot, compareControl);
        output = new FileOutputStream(tempFile);
        try {
            new DiffToChangeLog(emptyDiffResult, new DiffOutputControl(true, true, true, null)).print(new PrintStream(output));
            output.flush();
        } finally {
            output.close();
        }
        liquibase = createLiquibase(tempFile.getName());
        Scope.getCurrentScope().getLog(getClass()).info("updating from " + tempFile.getCanonicalPath());
        try {
            liquibase.update(this.contexts);
        } catch (LiquibaseException e) {
            throw e;
        }
        DatabaseSnapshot emptyAgainSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
        assertEquals("a database that was 'updated' to an empty snapshot should only have 2 tables left: " + "the database change log table and the lock table.", 2, emptyAgainSnapshot.get(Table.class).size());
        assertEquals("a database that was 'updated' to an empty snapshot should not contain any views.", 0, emptyAgainSnapshot.get(View.class).size());
    }
}
Also used : DiffOutputControl(liquibase.diff.output.DiffOutputControl) OracleDatabase(liquibase.database.core.OracleDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) ValidationFailedException(liquibase.exception.ValidationFailedException) DiffToReport(liquibase.diff.output.report.DiffToReport) CompareControl(liquibase.diff.compare.CompareControl) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) DiffResult(liquibase.diff.DiffResult) LiquibaseException(liquibase.exception.LiquibaseException) SnapshotControl(liquibase.snapshot.SnapshotControl) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) Test(org.junit.Test)

Example 28 with CompareControl

use of liquibase.diff.compare.CompareControl 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 29 with CompareControl

use of liquibase.diff.compare.CompareControl in project liquibase by liquibase.

the class LiquibaseDatabaseDiff method performLiquibaseTask.

@Override
protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException {
    // 
    if (isFormattedDiff()) {
        if (format != null && !format.equalsIgnoreCase("json")) {
            String messageString = "\nWARNING: The diff command 'diff --format=" + format + "' optional Pro parameter '--format' " + "currently supports only 'TXT' or 'JSON' as values.  (Blank defaults to 'TXT')";
            throw new LiquibaseException(String.format(messageString));
        }
        boolean hasProLicense = MavenUtils.checkProLicense(liquibaseProLicenseKey, commandName, getLog());
        if (!hasProLicense) {
            throw new LiquibaseException("The command 'diff --format=" + format + "' requires a Liquibase Pro License, available at https://www.liquibase.org/download or sales@liquibase.com." + " Options include the liquibase.pro.licenseKey in the defaults file, adding a flag in the CLI, and more. Learn more at https://docs.liquibase.com");
        }
    }
    ClassLoader cl = null;
    ResourceAccessor resourceAccessor;
    try {
        cl = getClassLoaderIncludingProjectClasspath();
        Thread.currentThread().setContextClassLoader(cl);
        ClassLoader artifactClassLoader = getMavenArtifactClassLoader();
        resourceAccessor = getResourceAccessor(artifactClassLoader);
    } catch (MojoExecutionException e) {
        throw new LiquibaseException("Could not create the class loader, " + e, e);
    }
    Database db = liquibase.getDatabase();
    Database referenceDatabase = CommandLineUtils.createDatabaseObject(resourceAccessor, referenceUrl, referenceUsername, referencePassword, referenceDriver, referenceDefaultCatalogName, referenceDefaultSchemaName, outputDefaultCatalog, outputDefaultSchema, null, null, propertyProviderClass, null, null, databaseChangeLogTableName, databaseChangeLogLockTableName);
    getLog().info("Performing Diff on database " + db.toString());
    if ((diffExcludeObjects != null) && (diffIncludeObjects != null)) {
        throw new UnexpectedLiquibaseException("Cannot specify both excludeObjects and includeObjects");
    }
    ObjectChangeFilter objectChangeFilter = null;
    if (diffExcludeObjects != null) {
        objectChangeFilter = new StandardObjectChangeFilter(StandardObjectChangeFilter.FilterType.EXCLUDE, diffExcludeObjects);
    }
    if (diffIncludeObjects != null) {
        objectChangeFilter = new StandardObjectChangeFilter(StandardObjectChangeFilter.FilterType.INCLUDE, diffIncludeObjects);
    }
    CompareControl.SchemaComparison[] schemaComparisons = createSchemaComparisons(db);
    if (diffChangeLogFile != null) {
        try {
            DiffOutputControl diffOutputControl = new DiffOutputControl(diffIncludeCatalog, diffIncludeSchema, diffIncludeTablespace, null).addIncludedSchema(new CatalogAndSchema(referenceDefaultCatalogName, referenceDefaultSchemaName));
            diffOutputControl.setObjectChangeFilter(objectChangeFilter);
            CommandLineUtils.doDiffToChangeLog(diffChangeLogFile, referenceDatabase, db, diffOutputControl, objectChangeFilter, StringUtil.trimToNull(diffTypes), schemaComparisons);
            if (new File(diffChangeLogFile).exists()) {
                getLog().info("Differences written to Change Log File, " + diffChangeLogFile);
            }
        } catch (IOException | ParserConfigurationException e) {
            throw new LiquibaseException(e);
        }
    } else {
        PrintStream printStream = createPrintStream();
        if (isFormattedDiff()) {
            CommandScope liquibaseCommand = new CommandScope("internalDiff");
            CommandScope diffCommand = CommandLineUtils.createDiffCommand(referenceDatabase, db, StringUtil.trimToNull(diffTypes), schemaComparisons, objectChangeFilter, printStream);
            CompareControl compareControl = new CompareControl(schemaComparisons, diffTypes);
            liquibaseCommand.addArgumentValue("format", format);
            liquibaseCommand.addArgumentValue("diffCommand", diffCommand);
            liquibaseCommand.addArgumentValue("targetDatabase", db);
            liquibaseCommand.addArgumentValue("referenceDatabase", referenceDatabase);
            liquibaseCommand.addArgumentValue("compareControl", compareControl);
            liquibaseCommand.addArgumentValue("objectChangeFilter", objectChangeFilter);
            if (StringUtil.isEmpty(diffTypes)) {
                liquibaseCommand.addArgumentValue("snapshotTypes", new Class[0]);
            } else {
                liquibaseCommand.addArgumentValue("snapshotTypes", diffTypes);
            }
            CommandScope formattedDiffCommand = new CommandScope("internalFormattedDiff");
            formattedDiffCommand.addArgumentValue("format", format);
            formattedDiffCommand.addArgumentValue("diffCommand", liquibaseCommand);
            formattedDiffCommand.execute();
        } else {
            CommandLineUtils.doDiff(referenceDatabase, db, StringUtil.trimToNull(diffTypes), schemaComparisons, objectChangeFilter, printStream);
        }
    }
}
Also used : ResourceAccessor(liquibase.resource.ResourceAccessor) PrintStream(java.io.PrintStream) StandardObjectChangeFilter(liquibase.diff.output.StandardObjectChangeFilter) ObjectChangeFilter(liquibase.diff.output.ObjectChangeFilter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DiffOutputControl(liquibase.diff.output.DiffOutputControl) IOException(java.io.IOException) CatalogAndSchema(liquibase.CatalogAndSchema) StandardObjectChangeFilter(liquibase.diff.output.StandardObjectChangeFilter) Database(liquibase.database.Database) CompareControl(liquibase.diff.compare.CompareControl) LiquibaseException(liquibase.exception.LiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) File(java.io.File)

Example 30 with CompareControl

use of liquibase.diff.compare.CompareControl in project camunda-bpm-platform by camunda.

the class SqlScriptTest method shouldEqualLiquibaseChangelogAndCreateScripts.

@Test
public void shouldEqualLiquibaseChangelogAndCreateScripts() throws Exception {
    // given
    SnapshotParserFactory.getInstance().register(new DirectAccessSnapshotParser());
    database = getDatabase();
    executeSqlScript("create", "engine");
    executeSqlScript("create", "identity");
    try (Liquibase liquibase = getLiquibase()) {
        // snapshot created of the database for manual scripts
        DatabaseSnapshot snapshotManualScripts = createCurrentDatabaseSnapshot();
        // database cleared and set up with Liquibase changelog
        liquibase.dropAll();
        liquibase.update(new Contexts());
        // snapshot created of the database for Liquibase changelog
        DatabaseSnapshot snapshotLiquibaseChangelog = createCurrentDatabaseSnapshot();
        // diff created for both snapshot
        DiffResult diffResult = liquibase.diff(getDatabaseForSnapshot(snapshotManualScripts), getDatabaseForSnapshot(snapshotLiquibaseChangelog), new CompareControl());
        // when generating changes to apply between both databases
        List<ChangeSet> changeSetsToApply = new DiffToChangeLog(diffResult, new CustomDiffOutputControl()).generateChangeSets();
        // then
        assertThat(changeSetsToApply).isEmpty();
    } finally {
        database = getDatabase();
        try (Liquibase liquibase = getLiquibase()) {
            liquibase.dropAll();
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) CompareControl(liquibase.diff.compare.CompareControl) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) DiffResult(liquibase.diff.DiffResult) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) Contexts(liquibase.Contexts) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test)

Aggregations

CompareControl (liquibase.diff.compare.CompareControl)30 DiffResult (liquibase.diff.DiffResult)18 Test (org.junit.Test)14 DiffOutputControl (liquibase.diff.output.DiffOutputControl)10 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)10 CatalogAndSchema (liquibase.CatalogAndSchema)8 SnapshotControl (liquibase.snapshot.SnapshotControl)8 ChangeSet (liquibase.changelog.ChangeSet)7 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)7 DatabaseObject (liquibase.structure.DatabaseObject)7 LiquibaseException (liquibase.exception.LiquibaseException)6 Database (liquibase.database.Database)5 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)4 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)4 DiffToReport (liquibase.diff.output.report.DiffToReport)4 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)4 File (java.io.File)3 Liquibase (liquibase.Liquibase)3 CommandScope (liquibase.command.CommandScope)3