use of liquibase.change.core.AddColumnChange in project liquibase by liquibase.
the class AddColumnChangeTest method generateStatements_DB2_multipleColumns_single_reorg.
@Test
public void generateStatements_DB2_multipleColumns_single_reorg() {
AddColumnChange change = new AddColumnChange();
AddColumnConfig column1 = new AddColumnConfig();
column1.setName("column1");
column1.setType("INT");
change.addColumn(column1);
AddColumnConfig column2 = new AddColumnConfig();
column2.setName("column2");
column2.setType("INT");
change.addColumn(column2);
SqlStatement[] statements = change.generateStatements(new DB2Database());
Assert.assertEquals(2, statements.length);
Assert.assertTrue(statements[0] instanceof AddColumnStatement);
AddColumnStatement stmt = (AddColumnStatement) statements[0];
Assert.assertTrue(stmt.isMultiple());
Assert.assertEquals(2, stmt.getColumns().size());
Assert.assertTrue(statements[1] instanceof ReorganizeTableStatement);
}
use of liquibase.change.core.AddColumnChange in project liquibase by liquibase.
the class AddColumnChangeTest method generateStatements_multipleColumns.
@Test
public void generateStatements_multipleColumns() {
AddColumnChange change = new AddColumnChange();
AddColumnConfig column1 = new AddColumnConfig();
column1.setName("column1");
column1.setType("INT");
change.addColumn(column1);
AddColumnConfig column2 = new AddColumnConfig();
column2.setName("column2");
column2.setType("INT");
change.addColumn(column2);
SqlStatement[] statements = change.generateStatements(new MockDatabase());
Assert.assertEquals(1, statements.length);
Assert.assertTrue(statements[0] instanceof AddColumnStatement);
AddColumnStatement stmt = (AddColumnStatement) statements[0];
Assert.assertTrue(stmt.isMultiple());
Assert.assertEquals(2, stmt.getColumns().size());
}
use of liquibase.change.core.AddColumnChange in project liquibase by liquibase.
the class MissingColumnChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
Column column = (Column) missingObject;
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;
}
AddColumnChange change = createAddColumnChange();
change.setTableName(column.getRelation().getName());
if (control.getIncludeCatalog()) {
change.setCatalogName(column.getRelation().getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(column.getRelation().getSchema().getName());
}
AddColumnConfig columnConfig = createAddColumnConfig();
columnConfig.setName(column.getName());
String dataType = column.getType().toString();
columnConfig.setType(dataType);
MissingTableChangeGenerator.setDefaultValue(columnConfig, column, comparisonDatabase);
Column.AutoIncrementInformation autoIncrementInfo = column.getAutoIncrementInformation();
if (autoIncrementInfo != null) {
columnConfig.setAutoIncrement(true);
columnConfig.setGenerationType(autoIncrementInfo.getGenerationType());
columnConfig.setDefaultOnNull(autoIncrementInfo.getDefaultOnNull());
}
if (column.getRemarks() != null) {
columnConfig.setRemarks(column.getRemarks());
}
ConstraintsConfig constraintsConfig = columnConfig.getConstraints();
if ((column.isNullable() != null) && !column.isNullable()) {
if (constraintsConfig == null) {
constraintsConfig = new ConstraintsConfig();
}
constraintsConfig.setNullable(false);
constraintsConfig.setNotNullConstraintName(column.getAttribute("notNullConstraintName", String.class));
if (!column.getValidateNullable()) {
constraintsConfig.setValidateNullable(false);
}
}
if (constraintsConfig != null) {
columnConfig.setConstraints(constraintsConfig);
}
change.addColumn(columnConfig);
return new Change[] { change };
}
use of liquibase.change.core.AddColumnChange in project liquibase by liquibase.
the class AddColumnGeneratorTest method testAddColumnWithNotNullConstraintAndValue.
@Test
public void testAddColumnWithNotNullConstraintAndValue() {
AddColumnChange change = new AddColumnChange();
change.setTableName(TABLE_NAME);
AddColumnConfig column = new AddColumnConfig();
column.setName("column1");
column.setType("int8");
column.setValueNumeric("0");
column.setConstraints(new ConstraintsConfig().setNullable(false));
change.addColumn(column);
AddColumnConfig column2 = new AddColumnConfig();
column2.setName("column2");
column2.setType("boolean");
column2.setValueBoolean("true");
column2.setConstraints(new ConstraintsConfig().setNullable(false));
change.addColumn(column2);
SqlStatement[] statements = change.generateStatements(new MySQLDatabase());
SqlGeneratorFactory instance = SqlGeneratorFactory.getInstance();
Sql[] sql = instance.generateSql(statements, new MySQLDatabase());
assertEquals(5, sql.length);
assertEquals("ALTER TABLE table_name ADD column1 BIGINT NULL, ADD column2 BIT(1) NULL", sql[0].toSql());
assertEquals("UPDATE table_name SET column1 = 0", sql[1].toSql());
assertEquals("UPDATE table_name SET column2 = 1", sql[2].toSql());
assertEquals("ALTER TABLE table_name MODIFY column1 BIGINT NOT NULL", sql[3].toSql());
assertEquals("ALTER TABLE table_name MODIFY column2 BIT(1) NOT NULL", sql[4].toSql());
}
Aggregations