1 package liquibase.change.core;
2
3 import liquibase.change.AbstractChange;
4 import liquibase.change.ChangeMetaData;
5 import liquibase.change.ColumnConfig;
6 import liquibase.database.Database;
7 import liquibase.database.core.SQLiteDatabase;
8 import liquibase.database.core.SQLiteDatabase.AlterTableVisitor;
9 import liquibase.database.core.SybaseASADatabase;
10 import liquibase.database.structure.Index;
11 import liquibase.statement.SqlStatement;
12 import liquibase.statement.core.DropUniqueConstraintStatement;
13 import liquibase.util.StringUtils;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18
19
20
21 public class DropUniqueConstraintChange extends AbstractChange {
22 private String schemaName;
23 private String tableName;
24 private String constraintName;
25
26
27
28 private String uniqueColumns;
29
30 public DropUniqueConstraintChange() {
31 super("dropUniqueConstraint", "Drop Unique Constraint", ChangeMetaData.PRIORITY_DEFAULT);
32 }
33
34 public String getSchemaName() {
35 return schemaName;
36 }
37
38 public void setSchemaName(String schemaName) {
39 this.schemaName = StringUtils.trimToNull(schemaName);
40 }
41
42 public String getTableName() {
43 return tableName;
44 }
45
46 public void setTableName(String tableName) {
47 this.tableName = tableName;
48 }
49
50 public String getConstraintName() {
51 return constraintName;
52 }
53
54 public void setConstraintName(String constraintName) {
55 this.constraintName = constraintName;
56 }
57
58 public String getUniqueColumns() {
59 return uniqueColumns;
60 }
61
62 public void setUniqueColumns(String uniqueColumns) {
63 this.uniqueColumns = uniqueColumns;
64 }
65
66 public SqlStatement[] generateStatements(Database database) {
67
68
69
70
71
72 DropUniqueConstraintStatement statement = new DropUniqueConstraintStatement(
73 getSchemaName() == null ? database.getDefaultSchemaName() : getSchemaName(), getTableName(),
74 getConstraintName());
75 if (database instanceof SybaseASADatabase) {
76 statement.setUniqueColumns(uniqueColumns);
77 }
78 return new SqlStatement[] { statement };
79 }
80
81 private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) {
82
83
84
85
86
87
88
89
90 List<SqlStatement> statements = new ArrayList<SqlStatement>();
91
92
93 AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() {
94 public ColumnConfig[] getColumnsToAdd() {
95 return new ColumnConfig[0];
96 }
97
98 public boolean copyThisColumn(ColumnConfig column) {
99 return true;
100 }
101
102 public boolean createThisColumn(ColumnConfig column) {
103 if (column.getName().equals(getConstraintName())) {
104 column.getConstraints().setUnique(false);
105 }
106 return true;
107 }
108
109 public boolean createThisIndex(Index index) {
110 return true;
111 }
112 };
113
114 try {
115
116 statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getSchemaName(),
117 getTableName()));
118 } catch (Exception e) {
119 e.printStackTrace();
120 }
121
122 return statements.toArray(new SqlStatement[statements.size()]);
123 }
124
125 public String getConfirmationMessage() {
126 return "Unique constraint " + getConstraintName() + " dropped from " + getTableName();
127 }
128 }