Search in sources :

Example 1 with DiffToReport

use of liquibase.diff.output.report.DiffToReport in project liquibase by liquibase.

the class AbstractIntegrationTest method testDiff.

@Test
public void testDiff() throws Exception {
    assumeNotNull(this.getDatabase());
    runCompleteChangeLog();
    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");
    DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(database, database, compareControl);
    try {
        assertTrue("comapring a database with itself should return a result of 'DBs are equal'", diffResult.areEqual());
    } catch (AssertionError e) {
        new DiffToReport(diffResult, System.err).print();
        throw e;
    }
}
Also used : DiffToReport(liquibase.diff.output.report.DiffToReport) CompareControl(liquibase.diff.compare.CompareControl) DiffResult(liquibase.diff.DiffResult) Test(org.junit.Test)

Example 2 with DiffToReport

use of liquibase.diff.output.report.DiffToReport in project liquibase by liquibase.

the class H2IntegrationTest method diffToPrintStream.

@Test
public void diffToPrintStream() throws Exception {
    if (getDatabase() == null) {
        return;
    }
    runCompleteChangeLog();
    DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(getDatabase(), null, new CompareControl());
    new DiffToReport(diffResult, System.out).print();
}
Also used : DiffToReport(liquibase.diff.output.report.DiffToReport) CompareControl(liquibase.diff.compare.CompareControl) DiffResult(liquibase.diff.DiffResult) Test(org.junit.Test) AbstractIntegrationTest(liquibase.dbtest.AbstractIntegrationTest)

Example 3 with DiffToReport

use of liquibase.diff.output.report.DiffToReport in project liquibase by liquibase.

the class InternalDiffCommandStep method run.

@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
    CommandScope commandScope = resultsBuilder.getCommandScope();
    InternalSnapshotCommandStep.logUnsupportedDatabase(commandScope.getArgumentValue(REFERENCE_DATABASE_ARG), this.getClass());
    DiffResult diffResult = createDiffResult(commandScope);
    resultsBuilder.addResult("diffResult", diffResult);
    Boolean printResult = commandScope.getArgumentValue(PRINT_RESULT);
    if (printResult == null || !printResult) {
        return;
    }
    final PrintStream printStream = new PrintStream(resultsBuilder.getOutputStream());
    new DiffToReport(diffResult, printStream).print();
    printStream.flush();
    resultsBuilder.addResult("statusCode", 0);
}
Also used : PrintStream(java.io.PrintStream) DiffToReport(liquibase.diff.output.report.DiffToReport) DiffResult(liquibase.diff.DiffResult)

Example 4 with DiffToReport

use of liquibase.diff.output.report.DiffToReport in project liquibase by liquibase.

the class DiffDatabaseTask method executeWithLiquibaseClassloader.

@Override
public void executeWithLiquibaseClassloader() throws BuildException {
    PrintStream printStream = null;
    try {
        printStream = new PrintStream(outputFile.getOutputStream(), true, getOutputEncoding());
        DiffResult diffResult = getDiffResult();
        DiffToReport diffReport = new DiffToReport(diffResult, printStream);
        log("Writing diff report " + outputFile.toString(), Project.MSG_INFO);
        diffReport.print();
    } catch (DatabaseException e) {
        throw new BuildException("Unable to make diff report: " + e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        throw new BuildException("Unable to make diff report. Encoding [" + outputEncoding + "] is not supported.", e);
    } catch (IOException e) {
        throw new BuildException("Unable to make diff report. Error opening output stream.", e);
    } finally {
        FileUtils.close(printStream);
    }
}
Also used : PrintStream(java.io.PrintStream) DiffToReport(liquibase.diff.output.report.DiffToReport) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DiffResult(liquibase.diff.DiffResult) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) DatabaseException(liquibase.exception.DatabaseException)

Example 5 with DiffToReport

use of liquibase.diff.output.report.DiffToReport 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)

Aggregations

DiffResult (liquibase.diff.DiffResult)6 DiffToReport (liquibase.diff.output.report.DiffToReport)6 CompareControl (liquibase.diff.compare.CompareControl)4 Test (org.junit.Test)4 PrintStream (java.io.PrintStream)2 DiffOutputControl (liquibase.diff.output.DiffOutputControl)2 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)2 DatabaseException (liquibase.exception.DatabaseException)2 ValidationFailedException (liquibase.exception.ValidationFailedException)2 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)2 SnapshotControl (liquibase.snapshot.SnapshotControl)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 DatabaseConnection (liquibase.database.DatabaseConnection)1 OracleDatabase (liquibase.database.core.OracleDatabase)1 PostgresDatabase (liquibase.database.core.PostgresDatabase)1 JdbcConnection (liquibase.database.jvm.JdbcConnection)1 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)1 LiquibaseException (liquibase.exception.LiquibaseException)1 Executor (liquibase.executor.Executor)1