Search in sources :

Example 46 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class ForeignKeyComparator method isSameObject.

@Override
public boolean isSameObject(DatabaseObject databaseObject1, DatabaseObject databaseObject2, Database accordingTo, DatabaseObjectComparatorChain chain) {
    if (!((databaseObject1 instanceof ForeignKey) && (databaseObject2 instanceof ForeignKey))) {
        return false;
    }
    ForeignKey thisForeignKey = (ForeignKey) databaseObject1;
    ForeignKey otherForeignKey = (ForeignKey) databaseObject2;
    if ((thisForeignKey.getPrimaryKeyTable() == null) || (thisForeignKey.getForeignKeyTable() == null) || (otherForeignKey.getPrimaryKeyTable() == null) || (otherForeignKey.getForeignKeyTable() == null)) {
        if (thisForeignKey.getForeignKeyTable() != null && otherForeignKey.getForeignKeyTable() != null) {
            // FK names are not necessarily unique across all tables, so first check if FK tables are different
            if (!chain.isSameObject(thisForeignKey.getForeignKeyTable(), otherForeignKey.getForeignKeyTable(), accordingTo)) {
                return false;
            }
        }
        if ((thisForeignKey.getName() != null) && (otherForeignKey.getName() != null)) {
            if (accordingTo.isCaseSensitive()) {
                return thisForeignKey.getName().equals(otherForeignKey.getName());
            } else {
                return thisForeignKey.getName().equalsIgnoreCase(otherForeignKey.getName());
            }
        } else {
            return false;
        }
    }
    if ((thisForeignKey.getForeignKeyColumns() != null) && (thisForeignKey.getPrimaryKeyColumns() != null) && (otherForeignKey.getForeignKeyColumns() != null) && (otherForeignKey.getPrimaryKeyColumns() != null)) {
        boolean columnsTheSame;
        StringUtil.StringUtilFormatter<Column> formatter = obj -> obj.toString(false);
        if (accordingTo.isCaseSensitive()) {
            columnsTheSame = StringUtil.join(thisForeignKey.getForeignKeyColumns(), ",", formatter).equals(StringUtil.join(otherForeignKey.getForeignKeyColumns(), ",", formatter)) && StringUtil.join(thisForeignKey.getPrimaryKeyColumns(), ",", formatter).equals(StringUtil.join(otherForeignKey.getPrimaryKeyColumns(), ",", formatter));
        } else {
            columnsTheSame = StringUtil.join(thisForeignKey.getForeignKeyColumns(), ",", formatter).equalsIgnoreCase(StringUtil.join(otherForeignKey.getForeignKeyColumns(), ",", formatter)) && StringUtil.join(thisForeignKey.getPrimaryKeyColumns(), ",", formatter).equalsIgnoreCase(StringUtil.join(otherForeignKey.getPrimaryKeyColumns(), ",", formatter));
        }
        return columnsTheSame && DatabaseObjectComparatorFactory.getInstance().isSameObject(thisForeignKey.getForeignKeyTable(), otherForeignKey.getForeignKeyTable(), chain.getSchemaComparisons(), accordingTo) && DatabaseObjectComparatorFactory.getInstance().isSameObject(thisForeignKey.getPrimaryKeyTable(), otherForeignKey.getPrimaryKeyTable(), chain.getSchemaComparisons(), accordingTo);
    }
    return false;
}
Also used : DatabaseObjectComparator(liquibase.diff.compare.DatabaseObjectComparator) DatabaseObjectComparatorFactory(liquibase.diff.compare.DatabaseObjectComparatorFactory) DatabaseObjectComparatorChain(liquibase.diff.compare.DatabaseObjectComparatorChain) DatabaseObject(liquibase.structure.DatabaseObject) Column(liquibase.structure.core.Column) CompareControl(liquibase.diff.compare.CompareControl) ForeignKey(liquibase.structure.core.ForeignKey) Set(java.util.Set) Database(liquibase.database.Database) ObjectDifferences(liquibase.diff.ObjectDifferences) StringUtil(liquibase.util.StringUtil) Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) ForeignKey(liquibase.structure.core.ForeignKey) StringUtil(liquibase.util.StringUtil)

Example 47 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class SequenceSnapshotGenerator method snapshotObject.

@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException {
    if (example.getSnapshotId() != null) {
        return example;
    }
    Database database = snapshot.getDatabase();
    List<Map<String, ?>> sequences;
    if (database instanceof Db2zDatabase) {
        sequences = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database).queryForList(new RawSqlStatement(getSelectSequenceSql(example.getSchema(), database)));
        return getSequences(example, database, sequences);
    } else {
        if (example.getAttribute("liquibase-complete", false)) {
            // need to go through "snapshotting" the object even if it was previously populated in addTo. Use the "liquibase-complete" attribute to track that it doesn't need to be fully snapshotted
            example.setSnapshotId(SnapshotIdService.getInstance().generateId());
            example.setAttribute("liquibase-complete", null);
            return example;
        }
        if (!database.supportsSequences()) {
            return null;
        }
        sequences = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database).queryForList(new RawSqlStatement(getSelectSequenceSql(example.getSchema(), database)));
        DatabaseObject sequenceRow = getSequences(example, database, sequences);
        return sequenceRow;
    }
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) Database(liquibase.database.Database) ExecutorService(liquibase.executor.ExecutorService) DatabaseObject(liquibase.structure.DatabaseObject) Map(java.util.Map)

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