Clover Coverage Report - Liquibase Core 2.0.3-SNAPSHOT
Coverage timestamp: Sat Aug 6 2011 11:33:15 EDT
../../../img/srcFileCovDistChart6.png 28% of files have more coverage
64   216   49   2.06
34   163   0.77   31
31     1.58  
1    
 
  AddForeignKeyConstraintChange       Line # 15 64 0% 49 57 55.8% 0.55813956
 
  (11)
 
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  77 toggle public AddForeignKeyConstraintChange() {
37  77 super("addForeignKeyConstraint", "Add Foreign Key Constraint", ChangeMetaData.PRIORITY_DEFAULT);
38    }
39   
 
40  124 toggle public String getBaseTableSchemaName() {
41  124 return baseTableSchemaName;
42    }
43   
 
44  63 toggle public void setBaseTableSchemaName(String baseTableSchemaName) {
45  63 this.baseTableSchemaName = baseTableSchemaName;
46    }
47   
 
48  106 toggle public String getBaseTableName() {
49  106 return baseTableName;
50    }
51   
 
52  63 toggle public void setBaseTableName(String baseTableName) {
53  63 this.baseTableName = baseTableName;
54    }
55   
 
56  106 toggle public String getBaseColumnNames() {
57  106 return baseColumnNames;
58    }
59   
 
60  63 toggle public void setBaseColumnNames(String baseColumnNames) {
61  63 this.baseColumnNames = baseColumnNames;
62    }
63   
 
64  124 toggle public String getReferencedTableSchemaName() {
65  124 return referencedTableSchemaName;
66    }
67   
 
68  62 toggle public void setReferencedTableSchemaName(String referencedTableSchemaName) {
69  62 this.referencedTableSchemaName = referencedTableSchemaName;
70    }
71   
 
72  105 toggle public String getReferencedTableName() {
73  105 return referencedTableName;
74    }
75   
 
76  62 toggle public void setReferencedTableName(String referencedTableName) {
77  62 this.referencedTableName = referencedTableName;
78    }
79   
 
80  105 toggle public String getReferencedColumnNames() {
81  105 return referencedColumnNames;
82    }
83   
 
84  62 toggle public void setReferencedColumnNames(String referencedColumnNames) {
85  62 this.referencedColumnNames = referencedColumnNames;
86    }
87   
 
88  105 toggle public String getConstraintName() {
89  105 return constraintName;
90    }
91   
 
92  63 toggle public void setConstraintName(String constraintName) {
93  63 this.constraintName = constraintName;
94    }
95   
 
96  106 toggle public Boolean getDeferrable() {
97  106 return deferrable;
98    }
99   
 
100  3 toggle public void setDeferrable(Boolean deferrable) {
101  3 this.deferrable = deferrable;
102    }
103   
 
104  106 toggle public Boolean getInitiallyDeferred() {
105  106 return initiallyDeferred;
106    }
107   
 
108  3 toggle public void setInitiallyDeferred(Boolean initiallyDeferred) {
109  3 this.initiallyDeferred = initiallyDeferred;
110    }
111   
112    // public Boolean getDeleteCascade() {
113    // return deleteCascade;
114    // }
115   
 
116  2 toggle public void setDeleteCascade(Boolean deleteCascade) {
117  2 if (deleteCascade != null && deleteCascade) {
118  2 setOnDelete("CASCADE");
119    }
120    }
121   
 
122  2 toggle public void setOnUpdate(String rule) {
123  2 this.onUpdate = rule;
124    }
125   
 
126  105 toggle public String getOnUpdate() {
127  105 return onUpdate;
128    }
129   
 
130  4 toggle public void setOnDelete(String onDelete) {
131  4 this.onDelete = onDelete;
132    }
133   
 
134  105 toggle public String getOnDelete() {
135  105 return this.onDelete;
136    }
137   
 
138  105 toggle public Boolean getReferencesUniqueColumn() {
139  105 return referencesUniqueColumn;
140    }
141   
 
142  3 toggle public void setReferencesUniqueColumn(Boolean referencesUniqueColumn) {
143  3 this.referencesUniqueColumn = referencesUniqueColumn;
144    }
145   
 
146  0 toggle 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    }
163   
 
164  0 toggle 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    }
181   
 
182  105 toggle 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  105 getBaseTableSchemaName() == null ? database.getDefaultSchemaName() : getBaseTableSchemaName(),
196    getBaseTableName(), getBaseColumnNames(),
197  105 getReferencedTableSchemaName() == null ? database.getDefaultSchemaName()
198    : getReferencedTableSchemaName(), getReferencedTableName(), getReferencedColumnNames())
199    .setDeferrable(deferrable).setInitiallyDeferred(initiallyDeferred).setOnUpdate(getOnUpdate())
200    .setOnDelete(getOnDelete()).setReferencesUniqueColumn(getReferencesUniqueColumn()) };
201    }
202   
 
203  0 toggle @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  1 toggle public String getConfirmationMessage() {
214  1 return "Foreign key contraint added to " + getBaseTableName() + " (" + getBaseColumnNames() + ")";
215    }
216    }