Search in sources :

Example 1 with CreateIndexChange

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 };
}
Also used : DropIndexChange(liquibase.change.core.DropIndexChange) ArrayList(java.util.ArrayList) UniqueConstraint(liquibase.structure.core.UniqueConstraint) Index(liquibase.structure.core.Index) Difference(liquibase.diff.Difference) DropIndexChange(liquibase.change.core.DropIndexChange) Change(liquibase.change.Change) CreateIndexChange(liquibase.change.core.CreateIndexChange) Column(liquibase.structure.core.Column) StringUtils(liquibase.util.StringUtils) AddColumnConfig(liquibase.change.AddColumnConfig) ArrayList(java.util.ArrayList) List(java.util.List) CreateIndexChange(liquibase.change.core.CreateIndexChange)

Example 2 with CreateIndexChange

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 };
}
Also used : Column(liquibase.structure.core.Column) PrimaryKey(liquibase.structure.core.PrimaryKey) Index(liquibase.structure.core.Index) AddColumnConfig(liquibase.change.AddColumnConfig) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Change(liquibase.change.Change) CreateIndexChange(liquibase.change.core.CreateIndexChange) CreateIndexChange(liquibase.change.core.CreateIndexChange)

Aggregations

AddColumnConfig (liquibase.change.AddColumnConfig)2 Change (liquibase.change.Change)2 CreateIndexChange (liquibase.change.core.CreateIndexChange)2 Column (liquibase.structure.core.Column)2 Index (liquibase.structure.core.Index)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DropIndexChange (liquibase.change.core.DropIndexChange)1 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)1 Difference (liquibase.diff.Difference)1 PrimaryKey (liquibase.structure.core.PrimaryKey)1 UniqueConstraint (liquibase.structure.core.UniqueConstraint)1 StringUtils (liquibase.util.StringUtils)1