Search in sources :

Example 1 with ObjectDifferences

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

the class DiffToChangeLog method generateChangeSets.

public List<ChangeSet> generateChangeSets() {
    final ChangeGeneratorFactory changeGeneratorFactory = ChangeGeneratorFactory.getInstance();
    DatabaseObjectComparator comparator = new DatabaseObjectComparator();
    String created = null;
    if (LiquibaseConfiguration.getInstance().getProperty(GlobalConfiguration.class, GlobalConfiguration.GENERATE_CHANGESET_CREATED_VALUES).getValue(Boolean.class)) {
        created = new SimpleDateFormat("yyyy-MM-dd HH:mmZ").format(new Date());
    }
    List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
    List<Class<? extends DatabaseObject>> types = getOrderedOutputTypes(MissingObjectChangeGenerator.class);
    List<DatabaseObject> missingObjects = new ArrayList<DatabaseObject>();
    for (Class<? extends DatabaseObject> type : types) {
        for (DatabaseObject object : diffResult.getMissingObjects(type, new DatabaseObjectComparator() {

            @Override
            public int compare(DatabaseObject o1, DatabaseObject o2) {
                if (o1 instanceof Column && o1.getAttribute("order", Integer.class) != null && o2.getAttribute("order", Integer.class) != null) {
                    int i = o1.getAttribute("order", Integer.class).compareTo(o2.getAttribute("order", Integer.class));
                    if (i != 0) {
                        return i;
                    }
                }
                return super.compare(o1, o2);
            }
        })) {
            if (object == null) {
                continue;
            }
            if (!diffResult.getReferenceSnapshot().getDatabase().isLiquibaseObject(object) && !diffResult.getReferenceSnapshot().getDatabase().isSystemObject(object)) {
                missingObjects.add(object);
            }
        }
    }
    for (DatabaseObject object : sortMissingObjects(missingObjects, diffResult.getReferenceSnapshot().getDatabase())) {
        ObjectQuotingStrategy quotingStrategy = diffOutputControl.getObjectQuotingStrategy();
        Change[] changes = changeGeneratorFactory.fixMissing(object, diffOutputControl, diffResult.getReferenceSnapshot().getDatabase(), diffResult.getComparisonSnapshot().getDatabase());
        addToChangeSets(changes, changeSets, quotingStrategy, created);
    }
    types = getOrderedOutputTypes(UnexpectedObjectChangeGenerator.class);
    for (Class<? extends DatabaseObject> type : types) {
        ObjectQuotingStrategy quotingStrategy = diffOutputControl.getObjectQuotingStrategy();
        for (DatabaseObject object : sortUnexpectedObjects(diffResult.getUnexpectedObjects(type, comparator), diffResult.getReferenceSnapshot().getDatabase())) {
            if (!diffResult.getComparisonSnapshot().getDatabase().isLiquibaseObject(object) && !diffResult.getComparisonSnapshot().getDatabase().isSystemObject(object)) {
                Change[] changes = changeGeneratorFactory.fixUnexpected(object, diffOutputControl, diffResult.getReferenceSnapshot().getDatabase(), diffResult.getComparisonSnapshot().getDatabase());
                addToChangeSets(changes, changeSets, quotingStrategy, created);
            }
        }
    }
    types = getOrderedOutputTypes(ChangedObjectChangeGenerator.class);
    for (Class<? extends DatabaseObject> type : types) {
        ObjectQuotingStrategy quotingStrategy = diffOutputControl.getObjectQuotingStrategy();
        for (Map.Entry<? extends DatabaseObject, ObjectDifferences> entry : diffResult.getChangedObjects(type, comparator).entrySet()) {
            if (!diffResult.getReferenceSnapshot().getDatabase().isLiquibaseObject(entry.getKey()) && !diffResult.getReferenceSnapshot().getDatabase().isSystemObject(entry.getKey())) {
                Change[] changes = changeGeneratorFactory.fixChanged(entry.getKey(), entry.getValue(), diffOutputControl, diffResult.getReferenceSnapshot().getDatabase(), diffResult.getComparisonSnapshot().getDatabase());
                addToChangeSets(changes, changeSets, quotingStrategy, created);
            }
        }
    }
    return changeSets;
}
Also used : Column(liquibase.structure.core.Column) DatabaseObjectComparator(liquibase.structure.DatabaseObjectComparator) DatabaseObject(liquibase.structure.DatabaseObject) ChangeSet(liquibase.changelog.ChangeSet) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy) GlobalConfiguration(liquibase.configuration.GlobalConfiguration) Change(liquibase.change.Change) ObjectDifferences(liquibase.diff.ObjectDifferences) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with ObjectDifferences

