Coverage Report - liquibase.change.core.DropAllForeignKeyConstraintsChange
 
Classes in this File Line Coverage Branch Coverage Complexity
DropAllForeignKeyConstraintsChange
5%
2/38
0%
0/14
2.111
 
 1  
 package liquibase.change.core;
 2  
 
 3  
 import liquibase.change.AbstractChange;
 4  
 import liquibase.change.ChangeMetaData;
 5  
 import liquibase.change.ChangeProperty;
 6  
 import liquibase.database.Database;
 7  
 import liquibase.exception.DatabaseException;
 8  
 import liquibase.exception.UnexpectedLiquibaseException;
 9  
 import liquibase.executor.Executor;
 10  
 import liquibase.executor.ExecutorService;
 11  
 import liquibase.sql.visitor.SqlVisitor;
 12  
 import liquibase.statement.SqlStatement;
 13  
 import liquibase.statement.core.FindForeignKeyConstraintsStatement;
 14  
 
 15  
 import java.util.ArrayList;
 16  
 import java.util.Arrays;
 17  
 import java.util.List;
 18  
 import java.util.Map;
 19  
 
 20  
 public class DropAllForeignKeyConstraintsChange extends AbstractChange {
 21  
 
 22  
     private String baseTableSchemaName;
 23  
     private String baseTableName;
 24  
 
 25  
     @ChangeProperty(includeInSerialization = false)
 26  
     private List<DropForeignKeyConstraintChange> childDropChanges;
 27  
 
 28  
     public DropAllForeignKeyConstraintsChange() {
 29  10
         super("dropAllForeignKeyConstraints", "Drop All Foreign Key Constraints", ChangeMetaData.PRIORITY_DEFAULT);
 30  10
     }
 31  
 
 32  
     public String getBaseTableSchemaName() {
 33  0
         return baseTableSchemaName;
 34  
     }
 35  
 
 36  
     public void setBaseTableSchemaName(String baseTableSchemaName) {
 37  0
         this.baseTableSchemaName = baseTableSchemaName;
 38  0
     }
 39  
 
 40  
     public String getBaseTableName() {
 41  0
         return baseTableName;
 42  
     }
 43  
 
 44  
     public void setBaseTableName(String baseTableName) {
 45  0
         this.baseTableName = baseTableName;
 46  0
     }
 47  
 
 48  
     public SqlStatement[] generateStatements(Database database) {
 49  0
         List<SqlStatement> sqlStatements = new ArrayList<SqlStatement>();
 50  
 
 51  0
         if (childDropChanges == null) {
 52  0
             generateChildren(database);
 53  
         }
 54  
 
 55  0
         if (childDropChanges != null) {
 56  0
             for (DropForeignKeyConstraintChange change : childDropChanges) {
 57  0
                 sqlStatements.addAll(Arrays.asList(change.generateStatements(database)));
 58  
             }
 59  
         }
 60  
 
 61  0
         return sqlStatements.toArray(new SqlStatement[sqlStatements.size()]);
 62  
     }
 63  
 
 64  
     public String getConfirmationMessage() {
 65  0
         return "Foreign keys on base table " + getBaseTableName() + " dropped";
 66  
     }
 67  
 
 68  
     private void generateChildren(Database database) {
 69  
         // Make a new list
 70  0
         childDropChanges = new ArrayList<DropForeignKeyConstraintChange>();
 71  
 
 72  0
         Executor executor = ExecutorService.getInstance().getExecutor(database);
 73  
 
 74  0
         FindForeignKeyConstraintsStatement sql = new FindForeignKeyConstraintsStatement(getBaseTableSchemaName(),
 75  
                 getBaseTableName());
 76  
 
 77  
         try {
 78  0
             List<Map> results = executor.queryForList(sql);
 79  
 
 80  0
             if (results != null && results.size() > 0) {
 81  0
                 for (Map result : results) {
 82  0
                     String baseTableName = (String) result
 83  
                             .get(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_NAME);
 84  0
                     String constraintName = (String) result
 85  
                             .get(FindForeignKeyConstraintsStatement.RESULT_COLUMN_CONSTRAINT_NAME);
 86  0
                     if (getBaseTableName().equalsIgnoreCase(baseTableName)) {
 87  0
                         DropForeignKeyConstraintChange dropForeignKeyConstraintChange = new DropForeignKeyConstraintChange();
 88  
 
 89  0
                         dropForeignKeyConstraintChange.setBaseTableSchemaName(getBaseTableSchemaName());
 90  0
                         dropForeignKeyConstraintChange.setBaseTableName(baseTableName);
 91  0
                         dropForeignKeyConstraintChange.setConstraintName(constraintName);
 92  
 
 93  0
                         childDropChanges.add(dropForeignKeyConstraintChange);
 94  0
                     } else {
 95  0
                         throw new IllegalStateException("Expected to return only foreign keys for base table name: "
 96  
                                 + getBaseTableName() + " and got results for table: " + baseTableName);
 97  
                     }
 98  0
                 }
 99  
             }
 100  0
         } catch (DatabaseException e) {
 101  0
             throw new UnexpectedLiquibaseException("Failed to find foreign keys for table: " + getBaseTableName(), e);
 102  0
         }
 103  0
     }
 104  
 
 105  
     @Override
 106  
     public boolean requiresUpdatedDatabaseMetadata(Database database) {
 107  0
         return true;
 108  
     }
 109  
 }