use of liquibase.structure.DatabaseObject in project liquibase by liquibase.
the class MissingViewChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, final Database comparisonDatabase, ChangeGeneratorChain chain) {
View view = (View) missingObject;
CreateViewChange change = createViewChange();
change.setViewName(view.getName());
if (control.getIncludeCatalog()) {
change.setCatalogName(view.getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(view.getSchema().getName());
}
if (view.getRemarks() != null) {
change.setRemarks(view.getRemarks());
}
String selectQuery = view.getDefinition();
boolean fullDefinitionOverridden = false;
if (selectQuery == null) {
selectQuery = "COULD NOT DETERMINE VIEW QUERY";
} else if ((comparisonDatabase instanceof OracleDatabase) && (view.getColumns() != null) && !view.getColumns().isEmpty()) {
String viewName;
if ((change.getCatalogName() == null) && (change.getSchemaName() == null)) {
viewName = comparisonDatabase.escapeObjectName(change.getViewName(), View.class);
} else {
viewName = comparisonDatabase.escapeViewName(change.getCatalogName(), change.getSchemaName(), change.getViewName());
}
selectQuery = "CREATE OR REPLACE FORCE VIEW " + viewName + " (" + StringUtil.join(view.getColumns(), ", ", new StringUtil.StringUtilFormatter() {
@Override
public String toString(Object obj) {
if ((((Column) obj).getComputed() != null) && ((Column) obj).getComputed()) {
return ((Column) obj).getName();
} else {
return comparisonDatabase.escapeColumnName(null, null, null, ((Column) obj).getName(), false);
}
}
}) + ") AS " + selectQuery;
change.setFullDefinition(true);
fullDefinitionOverridden = true;
}
change.setSelectQuery(selectQuery);
if (!fullDefinitionOverridden) {
change.setFullDefinition(view.getContainsFullDefinition());
}
return new Change[] { change };
}
use of liquibase.structure.DatabaseObject in project liquibase by liquibase.
the class ObjectDifferences method compare.
public void compare(String message, String attribute, DatabaseObject referenceObject, DatabaseObject compareToObject, CompareFunction compareFunction) {
if (compareControl.isSuppressedField(referenceObject.getClass(), attribute)) {
return;
}
Object referenceValue = referenceObject.getAttribute(attribute, Object.class);
Object compareValue = compareToObject.getAttribute(attribute, Object.class);
referenceValue = undoCollection(referenceValue, compareValue);
compareValue = undoCollection(compareValue, referenceValue);
boolean different;
if ((referenceValue == null) && (compareValue == null)) {
different = false;
} else if (((referenceValue == null) && (compareValue != null)) || ((referenceValue != null) && (compareValue == null))) {
different = true;
} else {
different = !compareFunction.areEqual(referenceValue, compareValue);
}
if (different) {
addDifference(message, attribute, referenceValue, compareValue);
}
}
use of liquibase.structure.DatabaseObject 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<>();
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.structure.DatabaseObject in project liquibase by liquibase.
the class IndexComparator method findDifferences.
@Override
public ObjectDifferences findDifferences(DatabaseObject databaseObject1, DatabaseObject databaseObject2, Database accordingTo, CompareControl compareControl, DatabaseObjectComparatorChain chain, Set<String> exclude) {
exclude.add("name");
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 (referenceList.get(i) == null || compareList.get(i) == null) {
return referenceList.get(i) == compareList.get(i);
}
if (!StringUtil.trimToEmpty((referenceList.get(i)).getName()).equalsIgnoreCase(StringUtil.trimToEmpty(compareList.get(i).getName()))) {
return false;
}
}
return true;
}
});
return differences;
}
use of liquibase.structure.DatabaseObject in project liquibase by liquibase.
the class StandardDiffGenerator method compare.
@Override
public DiffResult compare(DatabaseSnapshot referenceSnapshot, DatabaseSnapshot comparisonSnapshot, CompareControl compareControl) throws DatabaseException {
if (comparisonSnapshot == null) {
try {
// , compareControl.toSnapshotControl(CompareControl.DatabaseRole.REFERENCE));
comparisonSnapshot = new EmptyDatabaseSnapshot(referenceSnapshot.getDatabase());
} catch (InvalidExampleException e) {
throw new UnexpectedLiquibaseException(e);
}
}
DiffResult diffResult = new DiffResult(referenceSnapshot, comparisonSnapshot, compareControl);
checkVersionInfo(referenceSnapshot, comparisonSnapshot, diffResult);
Set<Class<? extends DatabaseObject>> typesToCompare = compareControl.getComparedTypes();
typesToCompare.retainAll(referenceSnapshot.getSnapshotControl().getTypesToInclude());
typesToCompare.retainAll(comparisonSnapshot.getSnapshotControl().getTypesToInclude());
for (Class<? extends DatabaseObject> typeToCompare : typesToCompare) {
compareObjectType(typeToCompare, referenceSnapshot, comparisonSnapshot, diffResult);
}
return diffResult;
}
Aggregations