use of liquibase.change.core.DropPrimaryKeyChange in project liquibase by liquibase.
the class UnexpectedPrimaryKeyChangeGenerator method fixUnexpected.
@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
// if (!diffResult.getObjectDiff(Table.class).getUnexpected().contains(pk.getTable())) {
PrimaryKey pk = (PrimaryKey) unexpectedObject;
DropPrimaryKeyChange change = new DropPrimaryKeyChange();
change.setTableName(pk.getTable().getName());
if (control.getIncludeCatalog()) {
change.setCatalogName(pk.getTable().getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(pk.getTable().getSchema().getName());
}
change.setConstraintName(pk.getName());
Index backingIndex = pk.getBackingIndex();
control.setAlreadyHandledUnexpected(backingIndex);
return new Change[] { change };
// }
}
use of liquibase.change.core.DropPrimaryKeyChange in project liquibase by liquibase.
the class ChangedPrimaryKeyChangeGenerator method fixChanged.
@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
// don't try to recreate PKs that differ in just clustered
Difference clusteredDiff = differences.getDifference("clustered");
if (clusteredDiff != null) {
if ((clusteredDiff.getReferenceValue() == null) || (clusteredDiff.getComparedValue() == null)) {
differences.removeDifference("clustered");
}
}
if (!differences.hasDifferences()) {
return new Change[0];
}
PrimaryKey pk = (PrimaryKey) changedObject;
List<Change> returnList = new ArrayList<>();
DropPrimaryKeyChange dropPkChange = new DropPrimaryKeyChange();
dropPkChange.setTableName(pk.getTable().getName());
returnList.add(dropPkChange);
AddPrimaryKeyChange addPkChange = new AddPrimaryKeyChange();
addPkChange.setTableName(pk.getTable().getName());
addPkChange.setColumnNames(pk.getColumnNames());
addPkChange.setConstraintName(pk.getName());
if (comparisonDatabase instanceof OracleDatabase) {
Index backingIndex = pk.getBackingIndex();
if ((backingIndex != null) && (backingIndex.getName() != null)) {
Change[] indexChanges = ChangeGeneratorFactory.getInstance().fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);
if (indexChanges != null) {
returnList.addAll(Arrays.asList(indexChanges));
}
addPkChange.setForIndexName(backingIndex.getName());
Schema schema = backingIndex.getSchema();
if (schema != null) {
if (control.getIncludeCatalog()) {
addPkChange.setForIndexCatalogName(schema.getCatalogName());
}
if (control.getIncludeSchema()) {
addPkChange.setForIndexSchemaName(schema.getName());
}
}
}
}
returnList.add(addPkChange);
if (control.getIncludeCatalog()) {
dropPkChange.setCatalogName(pk.getSchema().getCatalogName());
addPkChange.setCatalogName(pk.getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
dropPkChange.setSchemaName(pk.getSchema().getName());
addPkChange.setSchemaName(pk.getSchema().getName());
}
Difference columnDifferences = differences.getDifference("columns");
List<Column> referenceColumns;
List<Column> comparedColumns;
if (columnDifferences == null) {
referenceColumns = pk.getColumns();
comparedColumns = pk.getColumns();
} else {
referenceColumns = (List<Column>) columnDifferences.getReferenceValue();
comparedColumns = (List<Column>) columnDifferences.getComparedValue();
}
StringUtil.ToStringFormatter formatter = new StringUtil.ToStringFormatter();
control.setAlreadyHandledChanged(new Index().setRelation(pk.getTable()).setColumns(referenceColumns));
if (!StringUtil.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtil.join(comparedColumns, ",", formatter))) {
control.setAlreadyHandledChanged(new Index().setRelation(pk.getTable()).setColumns(comparedColumns));
}
control.setAlreadyHandledChanged(new UniqueConstraint().setRelation(pk.getTable()).setColumns(referenceColumns));
if (!StringUtil.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtil.join(comparedColumns, ",", formatter))) {
control.setAlreadyHandledChanged(new UniqueConstraint().setRelation(pk.getTable()).setColumns(comparedColumns));
}
return returnList.toArray(new Change[returnList.size()]);
}
Aggregations