Search in sources :

Example 11 with DiffResult

use of liquibase.diff.DiffResult 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.toString(), 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 12 with DiffResult

use of liquibase.diff.DiffResult in project liquibase by liquibase.

the class DiffDatabaseToChangeLogTask method executeWithLiquibaseClassloader.

@Override
protected void executeWithLiquibaseClassloader() throws BuildException {
    for (ChangeLogOutputFile changeLogOutputFile : changeLogOutputFiles) {
        PrintStream printStream = null;
        String encoding = getOutputEncoding(changeLogOutputFile);
        try {
            FileResource outputFile = changeLogOutputFile.getOutputFile();
            ChangeLogSerializer changeLogSerializer = changeLogOutputFile.getChangeLogSerializer();
            printStream = new PrintStream(outputFile.getOutputStream(), true, encoding);
            DiffResult diffResult = getDiffResult();
            DiffOutputControl diffOutputControl = getDiffOutputControl();
            DiffToChangeLog diffToChangeLog = new DiffToChangeLog(diffResult, diffOutputControl);
            diffToChangeLog.print(printStream, changeLogSerializer);
        } catch (UnsupportedEncodingException e) {
            throw new BuildException("Unable to diff databases to change log file. Encoding [" + encoding + "] is not supported.", e);
        } catch (IOException e) {
            throw new BuildException("Unable to diff databases to change log file. Error creating output stream.", e);
        } catch (ParserConfigurationException e) {
            throw new BuildException("Unable to diff databases to change log file. Error configuring parser.", e);
        } catch (DatabaseException e) {
            throw new BuildException("Unable to diff databases to change log file. " + e.toString(), 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) YamlChangeLogSerializer(liquibase.serializer.core.yaml.YamlChangeLogSerializer) StringChangeLogSerializer(liquibase.serializer.core.string.StringChangeLogSerializer) XMLChangeLogSerializer(liquibase.serializer.core.xml.XMLChangeLogSerializer) IOException(java.io.IOException) ChangeLogOutputFile(liquibase.integration.ant.type.ChangeLogOutputFile) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) DiffResult(liquibase.diff.DiffResult) BuildException(org.apache.tools.ant.BuildException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DatabaseException(liquibase.exception.DatabaseException)

Example 13 with DiffResult

use of liquibase.diff.DiffResult in project liquibase by liquibase.

the class AbstractIntegrationTest method testRerunDiffChangeLogAltSchema.

@Test
public void testRerunDiffChangeLogAltSchema() throws Exception {
    if (database == null) {
        return;
    }
    if (!database.supportsSchemas()) {
        return;
    }
    Liquibase liquibase = createLiquibase(includedChangeLog);
    clearDatabase(liquibase);
    database.setDefaultSchemaName("lbcat2");
    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(null, "lbcat2")) }, 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(liquibase);
    //run again to test changelog testing logic
    Executor executor = ExecutorService.getInstance().getExecutor(database);
    try {
        executor.execute(new DropTableStatement("lbcat2", "lbcat2", database.getDatabaseChangeLogTableName(), false));
    } catch (DatabaseException e) {
    //ok
    }
    try {
        executor.execute(new DropTableStatement("lbcat2", "lbcat2", database.getDatabaseChangeLogLockTableName(), false));
    } catch (DatabaseException e) {
    //ok
    }
    database.commit();
    DatabaseConnection connection = DatabaseTestContext.getInstance().getConnection(url);
    database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
    database.setDefaultSchemaName("lbcat2");
    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(finalDiffResult.areEqual());
}
Also used : LockService(liquibase.lockservice.LockService) DiffOutputControl(liquibase.diff.output.DiffOutputControl) CatalogAndSchema(liquibase.CatalogAndSchema) Liquibase(liquibase.Liquibase) Executor(liquibase.executor.Executor) ValidationFailedException(liquibase.exception.ValidationFailedException) 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 14 with DiffResult

use of liquibase.diff.DiffResult in project liquibase by liquibase.

the class AbstractIntegrationTest method testDiffExternalForeignKeys.

//    @Test
//    public void testEncondingUpdatingDatabase() throws Exception {
//        if (database == null) {
//            return;
//        }
//        
//        // First import some data from utf8 encoded csv
//        // and create a snapshot
//        Liquibase liquibase = createLiquibase("changelogs/common/encoding.utf8.changelog.xml");
//        liquibase.update(this.contexts);
//        DatabaseSnapshot utf8Snapshot = DatabaseSnapshotGeneratorFactory.getInstance().createSnapshot(database, null, null);
//
//        clearDatabase(liquibase);
//
//        // Second import some data from latin1 encoded csv
//        // and create a snapshot
//        liquibase = createLiquibase("changelogs/common/encoding.latin1.changelog.xml");
//        liquibase.update(this.contexts);
//        DatabaseSnapshot iso88951Snapshot = DatabaseSnapshotGeneratorFactory.getInstance().createSnapshot(database, null, null);
//
//        //TODO: We need better data diff support to be able to do that
//        //Diff diff = new Diff(utf8Snapshot,iso88951Snapshot);
//        //diff.setDiffData(true);
//        //assertFalse("There are no differences setting the same data in utf-8 and iso-8895-1 "
//        //        ,diff.compare().areEqual());
//
//        //For now we do an approach reading diff data
//        DiffResult[] diffGenerators =new DiffResult[2];
//        diffGenerators[0]= DiffGeneratorFactory(utf8Snapshot,iso88951Snapshot);
//        diffGenerators[0].setDiffData(true);
//        diffGenerators[1]= new DiffGeneratorFactory(iso88951Snapshot,utf8Snapshot);
//        diffGenerators[1].setDiffData(true);
//        for(DiffGeneratorFactory diffGenerator : diffGenerators) {
//            File tempFile = File.createTempFile("liquibase-test", ".xml");
//            tempFile.deleteOnExit();
//            FileOutputStream output=new FileOutputStream(tempFile);
//            diffGenerator.compare().print(new PrintStream(output,false,"UTF-8"),database);
//            output.close();
//            String diffOutput=StreamUtil.getStreamContents(new FileInputStream(tempFile),"UTF-8");
//            assertTrue("Update to SQL preserves encoding",
//                new RegexMatcher(diffOutput, new String[] {
//                    //For the UTF-8 encoded cvs
//                    "value=\"àèìòùáéíóúÀÈÌÒÙÁÉÍÓÚâêîôûäëïöü\"",
//                    "value=\"çñ®\""
//                }).allMatchedInSequentialOrder());
//        }
//
//    }
/**
     * Test that diff is capable to detect foreign keys to external schemas that doesn't appear in the changelog
     */
@Test
public void testDiffExternalForeignKeys() throws Exception {
    if (database == null) {
        return;
    }
    Liquibase liquibase = createLiquibase(externalfkInitChangeLog);
    liquibase.update(contexts);
    DiffResult diffResult = liquibase.diff(database, null, new CompareControl());
    DiffResultAssert.assertThat(diffResult).containsMissingForeignKeyWithName("fk_person_country");
}
Also used : Liquibase(liquibase.Liquibase) CompareControl(liquibase.diff.compare.CompareControl) DiffResult(liquibase.diff.DiffResult) Test(org.junit.Test)

Example 15 with DiffResult

use of liquibase.diff.DiffResult 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

DiffResult (liquibase.diff.DiffResult)16 CompareControl (liquibase.diff.compare.CompareControl)11 Test (org.junit.Test)8 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)7 DiffOutputControl (liquibase.diff.output.DiffOutputControl)6 DiffToReport (liquibase.diff.output.report.DiffToReport)6 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)5 SnapshotControl (liquibase.snapshot.SnapshotControl)5 PrintStream (java.io.PrintStream)4 Liquibase (liquibase.Liquibase)3 CommandResult (liquibase.command.CommandResult)3 DatabaseException (liquibase.exception.DatabaseException)3 DatabaseObject (liquibase.structure.DatabaseObject)3 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 CatalogAndSchema (liquibase.CatalogAndSchema)2 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)2 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)2 ValidationFailedException (liquibase.exception.ValidationFailedException)2 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)2