Search in sources :

Example 1 with AddPrimaryKeyChange

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

the class MissingPrimaryKeyChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    List<Change> returnList = new ArrayList<Change>();
    PrimaryKey pk = (PrimaryKey) missingObject;
    AddPrimaryKeyChange change = createAddPrimaryKeyChange();
    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());
    change.setColumnNames(pk.getColumnNames());
    if (control.getIncludeTablespace()) {
        change.setTablespace(pk.getTablespace());
    }
    if (referenceDatabase instanceof MSSQLDatabase && pk.getBackingIndex() != null && pk.getBackingIndex().getClustered() != null && !pk.getBackingIndex().getClustered()) {
        change.setClustered(false);
    }
    if (referenceDatabase instanceof PostgresDatabase && pk.getBackingIndex() != null && pk.getBackingIndex().getClustered() != null && pk.getBackingIndex().getClustered()) {
        change.setClustered(true);
    }
    if (comparisonDatabase instanceof OracleDatabase || (comparisonDatabase instanceof DB2Database && pk.getBackingIndex() != null && !comparisonDatabase.isSystemObject(pk.getBackingIndex()))) {
        Index backingIndex = pk.getBackingIndex();
        if (backingIndex != null && backingIndex.getName() != null) {
            try {
                if (!control.getIncludeCatalog() && !control.getIncludeSchema()) {
                    CatalogAndSchema schema = comparisonDatabase.getDefaultSchema().customize(comparisonDatabase);
                    //set table schema so it is found in the correct schema
                    backingIndex.getTable().setSchema(schema.getCatalogName(), schema.getSchemaName());
                }
                if (referenceDatabase.equals(comparisonDatabase) || !SnapshotGeneratorFactory.getInstance().has(backingIndex, comparisonDatabase)) {
                    Change[] fixes = ChangeGeneratorFactory.getInstance().fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);
                    if (fixes != null) {
                        for (Change fix : fixes) {
                            if (fix != null) {
                                returnList.add(fix);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                throw new UnexpectedLiquibaseException(e);
            }
            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());
                }
            }
        }
    }
    control.setAlreadyHandledMissing(pk.getBackingIndex());
    returnList.add(change);
    return returnList.toArray(new Change[returnList.size()]);
}
Also used : DB2Database(liquibase.database.core.DB2Database) CatalogAndSchema(liquibase.CatalogAndSchema) ArrayList(java.util.ArrayList) Change(liquibase.change.Change) CreateIndexChange(liquibase.change.core.CreateIndexChange) AddPrimaryKeyChange(liquibase.change.core.AddPrimaryKeyChange) CatalogAndSchema(liquibase.CatalogAndSchema) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseException(liquibase.exception.DatabaseException) OracleDatabase(liquibase.database.core.OracleDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) AddPrimaryKeyChange(liquibase.change.core.AddPrimaryKeyChange) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 2 with AddPrimaryKeyChange

use of liquibase.change.core.AddPrimaryKeyChange 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<Change>();
    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();
    }
    StringUtils.ToStringFormatter formatter = new StringUtils.ToStringFormatter();
    control.setAlreadyHandledChanged(new Index().setTable(pk.getTable()).setColumns(referenceColumns));
    if (!StringUtils.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtils.join(comparedColumns, ",", formatter))) {
        control.setAlreadyHandledChanged(new Index().setTable(pk.getTable()).setColumns(comparedColumns));
    }
    control.setAlreadyHandledChanged(new UniqueConstraint().setTable(pk.getTable()).setColumns(referenceColumns));
    if (!StringUtils.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtils.join(comparedColumns, ",", formatter))) {
        control.setAlreadyHandledChanged(new UniqueConstraint().setTable(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) StringUtils(liquibase.util.StringUtils) AddPrimaryKeyChange(liquibase.change.core.AddPrimaryKeyChange)

Aggregations

ArrayList (java.util.ArrayList)2 Change (liquibase.change.Change)2 AddPrimaryKeyChange (liquibase.change.core.AddPrimaryKeyChange)2 OracleDatabase (liquibase.database.core.OracleDatabase)2 CatalogAndSchema (liquibase.CatalogAndSchema)1 CreateIndexChange (liquibase.change.core.CreateIndexChange)1 DropPrimaryKeyChange (liquibase.change.core.DropPrimaryKeyChange)1 DB2Database (liquibase.database.core.DB2Database)1 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)1 PostgresDatabase (liquibase.database.core.PostgresDatabase)1 Difference (liquibase.diff.Difference)1 DatabaseException (liquibase.exception.DatabaseException)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 InvalidExampleException (liquibase.snapshot.InvalidExampleException)1 StringUtils (liquibase.util.StringUtils)1