use of liquibase.structure.DatabaseObjectComparator 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.structure.DatabaseObjectComparator in project liquibase by liquibase.
the class YamlSnapshotSerializer method toMap.
// @Override
// public String serialize(LiquibaseSerializable object, boolean pretty) {
// if (object instanceof DatabaseObject) {
// if (alreadySerializingObject) {
// return ((DatabaseObject) object).getObjectTypeName()+"#"+((DatabaseObject) object).getSnapshotId();
// } else {
// alreadySerializingObject = true;
// String string = super.serialize(object, pretty);
// alreadySerializingObject = false;
// return string;
// }
// }
// return super.serialize(object, pretty);
// }
@Override
protected Object toMap(final LiquibaseSerializable object) {
if (object instanceof DatabaseObject) {
if (alreadySerializingObject) {
String snapshotId = ((DatabaseObject) object).getSnapshotId();
if (snapshotId == null) {
String name = ((DatabaseObject) object).getName();
Object table = ((DatabaseObject) object).getAttribute("table", Object.class);
if (table == null) {
table = ((DatabaseObject) object).getAttribute("relation", Object.class);
}
if (table != null) {
name = table.toString() + "." + name;
}
if (((DatabaseObject) object).getSchema() != null) {
name = ((DatabaseObject) object).getSchema().toString() + "." + name;
}
throw new UnexpectedLiquibaseException("Found a null snapshotId for " + StringUtils.lowerCaseFirst(object.getClass().getSimpleName()) + " " + name);
}
return ((DatabaseObject) object).getClass().getName() + "#" + snapshotId;
} else {
alreadySerializingObject = true;
Object map = super.toMap(object);
alreadySerializingObject = false;
return map;
}
}
if (object instanceof DatabaseObjectCollection) {
SortedMap<String, Object> returnMap = new TreeMap<String, Object>();
for (Map.Entry<Class<? extends DatabaseObject>, Set<? extends DatabaseObject>> entry : ((DatabaseObjectCollection) object).toMap().entrySet()) {
ArrayList value = new ArrayList(entry.getValue());
Collections.sort(value, new DatabaseObjectComparator());
returnMap.put(entry.getKey().getName(), value);
}
return returnMap;
}
return super.toMap(object);
}
use of liquibase.structure.DatabaseObjectComparator 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: " + StringUtils.join(Arrays.asList(schemas), ", ", new StringUtils.StringUtilsFormatter<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 = StringUtils.trimToEmpty(referenceDatabase.getDefaultSchemaName());
}
if (comparisonName == null) {
comparisonName = StringUtils.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<Class<? extends DatabaseObject>>(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);
}
Aggregations