Search in sources :

Example 1 with ForeignKey

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

the class ChangedForeignKeyChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    ForeignKey fk = (ForeignKey) changedObject;
    StringUtil.StringUtilFormatter formatter = new StringUtil.StringUtilFormatter<Column>() {

        @Override
        public String toString(Column obj) {
            return obj.toString(false);
        }
    };
    DropForeignKeyConstraintChange dropFkChange = new DropForeignKeyConstraintChange();
    dropFkChange.setConstraintName(fk.getName());
    dropFkChange.setBaseTableName(fk.getForeignKeyTable().getName());
    AddForeignKeyConstraintChange addFkChange = new AddForeignKeyConstraintChange();
    addFkChange.setConstraintName(fk.getName());
    addFkChange.setBaseTableName(fk.getForeignKeyTable().getName());
    addFkChange.setBaseColumnNames(StringUtil.join(fk.getForeignKeyColumns(), ",", formatter));
    addFkChange.setReferencedTableName(fk.getPrimaryKeyTable().getName());
    addFkChange.setReferencedColumnNames(StringUtil.join(fk.getPrimaryKeyColumns(), ",", formatter));
    addFkChange.setOnDelete(fk.getDeleteRule());
    addFkChange.setOnUpdate(fk.getUpdateRule());
    if (control.getIncludeCatalog()) {
        dropFkChange.setBaseTableCatalogName(fk.getForeignKeyTable().getSchema().getCatalogName());
        addFkChange.setBaseTableCatalogName(fk.getForeignKeyTable().getSchema().getCatalogName());
        addFkChange.setReferencedTableCatalogName(fk.getPrimaryKeyTable().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        dropFkChange.setBaseTableSchemaName(fk.getForeignKeyTable().getSchema().getName());
        addFkChange.setBaseTableSchemaName(fk.getForeignKeyTable().getSchema().getName());
        addFkChange.setReferencedTableSchemaName(fk.getPrimaryKeyTable().getSchema().getName());
    }
    if (fk.getBackingIndex() != null) {
        control.setAlreadyHandledChanged(fk.getBackingIndex());
    }
    return new Change[] { dropFkChange, addFkChange };
}
Also used : Column(liquibase.structure.core.Column) DropForeignKeyConstraintChange(liquibase.change.core.DropForeignKeyConstraintChange) AddForeignKeyConstraintChange(liquibase.change.core.AddForeignKeyConstraintChange) AddForeignKeyConstraintChange(liquibase.change.core.AddForeignKeyConstraintChange) Change(liquibase.change.Change) DropForeignKeyConstraintChange(liquibase.change.core.DropForeignKeyConstraintChange) ForeignKey(liquibase.structure.core.ForeignKey) StringUtil(liquibase.util.StringUtil)

