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