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 };
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations