use of liquibase.change.core.DropIndexChange in project liquibase by liquibase.
the class ChangedIndexChangeGenerator method fixChanged.
@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
//don't try to recreate indexes that differ in just clustered
Difference clusteredDiff = differences.getDifference("clustered");
if (clusteredDiff != null) {
if (clusteredDiff.getReferenceValue() == null || clusteredDiff.getComparedValue() == null) {
differences.removeDifference("clustered");
}
}
for (String field : getIgnoredFields()) {
differences.removeDifference(field);
}
if (!differences.hasDifferences()) {
return new Change[0];
}
Index index = (Index) changedObject;
if (index.getTable() != null) {
if (index.getTable().getPrimaryKey() != null && DatabaseObjectComparatorFactory.getInstance().isSameObject(index.getTable().getPrimaryKey().getBackingIndex(), changedObject, differences.getSchemaComparisons(), comparisonDatabase)) {
return ChangeGeneratorFactory.getInstance().fixChanged(index.getTable().getPrimaryKey(), differences, control, referenceDatabase, comparisonDatabase);
}
List<UniqueConstraint> uniqueConstraints = index.getTable().getUniqueConstraints();
if (uniqueConstraints != null) {
for (UniqueConstraint constraint : uniqueConstraints) {
if (constraint.getBackingIndex() != null && DatabaseObjectComparatorFactory.getInstance().isSameObject(constraint.getBackingIndex(), changedObject, differences.getSchemaComparisons(), comparisonDatabase)) {
return ChangeGeneratorFactory.getInstance().fixChanged(constraint, differences, control, referenceDatabase, comparisonDatabase);
}
}
}
}
DropIndexChange dropIndexChange = createDropIndexChange();
dropIndexChange.setTableName(index.getTable().getName());
dropIndexChange.setIndexName(index.getName());
CreateIndexChange addIndexChange = createCreateIndexChange();
addIndexChange.setTableName(index.getTable().getName());
List<AddColumnConfig> columns = new ArrayList<AddColumnConfig>();
for (Column col : index.getColumns()) {
columns.add(new AddColumnConfig(col));
}
addIndexChange.setColumns(columns);
addIndexChange.setIndexName(index.getName());
addIndexChange.setUnique(index.isUnique());
if (control.getIncludeCatalog()) {
dropIndexChange.setCatalogName(index.getSchema().getCatalogName());
addIndexChange.setCatalogName(index.getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
dropIndexChange.setSchemaName(index.getSchema().getName());
addIndexChange.setSchemaName(index.getSchema().getName());
}
Difference columnsDifference = differences.getDifference("columns");
if (columnsDifference != null) {
List<Column> referenceColumns = (List<Column>) columnsDifference.getReferenceValue();
List<Column> comparedColumns = (List<Column>) columnsDifference.getComparedValue();
StringUtils.StringUtilsFormatter<Column> formatter = new StringUtils.StringUtilsFormatter<Column>() {
@Override
public String toString(Column obj) {
return obj.toString(false);
}
};
control.setAlreadyHandledChanged(new Index().setTable(index.getTable()).setColumns(referenceColumns));
if (!StringUtils.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtils.join(comparedColumns, ",", formatter))) {
control.setAlreadyHandledChanged(new Index().setTable(index.getTable()).setColumns(comparedColumns));
}
if (index.isUnique() != null && index.isUnique()) {
control.setAlreadyHandledChanged(new UniqueConstraint().setTable(index.getTable()).setColumns(referenceColumns));
if (!StringUtils.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtils.join(comparedColumns, ",", formatter))) {
control.setAlreadyHandledChanged(new UniqueConstraint().setTable(index.getTable()).setColumns(comparedColumns));
}
}
}
return new Change[] { dropIndexChange, addIndexChange };
}
use of liquibase.change.core.DropIndexChange in project liquibase by liquibase.
the class UnexpectedIndexChangeGenerator method fixUnexpected.
@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
Index index = (Index) unexpectedObject;
// if (index.getAssociatedWith().contains(Index.MARK_PRIMARY_KEY) || index.getAssociatedWith().contains(Index.MARK_FOREIGN_KEY) || index.getAssociatedWith().contains(Index.MARK_UNIQUE_CONSTRAINT)) {
// return null;
// }
DropIndexChange change = new DropIndexChange();
change.setTableName(index.getTable().getName());
if (control.getIncludeCatalog()) {
change.setCatalogName(index.getTable().getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(index.getTable().getSchema().getName());
}
change.setIndexName(index.getName());
change.setAssociatedWith(index.getAssociatedWithAsString());
return new Change[] { change };
}
Aggregations