use of liquibase.diff.ObjectDifferences 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 (!StringUtils.trimToEmpty((referenceList.get(i)).getName()).equalsIgnoreCase(StringUtils.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 3 with ObjectDifferences

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

the class ColumnComparator method findDifferences.

@Override
public ObjectDifferences findDifferences(DatabaseObject databaseObject1, DatabaseObject databaseObject2, Database accordingTo, CompareControl compareControl, DatabaseObjectComparatorChain chain, Set<String> exclude) {
    exclude.add("name");
    exclude.add("type");
    exclude.add("autoIncrementInformation");
    if (!LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getDiffColumnOrder()) {
        exclude.add("order");
    }
    ObjectDifferences differences = chain.findDifferences(databaseObject1, databaseObject2, accordingTo, compareControl, exclude);
    differences.compare("name", databaseObject1, databaseObject2, new ObjectDifferences.DatabaseObjectNameCompareFunction(Column.class, accordingTo));
    differences.compare("type", databaseObject1, databaseObject2, new ObjectDifferences.DatabaseObjectNameCompareFunction(Column.class, accordingTo));
    boolean autoIncrement1 = ((Column) databaseObject1).isAutoIncrement();
    boolean autoIncrement2 = ((Column) databaseObject2).isAutoIncrement();
    if (autoIncrement1 != autoIncrement2) {
        //only compare if autoIncrement or not since there are sometimes expected differences in start/increment/etc value.
        differences.addDifference("autoIncrement", autoIncrement1, autoIncrement2);
    }
    return differences;
}
Also used : ObjectDifferences(liquibase.diff.ObjectDifferences) Column(liquibase.structure.core.Column)

Example 4 with ObjectDifferences

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

the class DefaultDatabaseObjectComparator method findDifferences.

@Override
public ObjectDifferences findDifferences(DatabaseObject databaseObject1, DatabaseObject databaseObject2, Database accordingTo, CompareControl compareControl, DatabaseObjectComparatorChain chain, Set<String> exclude) {
    Set<String> attributes = new HashSet<String>();
    attributes.addAll(databaseObject1.getAttributes());
    attributes.addAll(databaseObject2.getAttributes());
    ObjectDifferences differences = new ObjectDifferences(compareControl);
    exclude.add("schema");
    exclude.add("catalog");
    for (String attribute : attributes) {
        if (exclude.contains(attribute)) {
            continue;
        }
        Object attribute1 = databaseObject1.getAttribute(attribute, Object.class);
        Object attribute2 = databaseObject2.getAttribute(attribute, Object.class);
        attribute1 = undoCollection(attribute1, attribute2);
        attribute2 = undoCollection(attribute2, attribute1);
        ObjectDifferences.CompareFunction compareFunction;
        if (attribute1 instanceof DatabaseObject || attribute2 instanceof DatabaseObject) {
            Class<? extends DatabaseObject> type;
            if (attribute1 != null) {
                type = (Class<? extends DatabaseObject>) attribute1.getClass();
            } else {
                type = (Class<? extends DatabaseObject>) attribute2.getClass();
            }
            compareFunction = new ObjectDifferences.DatabaseObjectNameCompareFunction(type, accordingTo);
        } else if (attribute1 instanceof DataType || attribute2 instanceof DataType) {
            compareFunction = new ObjectDifferences.ToStringCompareFunction(false);
        } else if (attribute1 instanceof Column.AutoIncrementInformation || attribute2 instanceof Column.AutoIncrementInformation) {
            compareFunction = new ObjectDifferences.ToStringCompareFunction(false);
        } else if (attribute1 instanceof Collection || attribute2 instanceof Collection) {
            compareFunction = new ObjectDifferences.OrderedCollectionCompareFunction(new ObjectDifferences.StandardCompareFunction(chain.getSchemaComparisons(), accordingTo));
        } else {
            compareFunction = new ObjectDifferences.StandardCompareFunction(chain.getSchemaComparisons(), accordingTo);
        }
        differences.compare(attribute, databaseObject1, databaseObject2, compareFunction);
    }
    return differences;
}
Also used : ObjectDifferences(liquibase.diff.ObjectDifferences) Column(liquibase.structure.core.Column) DatabaseObject(liquibase.structure.DatabaseObject) DataType(liquibase.structure.core.DataType) Collection(java.util.Collection) DatabaseObject(liquibase.structure.DatabaseObject) HashSet(java.util.HashSet)

Example 5 with ObjectDifferences

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

the class ForeignKeyComparator method findDifferences.

@Override
public ObjectDifferences findDifferences(DatabaseObject databaseObject1, DatabaseObject databaseObject2, Database accordingTo, CompareControl compareControl, DatabaseObjectComparatorChain chain, Set<String> exclue) {
    exclue.add("name");
    exclue.add("backingIndex");
    exclue.add("foreignKeyColumns");
    exclue.add("primaryKeyColumns");
    exclue.add("foreignKeyTable");
    exclue.add("primaryKeyTable");
    ObjectDifferences differences = chain.findDifferences(databaseObject1, databaseObject2, accordingTo, compareControl, exclue);
    differences.compare("foreignKeyColumns", databaseObject1, databaseObject2, new ObjectDifferences.DatabaseObjectNameCompareFunction(Column.class, accordingTo));
    differences.compare("primaryKeyColumns", databaseObject1, databaseObject2, new ObjectDifferences.DatabaseObjectNameCompareFunction(Column.class, accordingTo));
    differences.compare("foreignKeyTable", databaseObject1, databaseObject2, new ObjectDifferences.DatabaseObjectNameCompareFunction(Table.class, accordingTo));
    differences.compare("primaryKeyTable", databaseObject1, databaseObject2, new ObjectDifferences.DatabaseObjectNameCompareFunction(Table.class, accordingTo));
    return differences;
}
Also used : Table(liquibase.structure.core.Table) ObjectDifferences(liquibase.diff.ObjectDifferences) Column(liquibase.structure.core.Column)

Aggregations

ObjectDifferences (liquibase.diff.ObjectDifferences)10 DatabaseObject (liquibase.structure.DatabaseObject)5 Column (liquibase.structure.core.Column)4 List (java.util.List)2 CatalogAndSchema (liquibase.CatalogAndSchema)2 Schema (liquibase.structure.core.Schema)2 Table (liquibase.structure.core.Table)2 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Change (liquibase.change.Change)1 ChangeSet (liquibase.changelog.ChangeSet)1 GlobalConfiguration (liquibase.configuration.GlobalConfiguration)1 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)1 DatabaseObjectComparator (liquibase.structure.DatabaseObjectComparator)1 DataType (liquibase.structure.core.DataType)1