Search in sources :

Example 11 with ValidationFailedException

use of liquibase.exception.ValidationFailedException in project liquibase by liquibase.

the class AbstractIntegrationTest method testRerunDiffChangeLog.

@Test
public void testRerunDiffChangeLog() throws Exception {
    if (database == null) {
        return;
    }
    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);
        //todo            compareControl.setDiffData(true);
        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");
        }
        DiffOutputControl diffOutputControl = new DiffOutputControl();
        File tempFile = File.createTempFile("liquibase-test", ".xml");
        deleteOnExit(tempFile);
        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(liquibase);
        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;
        }
        //            tempFile.deleteOnExit();
        DatabaseSnapshot migratedSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
        DiffResult finalDiffResult = DiffGeneratorFactory.getInstance().compare(originalSnapshot, migratedSnapshot, compareControl);
        try {
            assertTrue(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());
        System.out.println("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(2, emptyAgainSnapshot.get(Table.class).size());
        assertEquals(0, emptyAgainSnapshot.get(View.class).size());
    }
}
Also used : DiffOutputControl(liquibase.diff.output.DiffOutputControl) OracleDatabase(liquibase.database.core.OracleDatabase) Liquibase(liquibase.Liquibase) 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

ValidationFailedException (liquibase.exception.ValidationFailedException)11 Liquibase (liquibase.Liquibase)9 Test (org.junit.Test)8 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)6 Date (java.util.Date)4 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 DiffResult (liquibase.diff.DiffResult)2 CompareControl (liquibase.diff.compare.CompareControl)2 DiffOutputControl (liquibase.diff.output.DiffOutputControl)2 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)2 DiffToReport (liquibase.diff.output.report.DiffToReport)2 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)2 SnapshotControl (liquibase.snapshot.SnapshotControl)2 PooledDataSourceFactory (io.dropwizard.db.PooledDataSourceFactory)1 CatalogAndSchema (liquibase.CatalogAndSchema)1 RuntimeEnvironment (liquibase.RuntimeEnvironment)1 ContextChangeSetFilter (liquibase.changelog.filter.ContextChangeSetFilter)1 DbmsChangeSetFilter (liquibase.changelog.filter.DbmsChangeSetFilter)1 LabelChangeSetFilter (liquibase.changelog.filter.LabelChangeSetFilter)1