Search in sources :

Example 21 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class PrimaryKeyComparator method findDifferences.

@Override
public ObjectDifferences findDifferences(DatabaseObject databaseObject1, DatabaseObject databaseObject2, Database accordingTo, CompareControl compareControl, DatabaseObjectComparatorChain chain, Set<String> exclude) {
    exclude.add("name");
    exclude.add("backingIndex");
    exclude.add("columns");
    ObjectDifferences differences = chain.findDifferences(databaseObject1, databaseObject2, accordingTo, compareControl, exclude);
    differences.compare("columns", databaseObject1, databaseObject2, new ObjectDifferences.CompareFunction() {

        @Override
        public boolean areEqual(Object referenceValue, Object compareToValue) {
            List<Column> referenceList = (List) referenceValue;
            List<Column> compareList = (List) compareToValue;
            if (referenceList.size() != compareList.size()) {
                return false;
            }
            for (int i = 0; i < referenceList.size(); i++) {
                if (!StringUtil.trimToEmpty((referenceList.get(i)).getName()).equalsIgnoreCase(StringUtil.trimToEmpty(compareList.get(i).getName()))) {
                    return false;
                }
            }
            return true;
        }
    });
    return differences;
}
Also used : ObjectDifferences(liquibase.diff.ObjectDifferences) DatabaseObject(liquibase.structure.DatabaseObject) List(java.util.List)

Example 22 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class StringSnapshotSerializerReadable method outputObjects.

protected void outputObjects(List objects, Class type, StringBuilder catalogBuffer) {
    List<? extends DatabaseObject> databaseObjects = sort(objects);
    if (!databaseObjects.isEmpty()) {
        catalogBuffer.append(type.getName()).append(":\n");
        StringBuilder typeBuffer = new StringBuilder();
        for (DatabaseObject databaseObject : databaseObjects) {
            typeBuffer.append(databaseObject.getName()).append("\n");
            typeBuffer.append(StringUtil.indent(serialize(databaseObject, null), INDENT_LENGTH)).append("\n");
        }
        catalogBuffer.append(StringUtil.indent(typeBuffer.toString(), INDENT_LENGTH)).append("\n");
    }
}
Also used : DatabaseObject(liquibase.structure.DatabaseObject)

Example 23 with DatabaseObject

use of liquibase.structure.DatabaseObject 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 24 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class AbstractChange method getAffectedDatabaseObjects.

/**
 * Implementation delegates logic to the
 * {@link liquibase.sqlgenerator.SqlGeneratorFactory#getAffectedDatabaseObjects(liquibase.statement.SqlStatement,
 * liquibase.database.Database)} method on the {@link SqlStatement} objects returned by {@link #generateStatements }
 * Returns empty set if change is not supported for the passed database
 */
@Override
public Set<DatabaseObject> getAffectedDatabaseObjects(Database database) {
    if (this.generateStatementsVolatile(database)) {
        return new HashSet<>();
    }
    Set<DatabaseObject> affectedObjects = new HashSet<>();
    SqlStatement[] statements = generateStatements(database);
    if (statements != null) {
        for (SqlStatement statement : statements) {
            affectedObjects.addAll(SqlGeneratorFactory.getInstance().getAffectedDatabaseObjects(statement, database));
        }
    }
    return affectedObjects;
}
Also used : SqlStatement(liquibase.statement.SqlStatement) DatabaseObject(liquibase.structure.DatabaseObject)

Example 25 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class DiffToReport method print.

