Search in sources :

Example 11 with DiffToChangeLog

use of liquibase.diff.output.changelog.DiffToChangeLog in project liquibase by liquibase.

the class GenerateChangeLogTask method executeWithLiquibaseClassloader.

@Override
public void executeWithLiquibaseClassloader() throws BuildException {
    Liquibase liquibase = getLiquibase();
    Database database = liquibase.getDatabase();
    CatalogAndSchema catalogAndSchema = buildCatalogAndSchema(database);
    DiffOutputControl diffOutputControl = getDiffOutputControl();
    DiffToChangeLog diffToChangeLog = new DiffToChangeLog(diffOutputControl);
    for (ChangeLogOutputFile changeLogOutputFile : changeLogOutputFiles) {
        String encoding = getOutputEncoding(changeLogOutputFile);
        PrintStream printStream = null;
        try {
            FileResource outputFile = changeLogOutputFile.getOutputFile();
            ChangeLogSerializer changeLogSerializer = changeLogOutputFile.getChangeLogSerializer();
            log("Writing change log file " + outputFile.toString(), Project.MSG_INFO);
            printStream = new PrintStream(outputFile.getOutputStream(), true, encoding);
            liquibase.generateChangeLog(catalogAndSchema, diffToChangeLog, printStream, changeLogSerializer);
        } catch (UnsupportedEncodingException e) {
            throw new BuildException("Unable to generate a change log. Encoding [" + encoding + "] is not supported.", e);
        } catch (IOException e) {
            throw new BuildException("Unable to generate a change log. Error creating output stream.", e);
        } catch (ParserConfigurationException e) {
            throw new BuildException("Unable to generate a change log. Error configuring parser.", e);
        } catch (DatabaseException e) {
            throw new BuildException("Unable to generate a change log: " + e.getMessage(), e);
        } finally {
            FileUtils.close(printStream);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) FileResource(org.apache.tools.ant.types.resources.FileResource) DiffOutputControl(liquibase.diff.output.DiffOutputControl) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ChangeLogSerializer(liquibase.serializer.ChangeLogSerializer) JsonChangeLogSerializer(liquibase.serializer.core.json.JsonChangeLogSerializer) StringChangeLogSerializer(liquibase.serializer.core.string.StringChangeLogSerializer) IOException(java.io.IOException) CatalogAndSchema(liquibase.CatalogAndSchema) ChangeLogOutputFile(liquibase.integration.ant.type.ChangeLogOutputFile) Liquibase(liquibase.Liquibase) Database(liquibase.database.Database) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) BuildException(org.apache.tools.ant.BuildException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DatabaseException(liquibase.exception.DatabaseException)

Example 12 with DiffToChangeLog

use of liquibase.diff.output.changelog.DiffToChangeLog in project liquibase by liquibase.

the class AbstractIntegrationTest method testGenerateChangeLogWithNoChanges.

@Test
public void testGenerateChangeLogWithNoChanges() throws Exception {
    assumeNotNull(this.getDatabase());
    runCompleteChangeLog();
    DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(database, database, new CompareControl());
    DiffToChangeLog changeLogWriter = new DiffToChangeLog(diffResult, new DiffOutputControl(false, false, false, null));
    List<ChangeSet> changeSets = changeLogWriter.generateChangeSets();
    assertEquals("generating two change logs without any changes in between should result in an empty generated " + "differential change set.", 0, changeSets.size());
}
Also used : CompareControl(liquibase.diff.compare.CompareControl) DiffOutputControl(liquibase.diff.output.DiffOutputControl) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) DiffResult(liquibase.diff.DiffResult) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test)

Example 13 with DiffToChangeLog

use of liquibase.diff.output.changelog.DiffToChangeLog 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 14 with DiffToChangeLog

use of liquibase.diff.output.changelog.DiffToChangeLog 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)

Aggregations

DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)14 DiffOutputControl (liquibase.diff.output.DiffOutputControl)13 DiffResult (liquibase.diff.DiffResult)11 CompareControl (liquibase.diff.compare.CompareControl)9 ChangeSet (liquibase.changelog.ChangeSet)7 Test (org.junit.Test)7 PrintStream (java.io.PrintStream)6 Database (liquibase.database.Database)5 SnapshotControl (liquibase.snapshot.SnapshotControl)4 File (java.io.File)3 Liquibase (liquibase.Liquibase)3 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)3 DatabaseException (liquibase.exception.DatabaseException)3 ValidationFailedException (liquibase.exception.ValidationFailedException)3 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)3 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 CatalogAndSchema (liquibase.CatalogAndSchema)2 Contexts (liquibase.Contexts)2