Search in sources :

Example 1 with UniqueConstraint

use of liquibase.structure.core.UniqueConstraint in project liquibase by liquibase.

the class AddUniqueConstraintChange method checkStatus.

@Override
public ChangeStatus checkStatus(Database database) {
    ChangeStatus result = new ChangeStatus();
    try {
        UniqueConstraint example = new UniqueConstraint(getConstraintName(), getCatalogName(), getSchemaName(), getTableName(), Column.arrayFromNames(getColumnNames()));
        UniqueConstraint snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(example, database);
        result.assertComplete(snapshot != null, "Unique constraint does not exist");
        return result;
    } catch (Exception e) {
        return result.unknown(e);
    }
}
Also used : UniqueConstraint(liquibase.structure.core.UniqueConstraint)

Example 2 with UniqueConstraint

use of liquibase.structure.core.UniqueConstraint in project liquibase by liquibase.

the class AddUniqueConstraintGenerator method getAffectedUniqueConstraint.

protected UniqueConstraint getAffectedUniqueConstraint(AddUniqueConstraintStatement statement) {
    UniqueConstraint uniqueConstraint = new UniqueConstraint().setName(statement.getConstraintName()).setTable((Table) new Table().setName(statement.getTableName()).setSchema(statement.getCatalogName(), statement.getSchemaName()));
    int i = 0;
    for (Column column : Column.listFromNames(statement.getColumnNames())) {
        uniqueConstraint.addColumn(i++, column);
    }
    return uniqueConstraint;
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) UniqueConstraint(liquibase.structure.core.UniqueConstraint) UniqueConstraint(liquibase.structure.core.UniqueConstraint)

Example 3 with UniqueConstraint

use of liquibase.structure.core.UniqueConstraint 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)

Example 4 with UniqueConstraint

use of liquibase.structure.core.UniqueConstraint 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 5 with UniqueConstraint

use of liquibase.structure.core.UniqueConstraint in project liquibase by liquibase.

the class UniqueConstraintComparator method isSameObject.

@Override
public boolean isSameObject(DatabaseObject databaseObject1, DatabaseObject databaseObject2, Database accordingTo, DatabaseObjectComparatorChain chain) {
    if (!(databaseObject1 instanceof UniqueConstraint && databaseObject2 instanceof UniqueConstraint)) {
        return false;
    }
    UniqueConstraint thisConstraint = (UniqueConstraint) databaseObject1;
    UniqueConstraint otherConstraint = (UniqueConstraint) databaseObject2;
    int thisConstraintSize = thisConstraint.getColumns().size();
    int otherConstraintSize = otherConstraint.getColumns().size();
    if (thisConstraint.getTable() != null && otherConstraint.getTable() != null) {
        if (!DatabaseObjectComparatorFactory.getInstance().isSameObject(thisConstraint.getTable(), otherConstraint.getTable(), chain.getSchemaComparisons(), accordingTo)) {
            return false;
        }
        if (databaseObject1.getSchema() != null && databaseObject2.getSchema() != null && !DatabaseObjectComparatorFactory.getInstance().isSameObject(databaseObject1.getSchema(), databaseObject2.getSchema(), chain.getSchemaComparisons(), accordingTo)) {
            return false;
        }
        if (databaseObject1.getName() != null && databaseObject2.getName() != null && DefaultDatabaseObjectComparator.nameMatches(databaseObject1, databaseObject2, accordingTo)) {
            return true;
        } else {
            if (thisConstraintSize == 0 || otherConstraintSize == 0) {
                return DefaultDatabaseObjectComparator.nameMatches(databaseObject1, databaseObject2, accordingTo);
            }
            if (thisConstraintSize > 0 && otherConstraintSize > 0 && thisConstraintSize != otherConstraintSize) {
                return false;
            }
            for (int i = 0; i < otherConstraintSize; i++) {
                if (!DatabaseObjectComparatorFactory.getInstance().isSameObject(thisConstraint.getColumns().get(i).setRelation(thisConstraint.getTable()), otherConstraint.getColumns().get(i).setRelation(otherConstraint.getTable()), chain.getSchemaComparisons(), accordingTo)) {
                    return false;
                }
            }
            return true;
        }
    } else {
        if (thisConstraintSize > 0 && otherConstraintSize > 0 && thisConstraintSize != otherConstraintSize) {
            return false;
        }
        if (!DefaultDatabaseObjectComparator.nameMatches(databaseObject1, databaseObject2, accordingTo)) {
            return false;
        }
        if (databaseObject1.getSchema() != null && databaseObject2.getSchema() != null) {
            return DatabaseObjectComparatorFactory.getInstance().isSameObject(databaseObject1.getSchema(), databaseObject2.getSchema(), chain.getSchemaComparisons(), accordingTo);
        } else {
            return true;
        }
    }
}
Also used : UniqueConstraint(liquibase.structure.core.UniqueConstraint) UniqueConstraint(liquibase.structure.core.UniqueConstraint)

Aggregations

UniqueConstraint (liquibase.structure.core.UniqueConstraint)6 ArrayList (java.util.ArrayList)3 Change (liquibase.change.Change)2 Column (liquibase.structure.core.Column)2 Index (liquibase.structure.core.Index)2 Table (liquibase.structure.core.Table)2 List (java.util.List)1 AddColumnConfig (liquibase.change.AddColumnConfig)1 AddUniqueConstraintChange (liquibase.change.core.AddUniqueConstraintChange)1 CreateIndexChange (liquibase.change.core.CreateIndexChange)1 DropIndexChange (liquibase.change.core.DropIndexChange)1 DropUniqueConstraintChange (liquibase.change.core.DropUniqueConstraintChange)1 OracleDatabase (liquibase.database.core.OracleDatabase)1 Difference (liquibase.diff.Difference)1 Schema (liquibase.structure.core.Schema)1 StringUtils (liquibase.util.StringUtils)1