Search in sources :

Example 11 with CompareControl

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

the class DiffCommand method createTargetSnapshot.

protected DatabaseSnapshot createTargetSnapshot() throws DatabaseException, InvalidExampleException {
    CatalogAndSchema[] schemas;
    if (compareControl == null || compareControl.getSchemaComparisons() == null) {
        schemas = new CatalogAndSchema[] { targetDatabase.getDefaultSchema() };
    } else {
        schemas = new CatalogAndSchema[compareControl.getSchemaComparisons().length];
        int i = 0;
        for (CompareControl.SchemaComparison comparison : compareControl.getSchemaComparisons()) {
            CatalogAndSchema schema;
            if (targetDatabase.supportsSchemas()) {
                schema = new CatalogAndSchema(targetDatabase.getDefaultCatalogName(), comparison.getComparisonSchema().getSchemaName());
            } else {
                schema = new CatalogAndSchema(comparison.getComparisonSchema().getSchemaName(), comparison.getComparisonSchema().getSchemaName());
            }
            schemas[i++] = schema;
        }
    }
    SnapshotControl snapshotControl = getTargetSnapshotControl();
    if (snapshotControl == null) {
        snapshotControl = new SnapshotControl(targetDatabase, snapshotTypes);
    }
    if (getSnapshotListener() != null) {
        snapshotControl.setSnapshotListener(getSnapshotListener());
    }
    ObjectQuotingStrategy originalStrategy = referenceDatabase.getObjectQuotingStrategy();
    try {
        referenceDatabase.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
        return SnapshotGeneratorFactory.getInstance().createSnapshot(schemas, targetDatabase, snapshotControl);
    } finally {
        referenceDatabase.setObjectQuotingStrategy(originalStrategy);
    }
}
Also used : CompareControl(liquibase.diff.compare.CompareControl) CatalogAndSchema(liquibase.CatalogAndSchema) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 12 with CompareControl

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

the class DiffToChangeLogTest method getOrderedOutputTypes_isConsistant.

@Test
public void getOrderedOutputTypes_isConsistant() throws Exception {
    MySQLDatabase database = new MySQLDatabase();
    DiffToChangeLog obj = new DiffToChangeLog(new DiffResult(new EmptyDatabaseSnapshot(database), new EmptyDatabaseSnapshot(database), new CompareControl()), null);
    for (Class<? extends ChangeGenerator> type : new Class[] { UnexpectedObjectChangeGenerator.class, MissingObjectChangeGenerator.class, ChangedObjectChangeGenerator.class }) {
        List<Class<? extends DatabaseObject>> orderedOutputTypes = obj.getOrderedOutputTypes(type);
        for (int i = 0; i < 50; i++) {
            assertThat("Error checking " + type.getName(), orderedOutputTypes, contains(obj.getOrderedOutputTypes(type).toArray()));
        }
    }
}
Also used : MySQLDatabase(liquibase.database.core.MySQLDatabase) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) CompareControl(liquibase.diff.compare.CompareControl) DatabaseObject(liquibase.structure.DatabaseObject) DiffResult(liquibase.diff.DiffResult) Test(org.junit.Test)

Example 13 with CompareControl

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

the class CommandLineUtils method doGenerateChangeLog.

public static void doGenerateChangeLog(String changeLogFile, Database originalDatabase, CatalogAndSchema[] schemas, String snapshotTypes, String author, String context, String dataDir, DiffOutputControl diffOutputControl) throws DatabaseException, IOException, ParserConfigurationException, InvalidExampleException, LiquibaseException {
    CompareControl.SchemaComparison[] comparisons = new CompareControl.SchemaComparison[schemas.length];
    int i = 0;
    for (CatalogAndSchema schema : schemas) {
        comparisons[i++] = new CompareControl.SchemaComparison(schema, schema);
    }
    CompareControl compareControl = new CompareControl(comparisons, snapshotTypes);
    diffOutputControl.setDataDir(dataDir);
    GenerateChangeLogCommand command = (GenerateChangeLogCommand) CommandFactory.getInstance().getCommand("generateChangeLog");
    command.setReferenceDatabase(originalDatabase).setSnapshotTypes(snapshotTypes).setOutputStream(System.out).setCompareControl(compareControl);
    command.setChangeLogFile(changeLogFile).setDiffOutputControl(diffOutputControl);
    command.setAuthor(author).setContext(context);
    try {
        command.execute();
    } catch (CommandExecutionException e) {
        throw new LiquibaseException(e);
    }
}
Also used : GenerateChangeLogCommand(liquibase.command.core.GenerateChangeLogCommand) CompareControl(liquibase.diff.compare.CompareControl) CommandExecutionException(liquibase.command.CommandExecutionException) CatalogAndSchema(liquibase.CatalogAndSchema)

Example 14 with CompareControl

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

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

Aggregations

CompareControl (liquibase.diff.compare.CompareControl)17 DiffResult (liquibase.diff.DiffResult)11 Test (org.junit.Test)8 CatalogAndSchema (liquibase.CatalogAndSchema)6 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)6 SnapshotControl (liquibase.snapshot.SnapshotControl)6 DiffOutputControl (liquibase.diff.output.DiffOutputControl)5 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)5 Liquibase (liquibase.Liquibase)4 DiffToReport (liquibase.diff.output.report.DiffToReport)4 CommandExecutionException (liquibase.command.CommandExecutionException)3 DatabaseObject (liquibase.structure.DatabaseObject)3 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)2 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)2 LiquibaseException (liquibase.exception.LiquibaseException)2 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)2 ValidationFailedException (liquibase.exception.ValidationFailedException)2 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)2 InvalidExampleException (liquibase.snapshot.InvalidExampleException)2 Change (liquibase.change.Change)1