Coverage Report - liquibase.change.core.AddForeignKeyConstraintChange
 
Classes in this File Line Coverage Branch Coverage Complexity
AddForeignKeyConstraintChange
61%
49/80
27%
10/36
1.645
 
 1  
 package liquibase.change.core;
 2  
 
 3  
 import liquibase.change.AbstractChange;
 4  
 import liquibase.change.Change;
 5  
 import liquibase.change.ChangeMetaData;
 6  
 import liquibase.database.Database;
 7  
 import liquibase.database.structure.ForeignKeyConstraintType;
 8  
 import liquibase.exception.UnexpectedLiquibaseException;
 9  
 import liquibase.statement.SqlStatement;
 10  
 import liquibase.statement.core.AddForeignKeyConstraintStatement;
 11  
 
 12  
 /**
 13  
  * Adds a foreign key constraint to an existing column.
 14  
  */
 15  
 public class AddForeignKeyConstraintChange extends AbstractChange {
 16  
     private String baseTableSchemaName;
 17  
     private String baseTableName;
 18  
     private String baseColumnNames;
 19  
 
 20  
     private String referencedTableSchemaName;
 21  
     private String referencedTableName;
 22  
     private String referencedColumnNames;
 23  
 
 24  
     private String constraintName;
 25  
 
 26  
     private Boolean deferrable;
 27  
     private Boolean initiallyDeferred;
 28  
 
 29  
     private String onUpdate;
 30  
     private String onDelete;
 31  
 
 32  
     // Some databases supports creation of FK with referention to column marked as unique, not primary
 33  
     // If FK referenced to such unique column this option should be set to false
 34  
     private Boolean referencesUniqueColumn;
 35  
 
 36  
     public AddForeignKeyConstraintChange() {
 37  77
         super("addForeignKeyConstraint", "Add Foreign Key Constraint", ChangeMetaData.PRIORITY_DEFAULT);
 38  77
     }
 39  
 
 40  
     public String getBaseTableSchemaName() {
 41  124
         return baseTableSchemaName;
 42  
     }
 43  
 
 44  
     public void setBaseTableSchemaName(String baseTableSchemaName) {
 45  63
         this.baseTableSchemaName = baseTableSchemaName;
 46  63
     }
 47  
 
 48  
     public String getBaseTableName() {
 49  106
         return baseTableName;
 50  
     }
 51  
 
 52  
     public void setBaseTableName(String baseTableName) {
 53  63
         this.baseTableName = baseTableName;
 54  63
     }
 55  
 
 56  
     public String getBaseColumnNames() {
 57  106
         return baseColumnNames;
 58  
     }
 59  
 
 60  
     public void setBaseColumnNames(String baseColumnNames) {
 61  63
         this.baseColumnNames = baseColumnNames;
 62  63
     }
 63  
 
 64  
     public String getReferencedTableSchemaName() {
 65  124
         return referencedTableSchemaName;
 66  
     }
 67  
 
 68  
     public void setReferencedTableSchemaName(String referencedTableSchemaName) {
 69  62
         this.referencedTableSchemaName = referencedTableSchemaName;
 70  62
     }
 71  
 
 72  
     public String getReferencedTableName() {
 73  105
         return referencedTableName;
 74  
     }
 75  
 
 76  
     public void setReferencedTableName(String referencedTableName) {
 77  62
         this.referencedTableName = referencedTableName;
 78  62
     }
 79  
 
 80  
     public String getReferencedColumnNames() {
 81  105
         return referencedColumnNames;
 82  
     }
 83  
 
 84  
     public void setReferencedColumnNames(String referencedColumnNames) {
 85  62
         this.referencedColumnNames = referencedColumnNames;
 86  62
     }
 87  
 
 88  
     public String getConstraintName() {
 89  105
         return constraintName;
 90  
     }
 91  
 
 92  
     public void setConstraintName(String constraintName) {
 93  63
         this.constraintName = constraintName;
 94  63
     }
 95  
 
 96  
     public Boolean getDeferrable() {
 97  106
         return deferrable;
 98  
     }
 99  
 
 100  
     public void setDeferrable(Boolean deferrable) {
 101  3
         this.deferrable = deferrable;
 102  3
     }
 103  
 
 104  
     public Boolean getInitiallyDeferred() {
 105  106
         return initiallyDeferred;
 106  
     }
 107  
 
 108  
     public void setInitiallyDeferred(Boolean initiallyDeferred) {
 109  3
         this.initiallyDeferred = initiallyDeferred;
 110  3
     }
 111  
 
 112  
     // public Boolean getDeleteCascade() {
 113  
     // return deleteCascade;
 114  
     // }
 115  
 
 116  
     public void setDeleteCascade(Boolean deleteCascade) {
 117  2
         if (deleteCascade != null && deleteCascade) {
 118  2
             setOnDelete("CASCADE");
 119  
         }
 120  2
     }
 121  
 
 122  
     public void setOnUpdate(String rule) {
 123  2
         this.onUpdate = rule;
 124  2
     }
 125  
 
 126  
     public String getOnUpdate() {
 127  105
         return onUpdate;
 128  
     }
 129  
 
 130  
     public void setOnDelete(String onDelete) {
 131  4
         this.onDelete = onDelete;
 132  4
     }
 133  
 
 134  
     public String getOnDelete() {
 135  105
         return this.onDelete;
 136  
     }
 137  
 
 138  
     public Boolean getReferencesUniqueColumn() {
 139  105
         return referencesUniqueColumn;
 140  
     }
 141  
 
 142  
     public void setReferencesUniqueColumn(Boolean referencesUniqueColumn) {
 143  3
         this.referencesUniqueColumn = referencesUniqueColumn;
 144  3
     }
 145  
 
 146  
     public void setOnDelete(ForeignKeyConstraintType rule) {
 147  0
         if (rule == null) {
 148  
             // nothing
 149  0
         } else if (rule == ForeignKeyConstraintType.importedKeyCascade) {
 150  0
             setOnDelete("CASCADE");
 151  0
         } else if (rule == ForeignKeyConstraintType.importedKeySetNull) {
 152  0
             setOnDelete("SET NULL");
 153  0
         } else if (rule == ForeignKeyConstraintType.importedKeySetDefault) {
 154  0
             setOnDelete("SET DEFAULT");
 155  0
         } else if (rule == ForeignKeyConstraintType.importedKeyRestrict) {
 156  0
             setOnDelete("RESTRICT");
 157  0
         } else if (rule == ForeignKeyConstraintType.importedKeyNoAction) {
 158  0
             setOnDelete("NO ACTION");
 159  
         } else {
 160  0
             throw new UnexpectedLiquibaseException("Unknown onDelete action: " + rule);
 161  
         }
 162  0
     }
 163  
 
 164  
     public void setOnUpdate(ForeignKeyConstraintType rule) {
 165  0
         if (rule == null) {
 166  
             // nothing
 167  0
         } else if (rule == ForeignKeyConstraintType.importedKeyCascade) {
 168  0
             setOnUpdate("CASCADE");
 169  0
         } else if (rule == ForeignKeyConstraintType.importedKeySetNull) {
 170  0
             setOnUpdate("SET NULL");
 171  0
         } else if (rule == ForeignKeyConstraintType.importedKeySetDefault) {
 172  0
             setOnUpdate("SET DEFAULT");
 173  0
         } else if (rule == ForeignKeyConstraintType.importedKeyRestrict) {
 174  0
             setOnUpdate("RESTRICT");
 175  0
         } else if (rule == ForeignKeyConstraintType.importedKeyNoAction) {
 176  0
             setOnUpdate("NO ACTION");
 177  
         } else {
 178  0
             throw new UnexpectedLiquibaseException("Unknown onUpdate action: " + onUpdate);
 179  
         }
 180  0
     }
 181  
 
 182  
     public SqlStatement[] generateStatements(Database database) {
 183  
 
 184  105
         boolean deferrable = false;
 185  105
         if (getDeferrable() != null) {
 186  1
             deferrable = getDeferrable();
 187  
         }
 188  
 
 189  105
         boolean initiallyDeferred = false;
 190  105
         if (getInitiallyDeferred() != null) {
 191  1
             initiallyDeferred = getInitiallyDeferred();
 192  
         }
 193  
 
 194  105
         return new SqlStatement[] { new AddForeignKeyConstraintStatement(getConstraintName(),
 195  
                 getBaseTableSchemaName() == null ? database.getDefaultSchemaName() : getBaseTableSchemaName(),
 196  
                 getBaseTableName(), getBaseColumnNames(),
 197  
                 getReferencedTableSchemaName() == null ? database.getDefaultSchemaName()
 198  
                         : getReferencedTableSchemaName(), getReferencedTableName(), getReferencedColumnNames())
 199  
                 .setDeferrable(deferrable).setInitiallyDeferred(initiallyDeferred).setOnUpdate(getOnUpdate())
 200  
                 .setOnDelete(getOnDelete()).setReferencesUniqueColumn(getReferencesUniqueColumn()) };
 201  
     }
 202  
 
 203  
     @Override
 204  
     protected Change[] createInverses() {
 205  0
         DropForeignKeyConstraintChange inverse = new DropForeignKeyConstraintChange();
 206  0
         inverse.setBaseTableSchemaName(getBaseTableSchemaName());
 207  0
         inverse.setBaseTableName(getBaseTableName());
 208  0
         inverse.setConstraintName(getConstraintName());
 209  
 
 210  0
         return new Change[] { inverse };
 211  
     }
 212  
 
 213  
     public String getConfirmationMessage() {
 214  1
         return "Foreign key contraint added to " + getBaseTableName() + " (" + getBaseColumnNames() + ")";
 215  
     }
 216  
 }