use of liquibase.statement.core.DropColumnStatement in project liquibase by liquibase.
the class DropColumnChange method generateSingleColumn.
private SqlStatement[] generateSingleColumn(Database database) throws DatabaseException {
if (database instanceof SQLiteDatabase) {
// return special statements for SQLite databases
return generateStatementsForSQLiteDatabase(database);
}
List<SqlStatement> statements = new ArrayList<>();
statements.add(new DropColumnStatement(getCatalogName(), getSchemaName(), getTableName(), getColumnName()));
if (database instanceof DB2Database) {
statements.add(new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getTableName()));
}
return statements.toArray(new SqlStatement[statements.size()]);
}
use of liquibase.statement.core.DropColumnStatement in project liquibase by liquibase.
the class DropColumnGenerator method generateMultipleColumnSql.
private Sql[] generateMultipleColumnSql(List<DropColumnStatement> columns, Database database) {
List<Sql> result = new ArrayList<>();
if (database instanceof MySQLDatabase) {
String alterTable = "ALTER TABLE " + database.escapeTableName(columns.get(0).getCatalogName(), columns.get(0).getSchemaName(), columns.get(0).getTableName());
for (int i = 0; i < columns.size(); i++) {
alterTable += " DROP " + database.escapeColumnName(columns.get(i).getCatalogName(), columns.get(i).getSchemaName(), columns.get(i).getTableName(), columns.get(i).getColumnName());
if (i < (columns.size() - 1)) {
alterTable += ",";
}
}
result.add(new UnparsedSql(alterTable, getAffectedColumns(columns)));
} else {
if (database instanceof MSSQLDatabase) {
for (DropColumnStatement column : columns) {
final Sql[] sqls = generateSingleColumnSql(column, database);
result.add(sqls[0]);
result.add(sqls[1]);
}
} else {
for (DropColumnStatement column : columns) {
result.add(generateSingleColumnSql(column, database)[0]);
}
}
}
return result.toArray(new Sql[result.size()]);
}
use of liquibase.statement.core.DropColumnStatement in project liquibase by liquibase.
the class DropColumnGenerator method validate.
@Override
public ValidationErrors validate(DropColumnStatement dropColumnStatement, Database database, SqlGeneratorChain sqlGeneratorChain) {
if (dropColumnStatement.isMultiple()) {
ValidationErrors validationErrors = new ValidationErrors();
DropColumnStatement firstColumn = dropColumnStatement.getColumns().get(0);
for (DropColumnStatement drop : dropColumnStatement.getColumns()) {
validationErrors.addAll(validateSingleColumn(drop));
if ((drop.getTableName() != null) && !drop.getTableName().equals(firstColumn.getTableName())) {
validationErrors.addError("All columns must be targeted at the same table");
}
if (drop.isMultiple()) {
validationErrors.addError("Nested multiple drop column statements are not supported");
}
}
return validationErrors;
} else {
return validateSingleColumn(dropColumnStatement);
}
}
use of liquibase.statement.core.DropColumnStatement in project liquibase by liquibase.
the class DropColumnGeneratorTest method testDropMultipleColumnsMySQL.
@Test
public void testDropMultipleColumnsMySQL() {
DropColumnStatement drop = new DropColumnStatement(Arrays.asList(new DropColumnStatement(null, null, "TEST_TABLE", "col1"), new DropColumnStatement(null, null, "TEST_TABLE", "col2")));
Assert.assertFalse(generatorUnderTest.validate(drop, new MySQLDatabase(), new MockSqlGeneratorChain()).hasErrors());
Sql[] sql = generatorUnderTest.generateSql(drop, new MySQLDatabase(), new MockSqlGeneratorChain());
Assert.assertEquals(1, sql.length);
Assert.assertEquals("ALTER TABLE TEST_TABLE DROP col1, DROP col2", sql[0].toSql());
List<String> actualNames = sql[0].getAffectedDatabaseObjects().stream().map(o -> o.toString()).collect(Collectors.toList());
List<String> expectedNames = Arrays.asList(new String[] { "TEST_TABLE.col1", "TEST_TABLE.col2", "TEST_TABLE", "DEFAULT" });
assertTrue(actualNames.containsAll(expectedNames));
assertTrue(expectedNames.containsAll(actualNames));
}
use of liquibase.statement.core.DropColumnStatement in project liquibase by liquibase.
the class DropColumnGeneratorTest method testDropMultipleColumnsMSSQL.
@Test
public void testDropMultipleColumnsMSSQL() {
DropColumnStatement drop = new DropColumnStatement(Arrays.asList(new DropColumnStatement(null, null, "TEST_TABLE", "col1"), new DropColumnStatement(null, null, "TEST_TABLE", "col2")));
Assert.assertFalse(generatorUnderTest.validate(drop, new MSSQLDatabase(), new MockSqlGeneratorChain()).hasErrors());
Sql[] sql = generatorUnderTest.generateSql(drop, new MSSQLDatabase(), new MockSqlGeneratorChain());
Assert.assertEquals(4, sql.length);
Assert.assertTrue(sql[0].toSql().contains("TEST_TABLE") && sql[0].toSql().contains("col1"));
Assert.assertEquals("ALTER TABLE TEST_TABLE DROP COLUMN col1", sql[1].toSql());
Assert.assertTrue(sql[2].toSql().contains("TEST_TABLE") && sql[2].toSql().contains("col2"));
Assert.assertEquals("ALTER TABLE TEST_TABLE DROP COLUMN col2", sql[3].toSql());
}
Aggregations