Search in sources :

Example 1 with AddUniqueConstraintChange

use of liquibase.change.core.AddUniqueConstraintChange in project liquibase by liquibase.

the class MissingUniqueConstraintChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    List<Change> returnList = new ArrayList<Change>();
    UniqueConstraint uc = (UniqueConstraint) missingObject;
    if (uc.getTable() == null) {
        return null;
    }
    AddUniqueConstraintChange change = new AddUniqueConstraintChange();
    change.setTableName(uc.getTable().getName());
    if (uc.getBackingIndex() != null && control.getIncludeTablespace()) {
        change.setTablespace(uc.getBackingIndex().getTablespace());
    }
    if (control.getIncludeCatalog()) {
        change.setCatalogName(uc.getTable().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(uc.getTable().getSchema().getName());
    }
    change.setConstraintName(uc.getName());
    change.setColumnNames(uc.getColumnNames());
    change.setDeferrable(uc.isDeferrable() ? Boolean.TRUE : null);
    change.setInitiallyDeferred(uc.isInitiallyDeferred() ? Boolean.TRUE : null);
    change.setDisabled(uc.isDisabled() ? Boolean.TRUE : null);
    if (referenceDatabase instanceof MSSQLDatabase) {
        change.setClustered(uc.isClustered() ? Boolean.TRUE : null);
    }
    if (comparisonDatabase instanceof OracleDatabase) {
        Index backingIndex = uc.getBackingIndex();
        if (backingIndex != null && backingIndex.getName() != null) {
            Change[] changes = ChangeGeneratorFactory.getInstance().fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);
            if (changes != null) {
                returnList.addAll(Arrays.asList(changes));
                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());
                    }
                }
            }
        }
    }
    Index backingIndex = uc.getBackingIndex();
    //        if (backingIndex == null) {
    //            Index exampleIndex = new Index().setTable(uc.getTable());
    //            for (String col : uc.getColumns()) {
    //                exampleIndex.getColumns().add(col);
    //            }
    //            control.setAlreadyHandledMissing(exampleIndex);
    //        } else {
    control.setAlreadyHandledMissing(backingIndex);
    //        }
    returnList.add(change);
    return returnList.toArray(new Change[returnList.size()]);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) ArrayList(java.util.ArrayList) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) Change(liquibase.change.Change) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Example 2 with AddUniqueConstraintChange

use of liquibase.change.core.AddUniqueConstraintChange in project liquibase by liquibase.

the class ChangedUniqueConstraintChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    List<Change> returnList = new ArrayList<Change>();
    UniqueConstraint uniqueConstraint = (UniqueConstraint) changedObject;
    DropUniqueConstraintChange dropUniqueConstraintChange = createDropUniqueConstraintChange();
    dropUniqueConstraintChange.setTableName(uniqueConstraint.getTable().getName());
    dropUniqueConstraintChange.setConstraintName(uniqueConstraint.getName());
    AddUniqueConstraintChange addUniqueConstraintChange = createAddUniqueConstraintChange();
    addUniqueConstraintChange.setConstraintName(uniqueConstraint.getName());
    addUniqueConstraintChange.setTableName(uniqueConstraint.getTable().getName());
    addUniqueConstraintChange.setColumnNames(uniqueConstraint.getColumnNames());
    returnList.add(dropUniqueConstraintChange);
    if (control.getIncludeCatalog()) {
        dropUniqueConstraintChange.setCatalogName(uniqueConstraint.getSchema().getCatalogName());
        addUniqueConstraintChange.setCatalogName(uniqueConstraint.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        dropUniqueConstraintChange.setSchemaName(uniqueConstraint.getSchema().getName());
        addUniqueConstraintChange.setSchemaName(uniqueConstraint.getSchema().getName());
    }
    Index backingIndex = uniqueConstraint.getBackingIndex();
    if (comparisonDatabase instanceof OracleDatabase) {
        if (backingIndex != null && backingIndex.getName() != null) {
            Change[] missingIndexChanges = ChangeGeneratorFactory.getInstance().fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);
            if (missingIndexChanges != null) {
                returnList.addAll(Arrays.asList(missingIndexChanges));
            }
            addUniqueConstraintChange.setForIndexName(backingIndex.getName());
            Schema schema = backingIndex.getSchema();
            if (schema != null) {
                if (control.getIncludeCatalog()) {
                    addUniqueConstraintChange.setForIndexCatalogName(schema.getCatalogName());
                }
                if (control.getIncludeSchema()) {
                    addUniqueConstraintChange.setForIndexSchemaName(schema.getName());
                }
            }
        }
    }
    control.setAlreadyHandledChanged(backingIndex);
    returnList.add(addUniqueConstraintChange);
    return returnList.toArray(new Change[returnList.size()]);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) DropUniqueConstraintChange(liquibase.change.core.DropUniqueConstraintChange) Schema(liquibase.structure.core.Schema) ArrayList(java.util.ArrayList) UniqueConstraint(liquibase.structure.core.UniqueConstraint) Index(liquibase.structure.core.Index) DropUniqueConstraintChange(liquibase.change.core.DropUniqueConstraintChange) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) Change(liquibase.change.Change)

Aggregations

ArrayList (java.util.ArrayList)2 Change (liquibase.change.Change)2 AddUniqueConstraintChange (liquibase.change.core.AddUniqueConstraintChange)2 OracleDatabase (liquibase.database.core.OracleDatabase)2 DropUniqueConstraintChange (liquibase.change.core.DropUniqueConstraintChange)1 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)1 Index (liquibase.structure.core.Index)1 Schema (liquibase.structure.core.Schema)1 UniqueConstraint (liquibase.structure.core.UniqueConstraint)1