Search in sources :

Example 1 with Difference

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

the class ChangedTableChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, final Database comparisonDatabase, ChangeGeneratorChain chain) {
    Table table = (Table) changedObject;
    Difference changedRemarks = differences.getDifference("remarks");
    if (changedRemarks != null) {
        SetTableRemarksChange change = new SetTableRemarksChange();
        if (control.getIncludeCatalog()) {
            change.setCatalogName(table.getSchema().getCatalogName());
        }
        if (control.getIncludeSchema()) {
            change.setSchemaName(table.getSchema().getName());
        }
        change.setTableName(table.getName());
        change.setRemarks(table.getRemarks());
        return new Change[] { change };
    }
    Difference changedTablespace = differences.getDifference("tablespace");
    if (changedTablespace != null) {
        // TODO: Implement moveTableToDifferentTablespace change type!
        Scope.getCurrentScope().getLog(getClass()).warning("A change of the tablespace was detected, however, Liquibase does not currently generate statements to move a table between tablespaces.");
    }
    return null;
}
Also used : Table(liquibase.structure.core.Table) Difference(liquibase.diff.Difference) SetTableRemarksChange(liquibase.change.core.SetTableRemarksChange) Change(liquibase.change.Change) SetTableRemarksChange(liquibase.change.core.SetTableRemarksChange)

Example 2 with Difference

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

the class ChangedColumnChangeGenerator method handleNullableDifferences.

protected void handleNullableDifferences(Column column, ObjectDifferences differences, DiffOutputControl control, List<Change> changes, Database referenceDatabase, Database comparisonDatabase) {
    Difference nullableDifference = differences.getDifference("nullable");
    if ((nullableDifference != null) && (nullableDifference.getReferenceValue() != null)) {
        boolean nullable = (Boolean) nullableDifference.getReferenceValue();
        if (nullable) {
            DropNotNullConstraintChange change = new DropNotNullConstraintChange();
            if (control.getIncludeCatalog()) {
                change.setCatalogName(column.getRelation().getSchema().getCatalog().getName());
            }
            if (control.getIncludeSchema()) {
                change.setSchemaName(column.getRelation().getSchema().getName());
            }
            change.setTableName(column.getRelation().getName());
            change.setColumnName(column.getName());
            change.setColumnDataType(DataTypeFactory.getInstance().from(column.getType(), comparisonDatabase).toString());
            changes.add(change);
        } else {
            AddNotNullConstraintChange change = new AddNotNullConstraintChange();
            if (control.getIncludeCatalog()) {
                change.setCatalogName(column.getRelation().getSchema().getCatalog().getName());
            }
            if (control.getIncludeSchema()) {
                change.setSchemaName(column.getRelation().getSchema().getName());
            }
            change.setTableName(column.getRelation().getName());
            change.setColumnName(column.getName());
            change.setColumnDataType(DataTypeFactory.getInstance().from(column.getType(), comparisonDatabase).toString());
            change.setValidate(column.getValidate());
            change.setConstraintName(column.getAttribute("notNullConstraintName", String.class));
            changes.add(change);
        }
    }
}
Also used : Difference(liquibase.diff.Difference)

Example 3 with Difference

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

the class ChangedColumnChangeGenerator method handleAutoIncrementDifferences.

protected void handleAutoIncrementDifferences(Column column, ObjectDifferences differences, DiffOutputControl control, List<Change> changes, Database referenceDatabase, Database comparisonDatabase) {
    Difference difference = differences.getDifference("autoIncrementInformation");
    if (difference != null) {
        if (difference.getReferenceValue() == null) {
            Scope.getCurrentScope().getLog(getClass()).info("ChangedColumnChangeGenerator cannot fix dropped auto increment values");
        // todo: Support dropping auto increments
        } else {
            AddAutoIncrementChange change = new AddAutoIncrementChange();
            if (control.getIncludeCatalog()) {
                change.setCatalogName(column.getRelation().getSchema().getCatalog().getName());
            }
            if (control.getIncludeSchema()) {
                change.setSchemaName(column.getRelation().getSchema().getName());
            }
            change.setTableName(column.getRelation().getName());
            change.setColumnName(column.getName());
            change.setColumnDataType(DataTypeFactory.getInstance().from(column.getType(), comparisonDatabase).toString());
            changes.add(change);
        }
    }
}
Also used : Difference(liquibase.diff.Difference)

Example 4 with Difference

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

the class ChangedColumnChangeGenerator method handleDefaultValueDifferences.

