Search in sources :

Example 1 with CompareControl

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

the class DiffCommand method createReferenceSnapshot.

protected DatabaseSnapshot createReferenceSnapshot() 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 (referenceDatabase.supportsSchemas()) {
                schema = new CatalogAndSchema(referenceDatabase.getDefaultCatalogName(), comparison.getReferenceSchema().getSchemaName());
            } else {
                schema = new CatalogAndSchema(comparison.getReferenceSchema().getSchemaName(), comparison.getReferenceSchema().getSchemaName());
            }
            schemas[i++] = schema;
        }
    }
    SnapshotControl snapshotControl = getReferenceSnapshotControl();
    if (snapshotControl == null) {
        snapshotControl = new SnapshotControl(referenceDatabase, snapshotTypes);
    }
    if (getSnapshotListener() != null) {
        snapshotControl.setSnapshotListener(getSnapshotListener());
    }
    ObjectQuotingStrategy originalStrategy = referenceDatabase.getObjectQuotingStrategy();
    try {
        referenceDatabase.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
        return SnapshotGeneratorFactory.getInstance().createSnapshot(schemas, referenceDatabase, snapshotControl);
    } finally {
        referenceDatabase.setObjectQuotingStrategy(originalStrategy);
    }
}
Also used : CompareControl(liquibase.diff.compare.CompareControl) CatalogAndSchema(liquibase.CatalogAndSchema) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 2 with CompareControl

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

the class CommandLineUtils method doDiffToChangeLog.

public static void doDiffToChangeLog(String changeLogFile, Database referenceDatabase, Database targetDatabase, DiffOutputControl diffOutputControl, String snapshotTypes, CompareControl.SchemaComparison[] schemaComparisons) throws LiquibaseException, IOException, ParserConfigurationException {
    DiffToChangeLogCommand command = (DiffToChangeLogCommand) CommandFactory.getInstance().getCommand("diffChangeLog");
    command.setReferenceDatabase(referenceDatabase).setTargetDatabase(targetDatabase).setSnapshotTypes(snapshotTypes).setCompareControl(new CompareControl(schemaComparisons, snapshotTypes)).setOutputStream(System.out);
    command.setChangeLogFile(changeLogFile).setDiffOutputControl(diffOutputControl);
    try {
        command.execute();
    } catch (CommandExecutionException e) {
        throw new LiquibaseException(e);
    }
}
Also used : DiffToChangeLogCommand(liquibase.command.core.DiffToChangeLogCommand) CompareControl(liquibase.diff.compare.CompareControl) CommandExecutionException(liquibase.command.CommandExecutionException)

Example 3 with CompareControl

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

the class CommandLineUtils method doDiff.

public static void doDiff(Database referenceDatabase, Database targetDatabase, String snapshotTypes, CompareControl.SchemaComparison[] schemaComparisons) throws LiquibaseException {
    DiffCommand diffCommand = (DiffCommand) CommandFactory.getInstance().getCommand("diff");
    diffCommand.setReferenceDatabase(referenceDatabase).setTargetDatabase(targetDatabase).setCompareControl(new CompareControl(schemaComparisons, snapshotTypes)).setSnapshotTypes(snapshotTypes).setOutputStream(System.out);
    System.out.println("");
    System.out.println("Diff Results:");
    try {
        diffCommand.execute();
    } catch (CommandExecutionException e) {
        throw new LiquibaseException(e);
    }
}
Also used : CompareControl(liquibase.diff.compare.CompareControl) CommandExecutionException(liquibase.command.CommandExecutionException) DiffCommand(liquibase.command.core.DiffCommand)

Example 4 with CompareControl

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

the class Liquibase method generateChangeLog.

