liquibase.statement.core.DropColumnStatement Java Examples

The following examples show how to use liquibase.statement.core.DropColumnStatement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PerconaAddColumnChangeTest.java    From liquibase-percona with Apache License 2.0 6 votes vote down vote up
@Test
public void testRollbackSQL() throws RollbackImpossibleException {
    enableLogging();

    SqlStatement[] statements = generateRollbackStatements();
    Assertions.assertEquals(3, statements.length);
    Assertions.assertEquals(CommentStatement.class, statements[0].getClass());
    Assertions.assertEquals("pt-online-schema-change "
            + "--alter-foreign-keys-method=auto "
            + "--nocheck-unique-key-change "
            + "--alter=\"DROP COLUMN new_column\" "
            + "--host=localhost --port=3306 --user=user --password=*** --execute D=testdb,t=person",
            ((CommentStatement)statements[0]).getText());
    Assertions.assertEquals(CommentStatement.class, statements[1].getClass());
    Assertions.assertEquals(DropColumnStatement.class, statements[2].getClass());
}
 
Example #2
Source File: DropGeometryColumnGeneratorGeoDBTest.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "generateSqlTestData")
public void testGenerateSql(final DropColumnStatement statement, final Database database,
      final Sql[] expected) throws DatabaseException {
   final DropGeometryColumnGeneratorGeoDB generator = new DropGeometryColumnGeneratorGeoDB();
   final SqlGeneratorChain sqlGeneratorChain = mock(SqlGeneratorChain.class);
   when(sqlGeneratorChain.generateSql(statement, database)).thenReturn(new Sql[0]);
   final Sql[] result = generator.generateSql(statement, database, sqlGeneratorChain);
   assertEquals(result.length, expected.length);
   if (result.length > 0) {
      for (int ii = 0; ii < result.length; ii++) {
         final Sql resultSql = result[ii];
         final Sql expectedSql = expected[ii];
         assertEquals(resultSql.toSql(), expectedSql.toSql());
      }
   }
}
 
Example #3
Source File: DropGeometryColumnGeneratorGeoDB.java    From liquibase-spatial with Apache License 2.0 6 votes vote down vote up
@Override
public Sql[] generateSql(final DropColumnStatement statement, final Database database,
      final SqlGeneratorChain sqlGeneratorChain) {

   String schemaName = statement.getSchemaName();
   if (schemaName == null) {
      schemaName = database.getDefaultSchemaName();
   }
   final String tableName = statement.getTableName();
   final String columnName = statement.getColumnName();
   final boolean isGeometryColumn = GeometryColumnsUtils.isGeometryColumn(database, schemaName,
         tableName, columnName);
   final List<Sql> list = new ArrayList<Sql>();
   if (isGeometryColumn) {
      dropSpatialIndexIfExists(statement.getCatalogName(), schemaName, tableName, database, list);
      final String sql = "CALL DropGeometryColumn('" + schemaName + "', '" + tableName + "', '"
            + columnName + "')";
      final Column column = getAffectedColumn(statement);
      final Sql dropGeometryColumn = new UnparsedSql(sql, column);
      list.add(dropGeometryColumn);
   } else {
      list.addAll(Arrays.asList(sqlGeneratorChain.generateSql(statement, database)));
   }
   return list.toArray(new Sql[list.size()]);
}
 
Example #4
Source File: PerconaDropColumnChangeTest.java    From liquibase-percona with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateSQL() {
    enableLogging();

    SqlStatement[] statements = generateStatements();
    Assertions.assertEquals(3, statements.length);
    Assertions.assertEquals(CommentStatement.class, statements[0].getClass());
    Assertions.assertEquals("pt-online-schema-change "
            + "--alter-foreign-keys-method=auto "
            + "--nocheck-unique-key-change "
            + "--alter=\"DROP COLUMN col_test\" "
            + "--host=localhost --port=3306 --user=user --password=*** --execute D=testdb,t=person",
            ((CommentStatement)statements[0]).getText());
    Assertions.assertEquals(CommentStatement.class, statements[1].getClass());
    Assertions.assertEquals(DropColumnStatement.class, statements[2].getClass());
}
 
Example #5
Source File: HiveDropColumnGenerator.java    From liquibase-impala with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #6
Source File: HiveDropColumnGenerator.java    From liquibase-impala with Apache License 2.0 5 votes vote down vote up
private Sql[] generateMultipleColumnSql(DropColumnStatement dropColumnStatement, Database database, Map<String, String> columnsPreserved) {
    if (columnsPreserved == null) {
        throw new UnexpectedLiquibaseException("no columns to preserve");
    }
    List<Sql> result = new ArrayList<Sql>();
    Map<String, String> columnsPreservedCopy = new HashMap<String, String>(columnsPreserved);
    String alterTable;
    List<DropColumnStatement> columns = null;

    if (dropColumnStatement.isMultiple()) {
        columns = dropColumnStatement.getColumns();
        for (DropColumnStatement statement : columns) {
            columnsPreservedCopy.remove(statement.getColumnName());
        }
        alterTable = "ALTER TABLE " + database.escapeTableName(columns.get(0).getCatalogName(), columns.get(0).getSchemaName(), columns.get(0).getTableName()) + " REPLACE COLUMNS (";
    } else {
        columnsPreservedCopy.remove(dropColumnStatement.getColumnName());
        alterTable = "ALTER TABLE " + database.escapeTableName(dropColumnStatement.getCatalogName(), dropColumnStatement.getSchemaName(), dropColumnStatement.getTableName()) + " REPLACE COLUMNS (";
    }

    int i = 0;
    for (String columnName : columnsPreservedCopy.keySet()) {
        alterTable += database.escapeObjectName(columnName, Column.class) + " " + columnsPreservedCopy.get(columnName);
        if (i < columnsPreservedCopy.size() - 1) {
            alterTable += ",";
        } else {
            alterTable += ")";
        }
        i++;
    }

    if (dropColumnStatement.isMultiple()) {
        result.add(new UnparsedSql(alterTable, getAffectedColumns(columns)));
    } else {
        result.add(new UnparsedSql(alterTable, getAffectedColumn(dropColumnStatement)));
    }
    return result.toArray(new Sql[result.size()]);
}
 
