Search in sources :

Example 1 with DropForeignKeyConstraintChange

use of liquibase.change.core.DropForeignKeyConstraintChange in project liquibase by liquibase.

the class UnexpectedForeignKeyChangeGenerator method fixUnexpected.

@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    ForeignKey fk = (ForeignKey) unexpectedObject;
    DropForeignKeyConstraintChange change = new DropForeignKeyConstraintChange();
    change.setConstraintName(fk.getName());
    change.setBaseTableName(fk.getForeignKeyTable().getName());
    if (control.getIncludeCatalog()) {
        change.setBaseTableCatalogName(fk.getForeignKeyTable().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setBaseTableSchemaName(fk.getForeignKeyTable().getSchema().getName());
    }
    Index backingIndex = fk.getBackingIndex();
    // if (backingIndex == null) {
    // Index exampleIndex = new Index().setTable(fk.getForeignKeyTable());
    // for (String col : fk.getForeignKeyColumns().split("\\s*,\\s*")) {
    // exampleIndex.getColumns().add(col);
    // }
    // control.setAlreadyHandledUnexpected(exampleIndex);
    // } else {
    control.setAlreadyHandledUnexpected(backingIndex);
    return new Change[] { change };
}
Also used : DropForeignKeyConstraintChange(liquibase.change.core.DropForeignKeyConstraintChange) Change(liquibase.change.Change) DropForeignKeyConstraintChange(liquibase.change.core.DropForeignKeyConstraintChange)

Example 2 with DropForeignKeyConstraintChange

use of liquibase.change.core.DropForeignKeyConstraintChange 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)

Aggregations

Change (liquibase.change.Change)2 DropForeignKeyConstraintChange (liquibase.change.core.DropForeignKeyConstraintChange)2 AddForeignKeyConstraintChange (liquibase.change.core.AddForeignKeyConstraintChange)1 Column (liquibase.structure.core.Column)1 ForeignKey (liquibase.structure.core.ForeignKey)1 StringUtil (liquibase.util.StringUtil)1