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);
}
}
}
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 };
}
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()]);
}
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()]);
}
Aggregations