Example #7
Source File: HiveDropColumnGenerator.java    From liquibase-impala with Apache License 2.0 5 votes vote down vote up
private Column[] getAffectedColumns(List<DropColumnStatement> columns) {
    List<Column> affected = new ArrayList<Column>();
    for (DropColumnStatement column : columns) {
        affected.add(getAffectedColumn(column));
    }
    return affected.toArray(new Column[affected.size()]);
}
 
Example #8
Source File: PerconaAddColumnChangeTest.java    From liquibase-percona with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithoutPerconaRollback() throws RollbackImpossibleException {
    PTOnlineSchemaChangeStatement.available = false;
    SqlStatement[] statements = generateRollbackStatements();
    Assertions.assertEquals(1, statements.length);
    Assertions.assertEquals(DropColumnStatement.class, statements[0].getClass());
}
 
Example #9
Source File: PerconaDropColumnChangeTest.java    From liquibase-percona with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithoutPercona() {
    PTOnlineSchemaChangeStatement.available = false;
    SqlStatement[] statements = generateStatements();
    Assertions.assertEquals(1, statements.length);
    Assertions.assertEquals(DropColumnStatement.class, statements[0].getClass());
}
 
Example #10
Source File: DropGeometryColumnGeneratorGeoDBTest.java    From liquibase-spatial with Apache License 2.0 5 votes vote down vote up
/**
 * Provides test data to {@link #testSupports(DropColumnStatement, Database, boolean)}.
 * 
 * @return the test data.
 */
@DataProvider
public Object[][] supportsTestData() {
   final DropColumnStatement statement = new DropColumnStatement(null, null, null, null);
   return new Object[][] { new Object[] { statement, new DerbyDatabase(), true },
         new Object[] { statement, new H2Database(), true },
         new Object[] { statement, new OracleDatabase(), false }, };
}
 
Example #11
Source File: HiveDropColumnGenerator.java    From liquibase-impala with Apache License 2.0 4 votes vote down vote up
private Column getAffectedColumn(DropColumnStatement statement) {
    return new Column().setName(statement.getColumnName()).setRelation(new Table().setName(statement.getTableName()).setSchema(statement.getCatalogName(), statement.getSchemaName()));
}
 
Example #12
Source File: HiveDropColumnGenerator.java    From liquibase-impala with Apache License 2.0 4 votes vote down vote up
@Override
public boolean supports(DropColumnStatement dropColumnStatement, Database database) {
    return database instanceof HiveDatabase && super.supports(dropColumnStatement, database);
}
 
Example #13
Source File: HiveDropColumnGenerator.java    From liquibase-impala with Apache License 2.0 4 votes vote down vote up
@Override
public Sql[] generateSql(DropColumnStatement dropColumnStatement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    Map<String, String> columnsPreserved = null;
    columnsPreserved = columnsMap((HiveDatabase) database, dropColumnStatement);
    return generateMultipleColumnSql(dropColumnStatement, database, columnsPreserved);
}
 
Example #14
Source File: DropGeometryColumnGeneratorGeoDB.java    From liquibase-spatial with Apache License 2.0 4 votes vote down vote up
/**
 * @see liquibase.sqlgenerator.core.AbstractSqlGenerator#supports(liquibase.statement.SqlStatement,
 *      liquibase.database.Database)
 */
@Override
public boolean supports(final DropColumnStatement statement, final Database database) {
   return database instanceof DerbyDatabase || database instanceof H2Database;
}
 
Example #15
Source File: DropGeometryColumnGeneratorGeoDB.java    From liquibase-spatial with Apache License 2.0 4 votes vote down vote up
@Override
public ValidationErrors validate(final DropColumnStatement statement, final Database database,
      final SqlGeneratorChain sqlGeneratorChain) {
   return sqlGeneratorChain.validate(statement, database);
}
 
Example #16
Source File: HiveDropColumnGenerator.java    From liquibase-impala with Apache License 2.0 4 votes vote down vote up
private ValidationErrors validateSingleColumn(DropColumnStatement dropColumnStatement) {
    ValidationErrors validationErrors = new ValidationErrors();
    validationErrors.checkRequiredField("tableName", dropColumnStatement.getTableName());
    validationErrors.checkRequiredField("columnName", dropColumnStatement.getColumnName());
    return validationErrors;
}
 
Example #17
Source File: DropGeometryColumnGeneratorGeoDBTest.java    From liquibase-spatial with Apache License 2.0 3 votes vote down vote up
/**
 * Tests {@link DropGeometryColumnGeneratorGeoDB#supports(DropColumnStatement, Database)}.
 * 
 * @param statement
 *           the add column statement.
 * @param database
 *           the database instance.
 * @param expected
 *           the expected result from <code>supports</code>.
 */
@Test(dataProvider = "supportsTestData")
public void testSupports(final DropColumnStatement statement, final Database database,
      final boolean expected) {
   final DropGeometryColumnGeneratorGeoDB generator = new DropGeometryColumnGeneratorGeoDB();
   final boolean result = generator.supports(statement, database);
   assertEquals(result, expected);
}