Search in sources :

Example 1 with DropColumnStatement

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()]);
}
Also used : DB2Database(liquibase.database.core.DB2Database) SqlStatement(liquibase.statement.SqlStatement) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) ArrayList(java.util.ArrayList) DropColumnStatement(liquibase.statement.core.DropColumnStatement)

Example 2 with DropColumnStatement

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()]);
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) ArrayList(java.util.ArrayList) DropColumnStatement(liquibase.statement.core.DropColumnStatement) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 3 with DropColumnStatement

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);
    }
}
Also used : ValidationErrors(liquibase.exception.ValidationErrors) DropColumnStatement(liquibase.statement.core.DropColumnStatement)

Example 4 with 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));
}
Also used : DropColumnStatement(liquibase.statement.core.DropColumnStatement) Arrays(java.util.Arrays) List(java.util.List) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Assert.assertTrue(org.junit.Assert.assertTrue) MySQLDatabase(liquibase.database.core.MySQLDatabase) Test(org.junit.Test) Assert(org.junit.Assert) Collectors(java.util.stream.Collectors) Sql(liquibase.sql.Sql) MySQLDatabase(liquibase.database.core.MySQLDatabase) DropColumnStatement(liquibase.statement.core.DropColumnStatement) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 5 with DropColumnStatement

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());
}
Also used : MSSQLDatabase(liquibase.database.core.MSSQLDatabase) DropColumnStatement(liquibase.statement.core.DropColumnStatement) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Aggregations

DropColumnStatement (liquibase.statement.core.DropColumnStatement)9 ArrayList (java.util.ArrayList)4 Sql (liquibase.sql.Sql)4 SqlStatement (liquibase.statement.SqlStatement)4 Test (org.junit.Test)4 DB2Database (liquibase.database.core.DB2Database)3 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)3 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)3 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)3 MockSqlGeneratorChain (liquibase.sqlgenerator.MockSqlGeneratorChain)3 ReorganizeTableStatement (liquibase.statement.core.ReorganizeTableStatement)3 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 DropColumnChange (liquibase.change.core.DropColumnChange)1 MockDatabase (liquibase.database.core.MockDatabase)1 MySQLDatabase (liquibase.database.core.MySQLDatabase)1 ValidationErrors (liquibase.exception.ValidationErrors)1 UnparsedSql (liquibase.sql.UnparsedSql)1 Assert (org.junit.Assert)1