Search in sources :

Example 1 with BooleanType

use of liquibase.datatype.core.BooleanType in project liquibase by liquibase.

the class AddDefaultValueGenerator method validate.

@Override
public ValidationErrors validate(AddDefaultValueStatement addDefaultValueStatement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    Object defaultValue = addDefaultValueStatement.getDefaultValue();
    ValidationErrors validationErrors = new ValidationErrors();
    validationErrors.checkRequiredField("defaultValue", defaultValue);
    validationErrors.checkRequiredField("columnName", addDefaultValueStatement.getColumnName());
    validationErrors.checkRequiredField("tableName", addDefaultValueStatement.getTableName());
    if (!database.supportsSequences() && defaultValue instanceof SequenceNextValueFunction) {
        validationErrors.addError("Database " + database.getShortName() + " does not support sequences");
    }
    if (database instanceof HsqlDatabase) {
        if (defaultValue instanceof SequenceNextValueFunction) {
            validationErrors.addError("Database " + database.getShortName() + " does not support adding sequence-based default values");
        } else if (defaultValue instanceof DatabaseFunction && !HsqlDatabase.supportsDefaultValueComputed(addDefaultValueStatement.getColumnDataType(), defaultValue.toString())) {
            validationErrors.addError("Database " + database.getShortName() + " does not support adding function-based default values");
        }
    }
    String columnDataType = addDefaultValueStatement.getColumnDataType();
    if (columnDataType != null) {
        LiquibaseDataType dataType = DataTypeFactory.getInstance().fromDescription(columnDataType, database);
        boolean typeMismatch = false;
        if (dataType instanceof BooleanType) {
            if (!(defaultValue instanceof Boolean)) {
                typeMismatch = true;
            }
        } else if (dataType instanceof CharType) {
            if (!(defaultValue instanceof String) && !(defaultValue instanceof DatabaseFunction)) {
                typeMismatch = true;
            }
        }
        if (typeMismatch) {
            validationErrors.addError("Default value of " + defaultValue + " does not match defined type of " + columnDataType);
        }
    }
    return validationErrors;
}
Also used : HsqlDatabase(liquibase.database.core.HsqlDatabase) DatabaseFunction(liquibase.statement.DatabaseFunction) ValidationErrors(liquibase.exception.ValidationErrors) LiquibaseDataType(liquibase.datatype.LiquibaseDataType) BooleanType(liquibase.datatype.core.BooleanType) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) CharType(liquibase.datatype.core.CharType)

Aggregations

HsqlDatabase (liquibase.database.core.HsqlDatabase)1 LiquibaseDataType (liquibase.datatype.LiquibaseDataType)1 BooleanType (liquibase.datatype.core.BooleanType)1 CharType (liquibase.datatype.core.CharType)1 ValidationErrors (liquibase.exception.ValidationErrors)1 DatabaseFunction (liquibase.statement.DatabaseFunction)1 SequenceNextValueFunction (liquibase.statement.SequenceNextValueFunction)1