use of liquibase.change.core.CreateIndexChange 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.CreateIndexChange in project liquibase by liquibase.
the class MissingIndexChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
Index index = (Index) missingObject;
if (comparisonDatabase instanceof MSSQLDatabase) {
PrimaryKey primaryKey = index.getTable().getPrimaryKey();
if (primaryKey != null && DatabaseObjectComparatorFactory.getInstance().isSameObject(missingObject, primaryKey.getBackingIndex(), control.getSchemaComparisons(), referenceDatabase)) {
//will be handled by the PK
return new Change[0];
}
}
CreateIndexChange change = createCreateIndexChange();
change.setTableName(index.getTable().getName());
if (control.getIncludeTablespace()) {
change.setTablespace(index.getTablespace());
}
if (control.getIncludeCatalog()) {
change.setCatalogName(index.getTable().getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(index.getTable().getSchema().getName());
}
change.setIndexName(index.getName());
change.setUnique(index.isUnique() != null && index.isUnique() ? Boolean.TRUE : null);
change.setAssociatedWith(index.getAssociatedWithAsString());
change.setClustered(index.getClustered() != null && index.getClustered() ? Boolean.TRUE : null);
for (Column column : index.getColumns()) {
change.addColumn(new AddColumnConfig(column));
}
return new Change[] { change };
}
Aggregations