Search in sources :

Example 1 with DropPrimaryKeyChange

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

the class UnexpectedPrimaryKeyChangeGenerator method fixUnexpected.

@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    // if (!diffResult.getObjectDiff(Table.class).getUnexpected().contains(pk.getTable())) {
    PrimaryKey pk = (PrimaryKey) unexpectedObject;
    DropPrimaryKeyChange change = new DropPrimaryKeyChange();
    change.setTableName(pk.getTable().getName());
    if (control.getIncludeCatalog()) {
        change.setCatalogName(pk.getTable().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(pk.getTable().getSchema().getName());
    }
    change.setConstraintName(pk.getName());
    Index backingIndex = pk.getBackingIndex();
    control.setAlreadyHandledUnexpected(backingIndex);
    return new Change[] { change };
// }
}
Also used : DropPrimaryKeyChange(liquibase.change.core.DropPrimaryKeyChange) PrimaryKey(liquibase.structure.core.PrimaryKey) Index(liquibase.structure.core.Index) DropPrimaryKeyChange(liquibase.change.core.DropPrimaryKeyChange) Change(liquibase.change.Change)

Example 2 with DropPrimaryKeyChange

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

the class ChangedPrimaryKeyChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    // don't try to recreate PKs that differ in just clustered
    Difference clusteredDiff = differences.getDifference("clustered");
    if (clusteredDiff != null) {
        if ((clusteredDiff.getReferenceValue() == null) || (clusteredDiff.getComparedValue() == null)) {
            differences.removeDifference("clustered");
        }
    }
    if (!differences.hasDifferences()) {
        return new Change[0];
    }
    PrimaryKey pk = (PrimaryKey) changedObject;
    List<Change> returnList = new ArrayList<>();
    DropPrimaryKeyChange dropPkChange = new DropPrimaryKeyChange();
    dropPkChange.setTableName(pk.getTable().getName());
    returnList.add(dropPkChange);
    AddPrimaryKeyChange addPkChange = new AddPrimaryKeyChange();
    addPkChange.setTableName(pk.getTable().getName());
    addPkChange.setColumnNames(pk.getColumnNames());
    addPkChange.setConstraintName(pk.getName());
    if (comparisonDatabase instanceof OracleDatabase) {
        Index backingIndex = pk.getBackingIndex();
        if ((backingIndex != null) && (backingIndex.getName() != null)) {
            Change[] indexChanges = ChangeGeneratorFactory.getInstance().fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);
            if (indexChanges != null) {
                returnList.addAll(Arrays.asList(indexChanges));
            }
            addPkChange.setForIndexName(backingIndex.getName());
            Schema schema = backingIndex.getSchema();
            if (schema != null) {
                if (control.getIncludeCatalog()) {
                    addPkChange.setForIndexCatalogName(schema.getCatalogName());
                }
                if (control.getIncludeSchema()) {
                    addPkChange.setForIndexSchemaName(schema.getName());
                }
            }
        }
    }
    returnList.add(addPkChange);
    if (control.getIncludeCatalog()) {
        dropPkChange.setCatalogName(pk.getSchema().getCatalogName());
        addPkChange.setCatalogName(pk.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        dropPkChange.setSchemaName(pk.getSchema().getName());
        addPkChange.setSchemaName(pk.getSchema().getName());
    }
    Difference columnDifferences = differences.getDifference("columns");
    List<Column> referenceColumns;
    List<Column> comparedColumns;
    if (columnDifferences == null) {
        referenceColumns = pk.getColumns();
        comparedColumns = pk.getColumns();
    } else {
        referenceColumns = (List<Column>) columnDifferences.getReferenceValue();
        comparedColumns = (List<Column>) columnDifferences.getComparedValue();
    }
    StringUtil.ToStringFormatter formatter = new StringUtil.ToStringFormatter();
    control.setAlreadyHandledChanged(new Index().setRelation(pk.getTable()).setColumns(referenceColumns));
    if (!StringUtil.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtil.join(comparedColumns, ",", formatter))) {
        control.setAlreadyHandledChanged(new Index().setRelation(pk.getTable()).setColumns(comparedColumns));
    }
    control.setAlreadyHandledChanged(new UniqueConstraint().setRelation(pk.getTable()).setColumns(referenceColumns));
    if (!StringUtil.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtil.join(comparedColumns, ",", formatter))) {
        control.setAlreadyHandledChanged(new UniqueConstraint().setRelation(pk.getTable()).setColumns(comparedColumns));
    }
    return returnList.toArray(new Change[returnList.size()]);
}
Also used : ArrayList(java.util.ArrayList) Difference(liquibase.diff.Difference) DropPrimaryKeyChange(liquibase.change.core.DropPrimaryKeyChange) Change(liquibase.change.Change) AddPrimaryKeyChange(liquibase.change.core.AddPrimaryKeyChange) OracleDatabase(liquibase.database.core.OracleDatabase) DropPrimaryKeyChange(liquibase.change.core.DropPrimaryKeyChange) AddPrimaryKeyChange(liquibase.change.core.AddPrimaryKeyChange) StringUtil(liquibase.util.StringUtil)

Aggregations

Change (liquibase.change.Change)2 DropPrimaryKeyChange (liquibase.change.core.DropPrimaryKeyChange)2 ArrayList (java.util.ArrayList)1 AddPrimaryKeyChange (liquibase.change.core.AddPrimaryKeyChange)1 OracleDatabase (liquibase.database.core.OracleDatabase)1 Difference (liquibase.diff.Difference)1 Index (liquibase.structure.core.Index)1 PrimaryKey (liquibase.structure.core.PrimaryKey)1 StringUtil (liquibase.util.StringUtil)1