Search in sources :

Example 11 with LiquibaseDataType

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()]);
}
Also used : LiquibaseDataType(liquibase.datatype.LiquibaseDataType) ArrayList(java.util.ArrayList) Change(liquibase.change.Change) Difference(liquibase.diff.Difference)

Example 12 with LiquibaseDataType

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;
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) LiquibaseDataType(liquibase.datatype.LiquibaseDataType) BooleanType(liquibase.datatype.core.BooleanType) Index(liquibase.structure.core.Index) Date(java.util.Date) Sql(liquibase.sql.Sql) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) CharType(liquibase.datatype.core.CharType) DateType(liquibase.datatype.core.DateType)

Aggregations

LiquibaseDataType (liquibase.datatype.LiquibaseDataType)12 DatabaseFunction (liquibase.statement.DatabaseFunction)5 PostgresDatabase (liquibase.database.core.PostgresDatabase)4 BooleanType (liquibase.datatype.core.BooleanType)4 ArrayList (java.util.ArrayList)3 ColumnConfig (liquibase.change.ColumnConfig)3 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)3 MySQLDatabase (liquibase.database.core.MySQLDatabase)3 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)3 Column (liquibase.structure.core.Column)3 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 Date (java.util.Date)2 Change (liquibase.change.Change)2 AbstractDb2Database (liquibase.database.core.AbstractDb2Database)2 HsqlDatabase (liquibase.database.core.HsqlDatabase)2 OracleDatabase (liquibase.database.core.OracleDatabase)2 CharType (liquibase.datatype.core.CharType)2 Difference (liquibase.diff.Difference)2 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)2