Search in sources :

Example 6 with Difference

use of liquibase.diff.Difference in project liquibase by liquibase.

the class ChangedColumnChangeGenerator method handleTypeDifferences.

protected void handleTypeDifferences(Column column, ObjectDifferences differences, DiffOutputControl control, List<Change> changes, Database referenceDatabase, Database comparisonDatabase) {
    Difference typeDifference = differences.getDifference("type");
    if (typeDifference != null) {
        String catalogName = null;
        String schemaName = null;
        if (control.getIncludeCatalog()) {
            catalogName = column.getRelation().getSchema().getCatalog().getName();
        }
        if (control.getIncludeSchema()) {
            schemaName = column.getRelation().getSchema().getName();
        }
        String tableName = column.getRelation().getName();
        if ((comparisonDatabase instanceof OracleDatabase) && ("clob".equalsIgnoreCase(((DataType) typeDifference.getReferenceValue()).getTypeName()) || "clob".equalsIgnoreCase(((DataType) typeDifference.getComparedValue()).getTypeName()))) {
            String tempColName = "TEMP_CLOB_CONVERT";
            OutputChange outputChange = new OutputChange();
            outputChange.setMessage("Cannot convert directly from " + ((DataType) typeDifference.getComparedValue()).getTypeName() + " to " + ((DataType) typeDifference.getReferenceValue()).getTypeName() + ". Instead a new column will be created and the data transferred. This may cause unexpected side effects including constraint issues and/or table locks.");
            changes.add(outputChange);
            AddColumnChange addColumn = new AddColumnChange();
            addColumn.setCatalogName(catalogName);
            addColumn.setSchemaName(schemaName);
            addColumn.setTableName(tableName);
            AddColumnConfig addColumnConfig = new AddColumnConfig(column);
            addColumnConfig.setName(tempColName);
            addColumnConfig.setType(typeDifference.getReferenceValue().toString());
            addColumnConfig.setAfterColumn(column.getName());
            addColumn.setColumns(Arrays.asList(addColumnConfig));
            changes.add(addColumn);
            changes.add(new RawSQLChange("UPDATE " + referenceDatabase.escapeObjectName(tableName, Table.class) + " SET " + tempColName + "=" + referenceDatabase.escapeObjectName(column.getName(), Column.class)));
            DropColumnChange dropColumnChange = new DropColumnChange();
            dropColumnChange.setCatalogName(catalogName);
            dropColumnChange.setSchemaName(schemaName);
            dropColumnChange.setTableName(tableName);
            dropColumnChange.setColumnName(column.getName());
            changes.add(dropColumnChange);
            RenameColumnChange renameColumnChange = new RenameColumnChange();
            renameColumnChange.setCatalogName(catalogName);
            renameColumnChange.setSchemaName(schemaName);
            renameColumnChange.setTableName(tableName);
            renameColumnChange.setOldColumnName(tempColName);
            renameColumnChange.setNewColumnName(column.getName());
            changes.add(renameColumnChange);
        } else {
            if ((comparisonDatabase instanceof MSSQLDatabase) && (column.getDefaultValue() != null)) {
                // have to drop the default value, will be added back with the "data type changed" logic.
                DropDefaultValueChange dropDefaultValueChange = new DropDefaultValueChange();
                dropDefaultValueChange.setCatalogName(catalogName);
                dropDefaultValueChange.setSchemaName(schemaName);
                dropDefaultValueChange.setTableName(tableName);
                dropDefaultValueChange.setColumnName(column.getName());
                changes.add(dropDefaultValueChange);
            }
            ModifyDataTypeChange change = new ModifyDataTypeChange();
            change.setCatalogName(catalogName);
            change.setSchemaName(schemaName);
            change.setTableName(tableName);
            change.setColumnName(column.getName());
            DataType referenceType = (DataType) typeDifference.getReferenceValue();
            change.setNewDataType(DataTypeFactory.getInstance().from(referenceType, comparisonDatabase).toString());
            changes.add(change);
        }
    }
}
Also used : Difference(liquibase.diff.Difference) OracleDatabase(liquibase.database.core.OracleDatabase) LiquibaseDataType(liquibase.datatype.LiquibaseDataType) AddColumnConfig(liquibase.change.AddColumnConfig) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Example 7 with Difference