public void generateChangeLog(CatalogAndSchema catalogAndSchema, DiffToChangeLog changeLogWriter, PrintStream outputStream, ChangeLogSerializer changeLogSerializer, Class<? extends DatabaseObject>... snapshotTypes) throws DatabaseException, IOException, ParserConfigurationException {
    Set<Class<? extends DatabaseObject>> finalCompareTypes = null;
    if (snapshotTypes != null && snapshotTypes.length > 0) {
        finalCompareTypes = new HashSet<Class<? extends DatabaseObject>>(Arrays.asList(snapshotTypes));
    }
    SnapshotControl snapshotControl = new SnapshotControl(this.getDatabase(), snapshotTypes);
    CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(catalogAndSchema, catalogAndSchema) }, finalCompareTypes);
    //        compareControl.addStatusListener(new OutDiffStatusListener());
    DatabaseSnapshot originalDatabaseSnapshot = null;
    try {
        originalDatabaseSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(compareControl.getSchemas(CompareControl.DatabaseRole.REFERENCE), getDatabase(), snapshotControl);
        DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(originalDatabaseSnapshot, SnapshotGeneratorFactory.getInstance().createSnapshot(compareControl.getSchemas(CompareControl.DatabaseRole.REFERENCE), null, snapshotControl), compareControl);
        changeLogWriter.setDiffResult(diffResult);
        if (changeLogSerializer != null) {
            changeLogWriter.print(outputStream, changeLogSerializer);
        } else {
            changeLogWriter.print(outputStream);
        }
    } catch (InvalidExampleException e) {
        throw new UnexpectedLiquibaseException(e);
    }
}
Also used : InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseObject(liquibase.structure.DatabaseObject) CompareControl(liquibase.diff.compare.CompareControl) DiffResult(liquibase.diff.DiffResult) SnapshotControl(liquibase.snapshot.SnapshotControl) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 5 with CompareControl

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

the class LiquibaseHelper method generateMigrations.

private File generateMigrations(final Database referenceDatabase, final Database targetDatabase) throws LiquibaseException, IOException {
    if (!resourcesDir.exists()) {
        resourcesDir.mkdirs();
    }
    if (!migrationsDir.exists()) {
        migrationsDir.mkdirs();
    }
    if (masterChangeLogFile.exists()) {
        LOG.info("Checking current database state");
        validateDatabaseState(targetDatabase);
    } else {
        LOG.info("Creating new master changelog");
        writeChangeSets(masterChangeLogFile, emptyList());
    }
    @SuppressWarnings("unchecked") final SnapshotControl snapshotControl = new SnapshotControl(referenceDatabase, liquibase.structure.core.Schema.class, liquibase.structure.core.Table.class, liquibase.structure.core.Column.class, liquibase.structure.core.PrimaryKey.class, liquibase.structure.core.Index.class);
    LOG.info("Executing diff");
    final CompareControl compareControl = new CompareControl(snapshotControl.getTypesToInclude());
    final DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(referenceDatabase, targetDatabase, compareControl);
    LOG.info("Converting diff to changelog");
    final DiffOutputControl diffOutputControl = new DiffOutputControl(false, false, true, null);
    final DiffToChangeLog diffToChangeLog = new DiffToChangeLog(diffResult, diffOutputControl);
    diffToChangeLog.setChangeSetAuthor(System.getProperty("user.name"));
    final List<ChangeSet> changeSets = filterChangeSets(diffToChangeLog.generateChangeSets());
    LOG.info("Found {} changes", changeSets.size());
    if (changeSets.isEmpty()) {
        return null;
    }
    final File generatedChangeLogFile = new File(migrationsDir, generateFileName(masterChangeLogFile));
    LOG.info("Writing new changelog: {}", generatedChangeLogFile);
    writeChangeSets(generatedChangeLogFile, changeSets);
    LOG.info("Add migration to master changelog: {}", masterChangeLogFile);
    addIncludeFile(generatedChangeLogFile);
    LOG.info("Cleaning changelog");
    cleanXmlFile(masterChangeLogFile);
    cleanXmlFile(generatedChangeLogFile);
    LOG.info("Diff complete");
    return generatedChangeLogFile;
}
Also used : CompareControl(liquibase.diff.compare.CompareControl) DiffOutputControl(liquibase.diff.output.DiffOutputControl) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) DiffResult(liquibase.diff.DiffResult) SnapshotControl(liquibase.snapshot.SnapshotControl) ChangeSet(liquibase.changelog.ChangeSet) File(java.io.File)

Aggregations

CompareControl (liquibase.diff.compare.CompareControl)29 DiffResult (liquibase.diff.DiffResult)17 Test (org.junit.Test)13 DiffOutputControl (liquibase.diff.output.DiffOutputControl)10 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)9 CatalogAndSchema (liquibase.CatalogAndSchema)8 SnapshotControl (liquibase.snapshot.SnapshotControl)8 DatabaseObject (liquibase.structure.DatabaseObject)7 ChangeSet (liquibase.changelog.ChangeSet)6 LiquibaseException (liquibase.exception.LiquibaseException)6 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)6 Database (liquibase.database.Database)5 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)4 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)4 DiffToReport (liquibase.diff.output.report.DiffToReport)4 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)4 File (java.io.File)3 CommandScope (liquibase.command.CommandScope)3 MySQLDatabase (liquibase.database.core.MySQLDatabase)3