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