use of liquibase.datatype.LiquibaseDataType 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.datatype.LiquibaseDataType in project liquibase by liquibase.
the class AddDefaultValueGeneratorSQLite method generateSql.
@Override
public Sql[] generateSql(final AddDefaultValueStatement statement, final Database database, SqlGeneratorChain sqlGeneratorChain) {
// Workaround implemented by replacing a table with a new one (duplicate)
// with default value set on the specified column
final SQLiteDatabase.AlterTableVisitor alterTableVisitor = new SQLiteDatabase.AlterTableVisitor() {
@Override
public ColumnConfig[] getColumnsToAdd() {
return new ColumnConfig[0];
}
@Override
public boolean copyThisColumn(ColumnConfig column) {
return true;
}
@Override
public boolean createThisColumn(ColumnConfig column) {
// update the column to set Default value while copying
if (column.getName().equals(statement.getColumnName())) {
if (statement.getDefaultValueConstraintName() != null) {
column.setDefaultValueConstraintName(statement.getDefaultValueConstraintName());
}
// could be of string, numeric, boolean and date
Object defaultValue = statement.getDefaultValue();
LiquibaseDataType dataType = DataTypeFactory.getInstance().fromDescription(statement.getColumnDataType(), database);
if (dataType instanceof BooleanType) {
// validation is done before hand so the defaultValue is instance of dataType
if (defaultValue instanceof Boolean) {
column.setDefaultValueBoolean((Boolean) defaultValue);
} else {
column.setDefaultValueBoolean(defaultValue.toString());
}
} else if (dataType instanceof CharType) {
column.setDefaultValue(defaultValue.toString());
} else if (dataType instanceof DateType) {
if (defaultValue instanceof Date) {
column.setDefaultValueDate((Date) defaultValue);
} else {
column.setDefaultValueDate(defaultValue.toString());
}
} else {
// fallback??
column.setDefaultValue(defaultValue.toString());
}
}
return true;
}
@Override
public boolean createThisIndex(Index index) {
return true;
}
};
Sql[] generatedSqls = SQLiteDatabase.getAlterTableSqls(database, alterTableVisitor, statement.getCatalogName(), statement.getSchemaName(), statement.getTableName());
return generatedSqls;
}
Aggregations