Search in sources :

Example 1 with DateType

use of liquibase.datatype.core.DateType 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

Date (java.util.Date)1 ColumnConfig (liquibase.change.ColumnConfig)1 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)1 LiquibaseDataType (liquibase.datatype.LiquibaseDataType)1 BooleanType (liquibase.datatype.core.BooleanType)1 CharType (liquibase.datatype.core.CharType)1 DateType (liquibase.datatype.core.DateType)1 Sql (liquibase.sql.Sql)1 Index (liquibase.structure.core.Index)1