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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations