Search in sources :

Example 1 with FindForeignKeyConstraintsStatement

use of liquibase.statement.core.FindForeignKeyConstraintsStatement in project liquibase by liquibase.

the class DropAllForeignKeyConstraintsChange method generateChildren.

private List<DropForeignKeyConstraintChange> generateChildren(Database database) {
    // Make a new list
    List<DropForeignKeyConstraintChange> childDropChanges = new ArrayList<DropForeignKeyConstraintChange>();
    Executor executor = ExecutorService.getInstance().getExecutor(database);
    FindForeignKeyConstraintsStatement sql = new FindForeignKeyConstraintsStatement(getBaseTableCatalogName(), getBaseTableSchemaName(), getBaseTableName());
    try {
        List<Map<String, ?>> results = executor.queryForList(sql);
        Set<String> handledConstraints = new HashSet<String>();
        if (results != null && results.size() > 0) {
            for (Map result : results) {
                String baseTableName = (String) result.get(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_NAME);
                String constraintName = (String) result.get(FindForeignKeyConstraintsStatement.RESULT_COLUMN_CONSTRAINT_NAME);
                if (DatabaseObjectComparatorFactory.getInstance().isSameObject(new Table().setName(getBaseTableName()), new Table().setName(baseTableName), null, database)) {
                    if (!handledConstraints.contains(constraintName)) {
                        DropForeignKeyConstraintChange dropForeignKeyConstraintChange = new DropForeignKeyConstraintChange();
                        dropForeignKeyConstraintChange.setBaseTableSchemaName(getBaseTableSchemaName());
                        dropForeignKeyConstraintChange.setBaseTableName(baseTableName);
                        dropForeignKeyConstraintChange.setConstraintName(constraintName);
                        childDropChanges.add(dropForeignKeyConstraintChange);
                        handledConstraints.add(constraintName);
                    }
                } else {
                    throw new IllegalStateException("Expected to return only foreign keys for base table name: " + getBaseTableName() + " and got results for table: " + baseTableName);
                }
            }
        }
        return childDropChanges;
    } catch (DatabaseException e) {
        throw new UnexpectedLiquibaseException("Failed to find foreign keys for table: " + getBaseTableName(), e);
    }
}
Also used : Table(liquibase.structure.core.Table) ArrayList(java.util.ArrayList) Executor(liquibase.executor.Executor) FindForeignKeyConstraintsStatement(liquibase.statement.core.FindForeignKeyConstraintsStatement) Map(java.util.Map) DatabaseException(liquibase.exception.DatabaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 DatabaseException (liquibase.exception.DatabaseException)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 Executor (liquibase.executor.Executor)1 FindForeignKeyConstraintsStatement (liquibase.statement.core.FindForeignKeyConstraintsStatement)1 Table (liquibase.structure.core.Table)1