liquibase.diff.DiffResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-core Show documentation
Show all versions of liquibase-core Show documentation
Liquibase is a tool for managing and executing database changes.
The newest version!
package liquibase.diff;
import liquibase.database.Database;
import liquibase.diff.compare.CompareControl;
import liquibase.diff.compare.DatabaseObjectComparatorFactory;
import liquibase.exception.DatabaseException;
import liquibase.snapshot.DatabaseSnapshot;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.Catalog;
import liquibase.structure.core.Column;
import liquibase.structure.core.Schema;
import liquibase.util.BooleanUtil;
import java.io.IOException;
import java.util.*;
public class DiffResult {
private final DatabaseSnapshot referenceSnapshot;
private final DatabaseSnapshot comparisonSnapshot;
private final CompareControl compareControl;
private StringDiff productNameDiff;
private StringDiff productVersionDiff;
private final Set missingObjects = new HashSet<>();
private final Set unexpectedObjects = new HashSet<>();
private final Map changedObjects = new HashMap<>();
public DiffResult(DatabaseSnapshot referenceDatabaseSnapshot, DatabaseSnapshot comparisonDatabaseSnapshot, CompareControl compareControl) {
this.referenceSnapshot = referenceDatabaseSnapshot;
this.comparisonSnapshot = comparisonDatabaseSnapshot;
this.compareControl = compareControl;
}
public DatabaseSnapshot getReferenceSnapshot() {
return referenceSnapshot;
}
public DatabaseSnapshot getComparisonSnapshot() {
return comparisonSnapshot;
}
public StringDiff getProductNameDiff() {
return productNameDiff;
}
public void setProductNameDiff(StringDiff productNameDiff) {
this.productNameDiff = productNameDiff;
}
public StringDiff getProductVersionDiff() {
return productVersionDiff;
}
public void setProductVersionDiff(StringDiff productVersionDiff) {
this.productVersionDiff = productVersionDiff;
}
public CompareControl getCompareControl() {
return compareControl;
}
public Set extends DatabaseObject> getMissingObjects() {
return missingObjects;
}
public Set getMissingObjects(Class type) {
Set returnSet = new HashSet<>();
for (DatabaseObject obj : missingObjects) {
if (type.isAssignableFrom(obj.getClass())) {
returnSet.add((T)obj);
}
}
return returnSet;
}
public SortedSet getMissingObjects(Class type, Comparator comparator) {
TreeSet set = new TreeSet<>(comparator);
set.addAll(getMissingObjects(type));
return set;
}
public T getMissingObject(T example, CompareControl.SchemaComparison[] schemaComparisons) {
Database accordingTo = getComparisonSnapshot().getDatabase();
DatabaseObjectComparatorFactory comparator = DatabaseObjectComparatorFactory.getInstance();
for (T obj : (Set) getMissingObjects(example.getClass())) {
if (comparator.isSameObject(obj, example, schemaComparisons, accordingTo)) {
return obj;
}
}
return null;
}
public void addMissingObject(DatabaseObject obj) {
if ((obj instanceof Column) && (BooleanUtil.isTrue(((Column) obj).getComputed()) || BooleanUtil.isTrue(((Column) obj).getDescending()))) {
return; //not really missing, it's a virtual column
}
missingObjects.add(obj);
}
public Set extends DatabaseObject> getUnexpectedObjects() {
return unexpectedObjects;
}
public Set getUnexpectedObjects(Class type) {
Set returnSet = new HashSet();
for (DatabaseObject obj : unexpectedObjects) {
if (type.isAssignableFrom(obj.getClass())) {
returnSet.add(obj);
}
}
return returnSet;
}
public SortedSet getUnexpectedObjects(Class type, Comparator comparator) {
TreeSet set = new TreeSet<>(comparator);
set.addAll(getUnexpectedObjects(type));
return set;
}
public T getUnexpectedObject(T example, CompareControl.SchemaComparison[] schemaComparisons) {
Database accordingTo = this.getComparisonSnapshot().getDatabase();
DatabaseObjectComparatorFactory comparator = DatabaseObjectComparatorFactory.getInstance();
for (T obj : (Set) getUnexpectedObjects(example.getClass())) {
if (comparator.isSameObject(obj, example, schemaComparisons, accordingTo)) {
return obj;
}
}
return null;
}
public void addUnexpectedObject(DatabaseObject obj) {
unexpectedObjects.add(obj);
}
public Map getChangedObjects() {
return changedObjects;
}
public Map getChangedObjects(Class type) {
Map returnSet = new HashMap();
for (Map.Entry obj : changedObjects.entrySet()) {
if (type.isAssignableFrom(obj.getKey().getClass())) {
returnSet.put(obj.getKey(), obj.getValue());
}
}
return returnSet;
}
public SortedMap getChangedObjects(Class type, Comparator comparator) {
SortedMap map = new TreeMap<>(comparator);
map.putAll(getChangedObjects(type));
return map;
}
public ObjectDifferences getChangedObject(DatabaseObject example, CompareControl.SchemaComparison[] schemaComparisons) {
Database accordingTo = this.getComparisonSnapshot().getDatabase();
DatabaseObjectComparatorFactory comparator = DatabaseObjectComparatorFactory.getInstance();
for (Map.Entry extends DatabaseObject, ObjectDifferences> entry : getChangedObjects(example.getClass()).entrySet()) {
if (comparator.isSameObject(entry.getKey(), example, schemaComparisons, accordingTo)) {
return entry.getValue();
}
}
return null;
}
public void addChangedObject(DatabaseObject obj, ObjectDifferences differences) {
if ((obj instanceof Catalog) || (obj instanceof Schema)) {
if ((differences.getSchemaComparisons() != null) && (differences.getDifferences().size() == 1) &&
(differences.getDifference("name") != null)) {
if ((obj instanceof Catalog) && this.getReferenceSnapshot().getDatabase().supports(Schema.class)) { //still save name
changedObjects.put(obj, differences);
return;
} else {
return; //don't save name differences
}
}
}
changedObjects.put(obj, differences);
}
public boolean areEqual() throws DatabaseException, IOException {
return missingObjects.isEmpty() && unexpectedObjects.isEmpty() && changedObjects.isEmpty();
}
public Set> getComparedTypes() {
return compareControl.getComparedTypes();
}
}