public void print() throws DatabaseException {
    final DatabaseObjectComparator comparator = new DatabaseObjectComparator();
    out.println("Reference Database: " + diffResult.getReferenceSnapshot().getDatabase());
    out.println("Comparison Database: " + diffResult.getComparisonSnapshot().getDatabase());
    CompareControl.SchemaComparison[] schemas = diffResult.getCompareControl().getSchemaComparisons();
    if ((schemas != null) && (schemas.length > 0)) {
        out.println("Compared Schemas: " + StringUtil.join(Arrays.asList(schemas), ", ", new StringUtil.StringUtilFormatter<CompareControl.SchemaComparison>() {

            @Override
            public String toString(CompareControl.SchemaComparison obj) {
                String referenceName;
                String comparisonName;
                Database referenceDatabase = diffResult.getReferenceSnapshot().getDatabase();
                Database comparisonDatabase = diffResult.getComparisonSnapshot().getDatabase();
                if (referenceDatabase.supportsSchemas()) {
                    referenceName = obj.getReferenceSchema().getSchemaName();
                    if (referenceName == null) {
                        referenceName = referenceDatabase.getDefaultSchemaName();
                    }
                } else if (referenceDatabase.supportsCatalogs()) {
                    referenceName = obj.getReferenceSchema().getCatalogName();
                    if (referenceName == null) {
                        referenceName = referenceDatabase.getDefaultCatalogName();
                    }
                } else {
                    return "";
                }
                if (comparisonDatabase.supportsSchemas()) {
                    comparisonName = obj.getComparisonSchema().getSchemaName();
                    if (comparisonName == null) {
                        comparisonName = comparisonDatabase.getDefaultSchemaName();
                    }
                } else if (comparisonDatabase.supportsCatalogs()) {
                    comparisonName = obj.getComparisonSchema().getCatalogName();
                    if (comparisonName == null) {
                        comparisonName = comparisonDatabase.getDefaultCatalogName();
                    }
                } else {
                    return "";
                }
                if (referenceName == null) {
                    referenceName = StringUtil.trimToEmpty(referenceDatabase.getDefaultSchemaName());
                }
                if (comparisonName == null) {
                    comparisonName = StringUtil.trimToEmpty(comparisonDatabase.getDefaultSchemaName());
                }
                if (referenceName.equalsIgnoreCase(comparisonName)) {
                    return referenceName;
                } else {
                    return referenceName + " -> " + comparisonName;
                }
            }
        }, true));
    }
    printComparison("Product Name", diffResult.getProductNameDiff(), out);
    printComparison("Product Version", diffResult.getProductVersionDiff(), out);
    TreeSet<Class<? extends DatabaseObject>> types = new TreeSet<>(new Comparator<Class<? extends DatabaseObject>>() {

        @Override
        public int compare(Class<? extends DatabaseObject> o1, Class<? extends DatabaseObject> o2) {
            return o1.getSimpleName().compareTo(o2.getSimpleName());
        }
    });
    types.addAll(diffResult.getCompareControl().getComparedTypes());
    for (Class<? extends DatabaseObject> type : types) {
        if (type.equals(Schema.class) && !diffResult.getComparisonSnapshot().getDatabase().supportsSchemas()) {
            continue;
        }
        printSetComparison("Missing " + getTypeName(type), diffResult.getMissingObjects(type, comparator), out);
        printSetComparison("Unexpected " + getTypeName(type), diffResult.getUnexpectedObjects(type, comparator), out);
        printChangedComparison("Changed " + getTypeName(type), diffResult.getChangedObjects(type, comparator), out);
    }
// printColumnComparison(diffResult.getColumns().getChanged(), out);
}
Also used : Schema(liquibase.structure.core.Schema) Database(liquibase.database.Database) DatabaseObjectComparator(liquibase.structure.DatabaseObjectComparator) CompareControl(liquibase.diff.compare.CompareControl) DatabaseObject(liquibase.structure.DatabaseObject) StringUtil(liquibase.util.StringUtil)

Aggregations

DatabaseObject (liquibase.structure.DatabaseObject)47 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)14 CompareControl (liquibase.diff.compare.CompareControl)10 Change (liquibase.change.Change)9 Database (liquibase.database.Database)9 CatalogAndSchema (liquibase.CatalogAndSchema)7 DiffResult (liquibase.diff.DiffResult)6 ObjectDifferences (liquibase.diff.ObjectDifferences)6 ArrayList (java.util.ArrayList)5 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)5 Column (liquibase.structure.core.Column)5 Table (liquibase.structure.core.Table)5 HashSet (java.util.HashSet)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 DiffOutputControl (liquibase.diff.output.DiffOutputControl)4 DatabaseException (liquibase.exception.DatabaseException)4 InvalidExampleException (liquibase.snapshot.InvalidExampleException)4 SnapshotControl (liquibase.snapshot.SnapshotControl)4 DatabaseObjectCollection (liquibase.structure.DatabaseObjectCollection)4 List (java.util.List)3