liquibase.change.core.AddNotNullConstraintChange Maven / Gradle / Ivy
package liquibase.change.core;
import liquibase.change.AbstractChange;
import liquibase.change.Change;
import liquibase.change.ChangeMetaData;
import liquibase.change.ColumnConfig;
import liquibase.database.Database;
import liquibase.database.core.DB2Database;
import liquibase.database.core.SQLiteDatabase;
import liquibase.database.core.SQLiteDatabase.AlterTableVisitor;
import liquibase.database.structure.Index;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.ReorganizeTableStatement;
import liquibase.statement.core.SetNullableStatement;
import liquibase.statement.core.UpdateStatement;
import liquibase.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Adds a not-null constraint to an existing column.
*/
public class AddNotNullConstraintChange extends AbstractChange {
private String schemaName;
private String tableName;
private String columnName;
private String defaultNullValue;
private String columnDataType;
public AddNotNullConstraintChange() {
super("addNotNullConstraint", "Add Not-Null Constraint", ChangeMetaData.PRIORITY_DEFAULT);
}
public String getSchemaName() {
return schemaName;
}
public void setSchemaName(String schemaName) {
this.schemaName = StringUtils.trimToNull(schemaName);
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getDefaultNullValue() {
return defaultNullValue;
}
public void setDefaultNullValue(String defaultNullValue) {
this.defaultNullValue = defaultNullValue;
}
public String getColumnDataType() {
return columnDataType;
}
public void setColumnDataType(String columnDataType) {
this.columnDataType = columnDataType;
}
public SqlStatement[] generateStatements(Database database) {
//// if (database instanceof SQLiteDatabase) {
// // return special statements for SQLite databases
// return generateStatementsForSQLiteDatabase(database);
// }
List statements = new ArrayList();
String schemaName = getSchemaName() == null?database.getDefaultSchemaName():getSchemaName();
if (defaultNullValue != null) {
statements.add(new UpdateStatement(schemaName, getTableName())
.addNewColumnValue(getColumnName(), getDefaultNullValue())
.setWhereClause(getColumnName() + " IS NULL"));
}
statements.add(new SetNullableStatement(schemaName, getTableName(), getColumnName(), getColumnDataType(), false));
if (database instanceof DB2Database) {
statements.add(new ReorganizeTableStatement(schemaName, getTableName()));
}
return statements.toArray(new SqlStatement[statements.size()]);
}
private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) {
// SQLite does not support this ALTER TABLE operation until now.
// For more information see: http://www.sqlite.org/omitted.html.
// This is a small work around...
List statements = new ArrayList();
String schemaName = getSchemaName() == null?database.getDefaultSchemaName():getSchemaName();
if (defaultNullValue != null) {
statements.add(new UpdateStatement(schemaName, getTableName())
.addNewColumnValue(getColumnName(), getDefaultNullValue())
.setWhereClause(getColumnName() + " IS NULL"));
}
// // ... test if column contains NULL values
// if (defaultNullValue == null) {
// List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy