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.RenameColumnStatement; |
13 | |
import liquibase.util.StringUtils; |
14 | |
|
15 | |
import java.util.ArrayList; |
16 | |
import java.util.List; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
public class RenameColumnChange extends AbstractChange { |
22 | |
|
23 | |
private String schemaName; |
24 | |
private String tableName; |
25 | |
private String oldColumnName; |
26 | |
private String newColumnName; |
27 | |
private String columnDataType; |
28 | |
|
29 | |
public RenameColumnChange() { |
30 | 20 | super("renameColumn", "Rename Column", ChangeMetaData.PRIORITY_DEFAULT); |
31 | 20 | } |
32 | |
|
33 | |
public String getSchemaName() { |
34 | 45 | return schemaName; |
35 | |
} |
36 | |
|
37 | |
public void setSchemaName(String schemaName) { |
38 | 7 | this.schemaName = StringUtils.trimToNull(schemaName); |
39 | 7 | } |
40 | |
|
41 | |
public String getTableName() { |
42 | 44 | return tableName; |
43 | |
} |
44 | |
|
45 | |
public void setTableName(String tableName) { |
46 | 7 | this.tableName = tableName; |
47 | 7 | } |
48 | |
|
49 | |
public String getOldColumnName() { |
50 | 44 | return oldColumnName; |
51 | |
} |
52 | |
|
53 | |
public void setOldColumnName(String oldColumnName) { |
54 | 7 | this.oldColumnName = oldColumnName; |
55 | 7 | } |
56 | |
|
57 | |
public String getNewColumnName() { |
58 | 44 | return newColumnName; |
59 | |
} |
60 | |
|
61 | |
public void setNewColumnName(String newColumnName) { |
62 | 7 | this.newColumnName = newColumnName; |
63 | 7 | } |
64 | |
|
65 | |
public String getColumnDataType() { |
66 | 44 | return columnDataType; |
67 | |
} |
68 | |
|
69 | |
public void setColumnDataType(String columnDataType) { |
70 | 0 | this.columnDataType = columnDataType; |
71 | 0 | } |
72 | |
|
73 | |
public SqlStatement[] generateStatements(Database database) { |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | 44 | return new SqlStatement[] { new RenameColumnStatement(getSchemaName() == null ? database.getDefaultSchemaName() |
80 | |
: getSchemaName(), getTableName(), getOldColumnName(), getNewColumnName(), getColumnDataType()) }; |
81 | |
} |
82 | |
|
83 | |
private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) { |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | 0 | List<SqlStatement> statements = new ArrayList<SqlStatement>(); |
90 | |
|
91 | |
|
92 | 0 | AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() { |
93 | |
public ColumnConfig[] getColumnsToAdd() { |
94 | 0 | return new ColumnConfig[0]; |
95 | |
} |
96 | |
|
97 | |
public boolean copyThisColumn(ColumnConfig column) { |
98 | 0 | return true; |
99 | |
} |
100 | |
|
101 | |
public boolean createThisColumn(ColumnConfig column) { |
102 | 0 | if (column.getName().equals(getOldColumnName())) { |
103 | 0 | column.setName(getNewColumnName()); |
104 | |
} |
105 | 0 | return true; |
106 | |
} |
107 | |
|
108 | |
public boolean createThisIndex(Index index) { |
109 | 0 | if (index.getColumns().contains(getOldColumnName())) { |
110 | 0 | index.getColumns().remove(getOldColumnName()); |
111 | 0 | index.getColumns().add(getNewColumnName()); |
112 | |
} |
113 | 0 | return true; |
114 | |
} |
115 | |
}; |
116 | |
|
117 | |
try { |
118 | |
|
119 | 0 | statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getSchemaName(), |
120 | |
getTableName())); |
121 | 0 | } catch (Exception e) { |
122 | 0 | System.err.println(e); |
123 | 0 | e.printStackTrace(); |
124 | 0 | } |
125 | |
|
126 | 0 | return statements.toArray(new SqlStatement[statements.size()]); |
127 | |
} |
128 | |
|
129 | |
@Override |
130 | |
protected Change[] createInverses() { |
131 | 0 | RenameColumnChange inverse = new RenameColumnChange(); |
132 | 0 | inverse.setSchemaName(getSchemaName()); |
133 | 0 | inverse.setTableName(getTableName()); |
134 | 0 | inverse.setOldColumnName(getNewColumnName()); |
135 | 0 | inverse.setNewColumnName(getOldColumnName()); |
136 | 0 | inverse.setColumnDataType(getColumnDataType()); |
137 | |
|
138 | 0 | return new Change[] { inverse }; |
139 | |
} |
140 | |
|
141 | |
public String getConfirmationMessage() { |
142 | 1 | return "Column " + tableName + "." + oldColumnName + " renamed to " + newColumnName; |
143 | |
} |
144 | |
|
145 | |
} |