Search in sources :

Example 6 with ForeignKey

use of liquibase.structure.core.ForeignKey in project liquibase by liquibase.

the class ForeignKeyExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
    try {
        ForeignKey example = new ForeignKey();
        example.setName(getForeignKeyName());
        example.setForeignKeyTable(new Table());
        if (StringUtil.trimToNull(getForeignKeyTableName()) != null) {
            example.getForeignKeyTable().setName(getForeignKeyTableName());
        }
        example.getForeignKeyTable().setSchema(new Schema(getCatalogName(), getSchemaName()));
        if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
            throw new PreconditionFailedException("Foreign Key " + database.escapeIndexName(catalogName, schemaName, foreignKeyName) + " does not exist", changeLog, this);
        }
    } catch (PreconditionFailedException e) {
        throw e;
    } catch (Exception e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Table(liquibase.structure.core.Table) Schema(liquibase.structure.core.Schema) PreconditionFailedException(liquibase.exception.PreconditionFailedException) ForeignKey(liquibase.structure.core.ForeignKey) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionErrorException(liquibase.exception.PreconditionErrorException) PreconditionErrorException(liquibase.exception.PreconditionErrorException)

Example 7 with ForeignKey

use of liquibase.structure.core.ForeignKey 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)

Aggregations

ForeignKey (liquibase.structure.core.ForeignKey)7 Table (liquibase.structure.core.Table)6 Column (liquibase.structure.core.Column)3 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)2 Schema (liquibase.structure.core.Schema)2 StringUtil (liquibase.util.StringUtil)2 Set (java.util.Set)1 CatalogAndSchema (liquibase.CatalogAndSchema)1 Change (liquibase.change.Change)1 AddForeignKeyConstraintChange (liquibase.change.core.AddForeignKeyConstraintChange)1 DropForeignKeyConstraintChange (liquibase.change.core.DropForeignKeyConstraintChange)1 Database (liquibase.database.Database)1 ObjectDifferences (liquibase.diff.ObjectDifferences)1 CompareControl (liquibase.diff.compare.CompareControl)1 DatabaseObjectComparator (liquibase.diff.compare.DatabaseObjectComparator)1 DatabaseObjectComparatorChain (liquibase.diff.compare.DatabaseObjectComparatorChain)1 DatabaseObjectComparatorFactory (liquibase.diff.compare.DatabaseObjectComparatorFactory)1 DatabaseException (liquibase.exception.DatabaseException)1 PreconditionErrorException (liquibase.exception.PreconditionErrorException)1 PreconditionFailedException (liquibase.exception.PreconditionFailedException)1