protected void handleDefaultValueDifferences(Column column, ObjectDifferences differences, DiffOutputControl control, List<Change> changes, Database referenceDatabase, Database comparisonDatabase) {
    Difference difference = differences.getDifference("defaultValue");
    if (difference != null) {
        Object value = difference.getReferenceValue();
        LiquibaseDataType columnDataType = DataTypeFactory.getInstance().from(column.getType(), comparisonDatabase);
        if (value == null) {
            DropDefaultValueChange change = new DropDefaultValueChange();
            if (control.getIncludeCatalog()) {
                change.setCatalogName(column.getRelation().getSchema().getCatalog().getName());
            }
            if (control.getIncludeSchema()) {
                change.setSchemaName(column.getRelation().getSchema().getName());
            }
            change.setTableName(column.getRelation().getName());
            change.setColumnName(column.getName());
            change.setColumnDataType(columnDataType.toString());
            changes.add(change);
        } else if (shouldTriggerAddDefaultChange(column, difference, comparisonDatabase)) {
            AddDefaultValueChange change = new AddDefaultValueChange();
            if (control.getIncludeCatalog()) {
                change.setCatalogName(column.getRelation().getSchema().getCatalog().getName());
            }
            if (control.getIncludeSchema()) {
                change.setSchemaName(column.getRelation().getSchema().getName());
            }
            change.setTableName(column.getRelation().getName());
            change.setColumnName(column.getName());
            change.setColumnDataType(columnDataType.toString());
            // 
            if (value instanceof Boolean || columnDataType instanceof BooleanType) {
                if (value instanceof Boolean) {
                    change.setDefaultValueBoolean((Boolean) value);
                } else if (columnDataType instanceof BooleanType) {
                    if (value instanceof DatabaseFunction) {
                        if (value.equals(new DatabaseFunction("'false'"))) {
                            change.setDefaultValueBoolean(false);
                        } else if (value.equals(new DatabaseFunction("'true'"))) {
                            change.setDefaultValueBoolean(true);
                        } else {
                            change.setDefaultValueComputed(((DatabaseFunction) value));
                        }
                    }
                }
            } else if (value instanceof Date) {
                change.setDefaultValueDate(new ISODateFormat().format(((Date) value)));
            } else if (value instanceof Number) {
                change.setDefaultValueNumeric(value.toString());
            } else if (value instanceof DatabaseFunction) {
                change.setDefaultValueComputed(((DatabaseFunction) value));
            } else {
                change.setDefaultValue(value.toString());
            }
            change.setDefaultValueConstraintName(column.getDefaultValueConstraintName());
            changes.add(change);
        }
    }
}
Also used : ISODateFormat(liquibase.util.ISODateFormat) DatabaseFunction(liquibase.statement.DatabaseFunction) LiquibaseDataType(liquibase.datatype.LiquibaseDataType) BooleanType(liquibase.datatype.core.BooleanType) DatabaseObject(liquibase.structure.DatabaseObject) Difference(liquibase.diff.Difference) Date(java.util.Date)

Example 5 with Difference

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

the class ChangedViewChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, final Database comparisonDatabase, ChangeGeneratorChain chain) {
    View view = (View) changedObject;
    CreateViewChange change = createViewChange();
    change.setViewName(view.getName());
    change.setReplaceIfExists(true);
    if (control.getIncludeCatalog()) {
        change.setCatalogName(view.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(view.getSchema().getName());
    }
    String selectQuery = view.getDefinition();
    boolean fullDefinitionOverridden = false;
    if (selectQuery == null) {
        selectQuery = "COULD NOT DETERMINE VIEW QUERY";
    } else if ((comparisonDatabase instanceof OracleDatabase) && (view.getColumns() != null) && !view.getColumns().isEmpty()) {
        String viewName;
        if ((change.getCatalogName() == null) && (change.getSchemaName() == null)) {
            viewName = comparisonDatabase.escapeObjectName(change.getViewName(), View.class);
        } else {
            viewName = comparisonDatabase.escapeViewName(change.getCatalogName(), change.getSchemaName(), change.getViewName());
        }
        selectQuery = "CREATE OR REPLACE FORCE VIEW " + viewName + " (" + StringUtil.join(view.getColumns(), ", ", new StringUtil.StringUtilFormatter() {

            @Override
            public String toString(Object obj) {
                if ((((Column) obj).getComputed() != null) && ((Column) obj).getComputed()) {
                    return ((Column) obj).getName();
                } else {
                    return comparisonDatabase.escapeColumnName(null, null, null, ((Column) obj).getName(), false);
                }
            }
        }) + ") AS " + selectQuery;
        change.setFullDefinition(true);
        fullDefinitionOverridden = true;
    }
    change.setSelectQuery(selectQuery);
    if (!fullDefinitionOverridden) {
        change.setFullDefinition(view.getContainsFullDefinition());
    }
    List<Change> changes = new ArrayList<>();
    changes.add(change);
    Difference changedRemarks = differences.getDifference("remarks");
    if (changedRemarks != null) {
        SetTableRemarksChange setRemarksChange = new SetTableRemarksChange();
        if (control.getIncludeCatalog()) {
            setRemarksChange.setCatalogName(view.getSchema().getCatalogName());
        }
        if (control.getIncludeSchema()) {
            setRemarksChange.setSchemaName(view.getSchema().getName());
        }
        setRemarksChange.setTableName(view.getName());
        setRemarksChange.setRemarks(view.getRemarks());
        changes.add(setRemarksChange);
    }
    return changes.toArray(new Change[changes.size()]);
}
Also used : CreateViewChange(liquibase.change.core.CreateViewChange) ArrayList(java.util.ArrayList) Change(liquibase.change.Change) CreateViewChange(liquibase.change.core.CreateViewChange) SetTableRemarksChange(liquibase.change.core.SetTableRemarksChange) Difference(liquibase.diff.Difference) View(liquibase.structure.core.View) OracleDatabase(liquibase.database.core.OracleDatabase) Column(liquibase.structure.core.Column) DatabaseObject(liquibase.structure.DatabaseObject) SetTableRemarksChange(liquibase.change.core.SetTableRemarksChange)

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