Example 2 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) throws PreconditionFailedException, PreconditionErrorException {
    try {
        ForeignKey example = new ForeignKey();
        example.setName(getForeignKeyName());
        example.setForeignKeyTable(new Table());
        if (StringUtils.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) ForeignKey(liquibase.structure.core.ForeignKey)

Example 3 with ForeignKey

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

the class AddForeignKeyConstraintChange method checkStatus.

@Override
public ChangeStatus checkStatus(Database database) {
    ChangeStatus result = new ChangeStatus();
    try {
        ForeignKey example = new ForeignKey(getConstraintName(), getBaseTableCatalogName(), getBaseTableSchemaName(), getBaseTableName());
        example.setPrimaryKeyTable(new Table(getReferencedTableCatalogName(), getReferencedTableSchemaName(), getReferencedTableName()));
        example.setForeignKeyColumns(Column.listFromNames(getBaseColumnNames()));
        example.setPrimaryKeyColumns(Column.listFromNames(getReferencedColumnNames()));
        ForeignKey snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(example, database);
        result.assertComplete(snapshot != null, "Foreign key does not exist");
        if (snapshot != null) {
            if (getInitiallyDeferred() != null) {
                result.assertCorrect(getInitiallyDeferred().equals(snapshot.isInitiallyDeferred()), "Initially deferred incorrect");
            }
            if (getDeferrable() != null) {
                result.assertCorrect(getDeferrable().equals(snapshot.isDeferrable()), "Initially deferred incorrect");
            }
            if (getValidate() != null) {
                result.assertCorrect(getValidate().equals(snapshot.shouldValidate()), "validate incorrect");
            }
        }
        return result;
    } catch (Exception e) {
        return result.unknown(e);
    }
}
Also used : Table(liquibase.structure.core.Table) ForeignKey(liquibase.structure.core.ForeignKey) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 4 with ForeignKey

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

the class DropAllForeignKeyConstraintsChange method generateChildren.

/**
 * Iterates through all the FOREIGN KEYs of the target table and outputs a list of DropForeignKeyConstraintChanges
 * for a given database type.
 *
 * @param database the database type for which subchanges need to be generated
 * @return the list of generated DropForeignKeyConstraintChanges
 */
private List<DropForeignKeyConstraintChange> generateChildren(Database database) {
    // Make a new list
    List<DropForeignKeyConstraintChange> childDropChanges = new ArrayList<>();
    try {
        SnapshotControl control = new SnapshotControl(database);
        control.getTypesToInclude().add(ForeignKey.class);
        CatalogAndSchema catalogAndSchema = new CatalogAndSchema(getBaseTableCatalogName(), getBaseTableSchemaName());
        catalogAndSchema = catalogAndSchema.standardize(database);
        Table target = SnapshotGeneratorFactory.getInstance().createSnapshot(new Table(catalogAndSchema.getCatalogName(), catalogAndSchema.getSchemaName(), database.correctObjectName(getBaseTableName(), Table.class)), database);
        List<ForeignKey> results = ((target == null) ? null : target.getOutgoingForeignKeys());
        Set<String> handledConstraints = new HashSet<>();
        if ((results != null) && (!results.isEmpty())) {
            for (ForeignKey fk : results) {
                Table baseTable = fk.getForeignKeyTable();
                String constraintName = fk.getName();
                if (DatabaseObjectComparatorFactory.getInstance().isSameObject(baseTable, target, null, database)) {
                    if (!handledConstraints.contains(constraintName)) {
                        DropForeignKeyConstraintChange dropForeignKeyConstraintChange = new DropForeignKeyConstraintChange();
                        dropForeignKeyConstraintChange.setBaseTableSchemaName(getBaseTableSchemaName());
                        dropForeignKeyConstraintChange.setBaseTableName(baseTableName);
                        dropForeignKeyConstraintChange.setConstraintName(constraintName);
                        childDropChanges.add(dropForeignKeyConstraintChange);
                        handledConstraints.add(constraintName);
                    }
                } else {
                    throw new UnexpectedLiquibaseException("Expected to return only foreign keys for base table name: " + getBaseTableName() + " and got results for table: " + baseTableName);
                }
            }
        }
        return childDropChanges;
    } catch (DatabaseException | InvalidExampleException e) {
        throw new UnexpectedLiquibaseException("Failed to find foreign keys for table: " + getBaseTableName(), e);
    }
}
Also used : Table(liquibase.structure.core.Table) CatalogAndSchema(liquibase.CatalogAndSchema) ForeignKey(liquibase.structure.core.ForeignKey) InvalidExampleException(liquibase.snapshot.InvalidExampleException) SnapshotControl(liquibase.snapshot.SnapshotControl) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException)

Example 5 with ForeignKey

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

the class AddLookupTableChange method checkStatus.

@Override
public ChangeStatus checkStatus(Database database) {
    ChangeStatus result = new ChangeStatus();
    try {
        Table newTableExample = new Table(getNewTableCatalogName(), getNewTableSchemaName(), getNewTableName());
        Column newColumnExample = new Column(Table.class, getNewTableCatalogName(), getNewTableSchemaName(), getNewTableName(), getNewColumnName());
        ForeignKey foreignKeyExample = new ForeignKey(getConstraintName(), getExistingTableCatalogName(), getExistingTableSchemaName(), getExistingTableName());
        foreignKeyExample.setPrimaryKeyTable(newTableExample);
        foreignKeyExample.setForeignKeyColumns(Column.listFromNames(getExistingColumnName()));
        foreignKeyExample.setPrimaryKeyColumns(Column.listFromNames(getNewColumnName()));
        result.assertComplete(SnapshotGeneratorFactory.getInstance().has(newTableExample, database), "New table does not exist");
        result.assertComplete(SnapshotGeneratorFactory.getInstance().has(newColumnExample, database), "New column does not exist");
        result.assertComplete(SnapshotGeneratorFactory.getInstance().has(foreignKeyExample, database), "Foreign key does not exist");
        return result;
    } catch (Exception e) {
        return result.unknown(e);
    }
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) ForeignKey(liquibase.structure.core.ForeignKey)

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