use of liquibase.change.core.AddPrimaryKeyChange in project liquibase by liquibase.
the class MissingPrimaryKeyChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
List<Change> returnList = new ArrayList<Change>();
PrimaryKey pk = (PrimaryKey) missingObject;
AddPrimaryKeyChange change = createAddPrimaryKeyChange();
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());
change.setColumnNames(pk.getColumnNames());
if (control.getIncludeTablespace()) {
change.setTablespace(pk.getTablespace());
}
if (referenceDatabase instanceof MSSQLDatabase && pk.getBackingIndex() != null && pk.getBackingIndex().getClustered() != null && !pk.getBackingIndex().getClustered()) {
change.setClustered(false);
}
if (referenceDatabase instanceof PostgresDatabase && pk.getBackingIndex() != null && pk.getBackingIndex().getClustered() != null && pk.getBackingIndex().getClustered()) {
change.setClustered(true);
}
if (comparisonDatabase instanceof OracleDatabase || (comparisonDatabase instanceof DB2Database && pk.getBackingIndex() != null && !comparisonDatabase.isSystemObject(pk.getBackingIndex()))) {
Index backingIndex = pk.getBackingIndex();
if (backingIndex != null && backingIndex.getName() != null) {
try {
if (!control.getIncludeCatalog() && !control.getIncludeSchema()) {
CatalogAndSchema schema = comparisonDatabase.getDefaultSchema().customize(comparisonDatabase);
//set table schema so it is found in the correct schema
backingIndex.getTable().setSchema(schema.getCatalogName(), schema.getSchemaName());
}
if (referenceDatabase.equals(comparisonDatabase) || !SnapshotGeneratorFactory.getInstance().has(backingIndex, comparisonDatabase)) {
Change[] fixes = ChangeGeneratorFactory.getInstance().fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);
if (fixes != null) {
for (Change fix : fixes) {
if (fix != null) {
returnList.add(fix);
}
}
}
}
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
change.setForIndexName(backingIndex.getName());
Schema schema = backingIndex.getSchema();
if (schema != null) {
if (control.getIncludeCatalog()) {
change.setForIndexCatalogName(schema.getCatalogName());
}
if (control.getIncludeSchema()) {
change.setForIndexSchemaName(schema.getName());
}
}
}
}
control.setAlreadyHandledMissing(pk.getBackingIndex());
returnList.add(change);
return returnList.toArray(new Change[returnList.size()]);
}
use of liquibase.change.core.AddPrimaryKeyChange 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<Change>();
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();
}
StringUtils.ToStringFormatter formatter = new StringUtils.ToStringFormatter();
control.setAlreadyHandledChanged(new Index().setTable(pk.getTable()).setColumns(referenceColumns));
if (!StringUtils.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtils.join(comparedColumns, ",", formatter))) {
control.setAlreadyHandledChanged(new Index().setTable(pk.getTable()).setColumns(comparedColumns));
}
control.setAlreadyHandledChanged(new UniqueConstraint().setTable(pk.getTable()).setColumns(referenceColumns));
if (!StringUtils.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtils.join(comparedColumns, ",", formatter))) {
control.setAlreadyHandledChanged(new UniqueConstraint().setTable(pk.getTable()).setColumns(comparedColumns));
}
return returnList.toArray(new Change[returnList.size()]);
}
Aggregations