1 // Copyright 2011 Leo Przybylski. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without modification, are
4 // permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice, this list of
7 // conditions and the following disclaimer.
8 //
9 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
10 // of conditions and the following disclaimer in the documentation and/or other materials
11 // provided with the distribution.
12 //
13 // THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
14 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
16 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
21 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 //
23 // The views and conclusions contained in the software and documentation are those of the
24 // authors and should not be interpreted as representing official policies, either expressed
25 // or implied, of Leo Przybylski.
26 package liquibase.change.ext;
27
28 import liquibase.change.AbstractChange;
29 import liquibase.change.Change;
30 import liquibase.change.ChangeMetaData;
31 import liquibase.database.Database;
32 import liquibase.database.structure.ForeignKeyConstraintType;
33 import liquibase.exception.UnexpectedLiquibaseException;
34 import liquibase.statement.SqlStatement;
35 import liquibase.statement.core.AddForeignKeyConstraintStatement;
36
37 import static liquibase.ext.Constants.EXTENSION_PRIORITY;
38
39 /**
40 * Adds a foreign key constraint to an existing column. Making resulting foreign keys ignore schema information
41 *
42 * Leo Przybylski (leo [at] rsmart.com)
43 */
44 public class AddForeignKeyConstraintChange extends liquibase.change.core.AddForeignKeyConstraintChange {
45 public AddForeignKeyConstraintChange() {
46 setPriority(EXTENSION_PRIORITY);
47 }
48
49
50 public SqlStatement[] generateStatements(Database database) {
51
52 boolean deferrable = false;
53 if (getDeferrable() != null) {
54 deferrable = getDeferrable();
55 }
56
57 boolean initiallyDeferred = false;
58 if (getInitiallyDeferred() != null) {
59 initiallyDeferred = getInitiallyDeferred();
60 }
61
62 return new SqlStatement[]{
63 new AddForeignKeyConstraintStatement(getConstraintName(), null,
64 getBaseTableName(),
65 getBaseColumnNames(),
66 null,
67 getReferencedTableName(),
68 getReferencedColumnNames())
69 .setDeferrable(deferrable)
70 .setInitiallyDeferred(initiallyDeferred)
71 .setOnUpdate(getOnUpdate())
72 .setOnDelete(getOnDelete())
73 .setReferencesUniqueColumn(getReferencesUniqueColumn())
74 };
75 }
76 }