use of liquibase.statement.core.AddColumnStatement in project liquibase by liquibase.
the class AddColumnChange method generateStatements.
@Override
public SqlStatement[] generateStatements(Database database) {
List<SqlStatement> sql = new ArrayList<SqlStatement>();
List<AddColumnStatement> addColumnStatements = new ArrayList<AddColumnStatement>();
List<UpdateStatement> addColumnUpdateStatements = new ArrayList<UpdateStatement>();
if (getColumns().size() == 0) {
return new SqlStatement[] { new AddColumnStatement(catalogName, schemaName, tableName, null, null, null) };
}
for (AddColumnConfig column : getColumns()) {
Set<ColumnConstraint> constraints = new HashSet<ColumnConstraint>();
ConstraintsConfig constraintsConfig = column.getConstraints();
if (constraintsConfig != null) {
if (constraintsConfig.isNullable() != null && !constraintsConfig.isNullable()) {
constraints.add(new NotNullConstraint());
}
if (constraintsConfig.isUnique() != null && constraintsConfig.isUnique()) {
constraints.add(new UniqueConstraint());
}
if (constraintsConfig.isPrimaryKey() != null && constraintsConfig.isPrimaryKey()) {
constraints.add(new PrimaryKeyConstraint(constraintsConfig.getPrimaryKeyName()));
}
if (constraintsConfig.getReferences() != null || (constraintsConfig.getReferencedColumnNames() != null && constraintsConfig.getReferencedTableName() != null)) {
constraints.add(new ForeignKeyConstraint(constraintsConfig.getForeignKeyName(), constraintsConfig.getReferences(), constraintsConfig.getReferencedTableName(), constraintsConfig.getReferencedColumnNames()));
}
}
if (column.isAutoIncrement() != null && column.isAutoIncrement()) {
constraints.add(new AutoIncrementConstraint(column.getName(), column.getStartWith(), column.getIncrementBy()));
}
AddColumnStatement addColumnStatement = new AddColumnStatement(getCatalogName(), getSchemaName(), getTableName(), column.getName(), column.getType(), column.getDefaultValueObject(), column.getRemarks(), constraints.toArray(new ColumnConstraint[constraints.size()]));
addColumnStatement.setDefaultValueConstraintName(column.getDefaultValueConstraintName());
if ((database instanceof MySQLDatabase) && (column.getAfterColumn() != null)) {
addColumnStatement.setAddAfterColumn(column.getAfterColumn());
} else if (((database instanceof HsqlDatabase) || (database instanceof H2Database)) && (column.getBeforeColumn() != null)) {
addColumnStatement.setAddBeforeColumn(column.getBeforeColumn());
} else if ((database instanceof FirebirdDatabase) && (column.getPosition() != null)) {
addColumnStatement.setAddAtPosition(column.getPosition());
}
addColumnStatements.add(addColumnStatement);
if (column.getValueObject() != null) {
UpdateStatement updateStatement = new UpdateStatement(getCatalogName(), getSchemaName(), getTableName());
updateStatement.addNewColumnValue(column.getName(), column.getValueObject());
if (database instanceof DB2Database) {
// Cannot update until table is reorganized in DB2
addColumnUpdateStatements.add(updateStatement);
} else {
sql.add(updateStatement);
}
}
}
if (database instanceof DB2Database) {
sql.add(new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getTableName()));
// Add all the update statements after the reorg table in DB2
sql.addAll(addColumnUpdateStatements);
}
if (addColumnStatements.size() == 1) {
sql.add(0, addColumnStatements.get(0));
} else {
sql.add(0, new AddColumnStatement(addColumnStatements));
}
for (ColumnConfig column : getColumns()) {
String columnRemarks = StringUtils.trimToNull(column.getRemarks());
if (columnRemarks != null) {
SetColumnRemarksStatement remarksStatement = new SetColumnRemarksStatement(catalogName, schemaName, tableName, column.getName(), columnRemarks);
if (SqlGeneratorFactory.getInstance().supports(remarksStatement, database)) {
sql.add(remarksStatement);
}
}
}
return sql.toArray(new SqlStatement[sql.size()]);
}
use of liquibase.statement.core.AddColumnStatement in project liquibase by liquibase.
the class AddColumnGenerator method generateMultipleColumns.
private Sql[] generateMultipleColumns(List<AddColumnStatement> columns, Database database) {
List<Sql> result = new ArrayList<Sql>();
if (database instanceof MySQLDatabase) {
String alterTable = generateSingleColumBaseSQL(columns.get(0), database);
for (int i = 0; i < columns.size(); i++) {
alterTable += generateSingleColumnSQL(columns.get(i), database);
if (i < columns.size() - 1) {
alterTable += ",";
}
}
result.add(new UnparsedSql(alterTable, getAffectedColumns(columns)));
for (AddColumnStatement statement : columns) {
addUniqueConstrantStatements(statement, database, result);
addForeignKeyStatements(statement, database, result);
}
} else {
for (AddColumnStatement column : columns) {
result.addAll(Arrays.asList(generateSingleColumn(column, database)));
}
}
return result.toArray(new Sql[result.size()]);
}
use of liquibase.statement.core.AddColumnStatement in project liquibase by liquibase.
the class OfflineDatabaseTest method canOutputSQLFromOfflineOracleDB.
/**
* Check if it it's possible to output SQL from an OfflineConnection
* set to Oracle (offline:oracle).
*
* @see <a href="https://liquibase.jira.com/browse/CORE-2192">CORE-2192</a>
*/
@Test
public void canOutputSQLFromOfflineOracleDB() throws Exception {
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 = new SqlStatement[0];
try {
statements = change.generateStatements(createOfflineDatabase("offline:oracle"));
} catch (DatabaseException e) {
Assert.fail("Can't generate statements from an Offline Oracle database.");
}
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.statement.core.AddColumnStatement in project liquibase by liquibase.
the class AddColumnGeneratorTest method testAddColumnAfter.
@Test
public void testAddColumnAfter() {
AddColumnStatement statement = new AddColumnStatement(null, null, TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, null);
statement.setAddAfterColumn("column_after");
assertFalse(generatorUnderTest.validate(statement, new MySQLDatabase(), new MockSqlGeneratorChain()).hasErrors());
}
use of liquibase.statement.core.AddColumnStatement in project liquibase by liquibase.
the class AddColumnGeneratorTest method testAddMultipleColumnsMySql.
@Test
public void testAddMultipleColumnsMySql() {
AddColumnStatement columns = new AddColumnStatement(new AddColumnStatement(null, null, TABLE_NAME, "column1", "INT", null, new NotNullConstraint()), new AddColumnStatement(null, null, TABLE_NAME, "column2", "INT", null, new NotNullConstraint()));
assertFalse(generatorUnderTest.validate(columns, new MySQLDatabase(), new MockSqlGeneratorChain()).hasErrors());
Sql[] sql = generatorUnderTest.generateSql(columns, new MySQLDatabase(), new MockSqlGeneratorChain());
assertEquals(1, sql.length);
assertEquals("ALTER TABLE " + TABLE_NAME + " ADD column1 INT NOT NULL, ADD column2 INT NOT NULL", sql[0].toSql());
assertEquals("[DEFAULT, table_name, table_name.column1, table_name.column2]", String.valueOf(sql[0].getAffectedDatabaseObjects()));
}
Aggregations