use of liquibase.change.core.AddForeignKeyConstraintChange 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 };
}
use of liquibase.change.core.AddForeignKeyConstraintChange in project liquibase by liquibase.
the class MissingForeignKeyChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
ForeignKey fk = (ForeignKey) missingObject;
AddForeignKeyConstraintChange change = new AddForeignKeyConstraintChange();
change.setConstraintName(fk.getName());
String defaultSchemaName = StringUtil.trimToEmpty(referenceDatabase.getDefaultSchemaName());
String defaultCatalogName = StringUtil.trimToEmpty(referenceDatabase.getDefaultCatalogName());
String compDefaultSchemaName = StringUtil.trimToEmpty(comparisonDatabase.getDefaultSchemaName());
String compDefaultCatalogName = StringUtil.trimToEmpty(comparisonDatabase.getDefaultCatalogName());
boolean includedCatalog = false;
change.setReferencedTableName(fk.getPrimaryKeyTable().getName());
String missingPrimaryKeyCatalogName = StringUtil.trimToEmpty(fk.getPrimaryKeyTable().getSchema().getCatalogName());
if (referenceDatabase.supportsCatalogs()) {
if (control.getIncludeCatalog() || control.considerCatalogsAsSchemas()) {
change.setReferencedTableCatalogName(fk.getPrimaryKeyTable().getSchema().getCatalogName());
includedCatalog = true;
} else if (!defaultCatalogName.equalsIgnoreCase(missingPrimaryKeyCatalogName)) {
if (!compDefaultCatalogName.equalsIgnoreCase(missingPrimaryKeyCatalogName)) {
// don't include catalogName if it's in the default catalog
change.setReferencedTableCatalogName(fk.getPrimaryKeyTable().getSchema().getCatalogName());
includedCatalog = true;
}
}
}
String missingPrimaryKeySchemaName = StringUtil.trimToEmpty(fk.getPrimaryKeyTable().getSchema().getName());
if (referenceDatabase.supportsSchemas()) {
if (includedCatalog || control.getIncludeSchema()) {
change.setReferencedTableSchemaName(fk.getPrimaryKeyTable().getSchema().getName());
} else if (!defaultSchemaName.equalsIgnoreCase(missingPrimaryKeySchemaName)) {
if (!compDefaultSchemaName.equalsIgnoreCase(missingPrimaryKeySchemaName)) {
// don't include schemaName if it's in the default schema
change.setReferencedTableSchemaName(fk.getPrimaryKeyTable().getSchema().getName());
}
}
}
change.setReferencedColumnNames(StringUtil.join(fk.getPrimaryKeyColumns(), ",", new StringUtil.StringUtilFormatter<Column>() {
@Override
public String toString(Column obj) {
return obj.getName();
}
}));
change.setBaseTableName(fk.getForeignKeyTable().getName());
if (control.getIncludeCatalog()) {
change.setBaseTableCatalogName(fk.getForeignKeyTable().getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setBaseTableSchemaName(fk.getForeignKeyTable().getSchema().getName());
}
change.setBaseColumnNames(StringUtil.join(fk.getForeignKeyColumns(), ",", new StringUtil.StringUtilFormatter<Column>() {
@Override
public String toString(Column obj) {
return obj.getName();
}
}));
change.setDeferrable(fk.isDeferrable());
change.setInitiallyDeferred(fk.isInitiallyDeferred());
change.setValidate(fk.shouldValidate());
change.setOnUpdate(fk.getUpdateRule());
change.setOnDelete(fk.getDeleteRule());
Index backingIndex = fk.getBackingIndex();
if (referenceDatabase.createsIndexesForForeignKeys()) {
control.setAlreadyHandledMissing(backingIndex);
}
return new Change[] { change };
}
Aggregations