Search in sources :

Example 16 with DatabaseObject

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 };
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) Column(liquibase.structure.core.Column) CreateViewChange(liquibase.change.core.CreateViewChange) DatabaseObject(liquibase.structure.DatabaseObject) Change(liquibase.change.Change) CreateViewChange(liquibase.change.core.CreateViewChange) View(liquibase.structure.core.View)

Example 17 with DatabaseObject

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);
    }
}
Also used : DatabaseObject(liquibase.structure.DatabaseObject)

Example 18 with DatabaseObject

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;
}
Also used : ObjectDifferences(liquibase.diff.ObjectDifferences) DatabaseObject(liquibase.structure.DatabaseObject) DataType(liquibase.structure.core.DataType) Collection(java.util.Collection) DatabaseObject(liquibase.structure.DatabaseObject) HashSet(java.util.HashSet)

Example 19 with DatabaseObject

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;
}
Also used : ObjectDifferences(liquibase.diff.ObjectDifferences) DatabaseObject(liquibase.structure.DatabaseObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 20 with DatabaseObject

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;
}
Also used : InvalidExampleException(liquibase.snapshot.InvalidExampleException) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) DatabaseObject(liquibase.structure.DatabaseObject) DiffResult(liquibase.diff.DiffResult) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Aggregations

DatabaseObject (liquibase.structure.DatabaseObject)47 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)14 CompareControl (liquibase.diff.compare.CompareControl)10 Change (liquibase.change.Change)9 Database (liquibase.database.Database)9 CatalogAndSchema (liquibase.CatalogAndSchema)7 DiffResult (liquibase.diff.DiffResult)6 ObjectDifferences (liquibase.diff.ObjectDifferences)6 ArrayList (java.util.ArrayList)5 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)5 Column (liquibase.structure.core.Column)5 Table (liquibase.structure.core.Table)5 HashSet (java.util.HashSet)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 DiffOutputControl (liquibase.diff.output.DiffOutputControl)4 DatabaseException (liquibase.exception.DatabaseException)4 InvalidExampleException (liquibase.snapshot.InvalidExampleException)4 SnapshotControl (liquibase.snapshot.SnapshotControl)4 DatabaseObjectCollection (liquibase.structure.DatabaseObjectCollection)4 List (java.util.List)3