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