use of liquibase.diff.Difference 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.getRelation() != null && index.getRelation() instanceof Table) {
        if ((((Table) index.getRelation()).getPrimaryKey() != null) && DatabaseObjectComparatorFactory.getInstance().isSameObject(((Table) index.getRelation()).getPrimaryKey().getBackingIndex(), changedObject, differences.getSchemaComparisons(), comparisonDatabase)) {
            return ChangeGeneratorFactory.getInstance().fixChanged(((Table) index.getRelation()).getPrimaryKey(), differences, control, referenceDatabase, comparisonDatabase);
        }
        List<UniqueConstraint> uniqueConstraints = ((Table) index.getRelation()).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.getRelation().getName());
    dropIndexChange.setIndexName(index.getName());
    CreateIndexChange addIndexChange = createCreateIndexChange();
    addIndexChange.setTableName(index.getRelation().getName());
    List<AddColumnConfig> columns = new ArrayList<>();
    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();
        StringUtil.StringUtilFormatter<Column> formatter = new StringUtil.StringUtilFormatter<Column>() {

            @Override
            public String toString(Column obj) {
                return obj.toString(false);
            }
        };
        control.setAlreadyHandledChanged(new Index().setRelation(index.getRelation()).setColumns(referenceColumns));
        if (!StringUtil.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtil.join(comparedColumns, ",", formatter))) {
            control.setAlreadyHandledChanged(new Index().setRelation(index.getRelation()).setColumns(comparedColumns));
        }
        if ((index.isUnique() != null) && index.isUnique()) {
            control.setAlreadyHandledChanged(new UniqueConstraint().setRelation(index.getRelation()).setColumns(referenceColumns));
            if (!StringUtil.join(referenceColumns, ",", formatter).equalsIgnoreCase(StringUtil.join(comparedColumns, ",", formatter))) {
                control.setAlreadyHandledChanged(new UniqueConstraint().setRelation(index.getRelation()).setColumns(comparedColumns));
            }
        }
    }
    return new Change[] { dropIndexChange, addIndexChange };
}
Also used : DropIndexChange(liquibase.change.core.DropIndexChange) Table(liquibase.structure.core.Table) 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) AddColumnConfig(liquibase.change.AddColumnConfig) ArrayList(java.util.ArrayList) List(java.util.List) StringUtil(liquibase.util.StringUtil) CreateIndexChange(liquibase.change.core.CreateIndexChange)

Example 8 with Difference

use of liquibase.diff.Difference in project liquibase by liquibase.

the class ChangedColumnChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    Column column = (Column) changedObject;
    if (column.getRelation() instanceof View) {
        return null;
    }
    if (column.getRelation().getSnapshotId() == null) {
        // not an actual table, maybe an alias, maybe in a different schema. Don't fix it.
        return null;
    }
    List<Change> changes = new ArrayList<>();
    handleTypeDifferences(column, differences, control, changes, referenceDatabase, comparisonDatabase);
    handleNullableDifferences(column, differences, control, changes, referenceDatabase, comparisonDatabase);
    handleDefaultValueDifferences(column, differences, control, changes, referenceDatabase, comparisonDatabase);
    handleAutoIncrementDifferences(column, differences, control, changes, referenceDatabase, comparisonDatabase);
    Difference remarksDiff = differences.getDifference("remarks");
    if (remarksDiff != null) {
        SetColumnRemarksChange change = new SetColumnRemarksChange();
        if (control.getIncludeCatalog()) {
            change.setCatalogName(column.getSchema().getCatalogName());
        }
        if (control.getIncludeSchema()) {
            change.setSchemaName(column.getSchema().getName());
        }
        change.setTableName(column.getRelation().getName());
        change.setColumnName(column.getName());
        change.setRemarks(column.getRemarks());
        LiquibaseDataType columnDataType = DataTypeFactory.getInstance().from(column.getType(), comparisonDatabase);
        if (columnDataType != null) {
            change.setColumnDataType(columnDataType.toString());
        }
        changes.add(change);
    }
    return changes.toArray(new Change[changes.size()]);
}
Also used : LiquibaseDataType(liquibase.datatype.LiquibaseDataType) ArrayList(java.util.ArrayList) Change(liquibase.change.Change) Difference(liquibase.diff.Difference)

Example 9 with Difference

use of liquibase.diff.Difference 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

Difference (liquibase.diff.Difference)9 Change (liquibase.change.Change)5 ArrayList (java.util.ArrayList)4 OracleDatabase (liquibase.database.core.OracleDatabase)3 LiquibaseDataType (liquibase.datatype.LiquibaseDataType)3 AddColumnConfig (liquibase.change.AddColumnConfig)2 SetTableRemarksChange (liquibase.change.core.SetTableRemarksChange)2 DatabaseObject (liquibase.structure.DatabaseObject)2 Column (liquibase.structure.core.Column)2 Table (liquibase.structure.core.Table)2 StringUtil (liquibase.util.StringUtil)2 Date (java.util.Date)1 List (java.util.List)1 AddPrimaryKeyChange (liquibase.change.core.AddPrimaryKeyChange)1 CreateIndexChange (liquibase.change.core.CreateIndexChange)1 CreateViewChange (liquibase.change.core.CreateViewChange)1 DropIndexChange (liquibase.change.core.DropIndexChange)1 DropPrimaryKeyChange (liquibase.change.core.DropPrimaryKeyChange)1 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)1 BooleanType (liquibase.datatype.core.BooleanType)1