1 | |
package liquibase.change.core; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.HashSet; |
5 | |
import java.util.List; |
6 | |
import java.util.Set; |
7 | |
|
8 | |
import liquibase.change.AbstractChange; |
9 | |
import liquibase.change.Change; |
10 | |
import liquibase.change.ChangeMetaData; |
11 | |
import liquibase.change.ChangeWithColumns; |
12 | |
import liquibase.change.ColumnConfig; |
13 | |
import liquibase.database.Database; |
14 | |
import liquibase.database.core.DB2Database; |
15 | |
import liquibase.exception.ValidationErrors; |
16 | |
import liquibase.statement.AutoIncrementConstraint; |
17 | |
import liquibase.statement.ColumnConstraint; |
18 | |
import liquibase.statement.ForeignKeyConstraint; |
19 | |
import liquibase.statement.NotNullConstraint; |
20 | |
import liquibase.statement.PrimaryKeyConstraint; |
21 | |
import liquibase.statement.SqlStatement; |
22 | |
import liquibase.statement.UniqueConstraint; |
23 | |
import liquibase.statement.core.AddColumnStatement; |
24 | |
import liquibase.statement.core.ReorganizeTableStatement; |
25 | |
import liquibase.statement.core.UpdateStatement; |
26 | |
import liquibase.util.StringUtils; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class AddColumnChange extends AbstractChange implements ChangeWithColumns { |
32 | |
|
33 | |
private String schemaName; |
34 | |
private String tableName; |
35 | |
private List<ColumnConfig> columns; |
36 | |
|
37 | |
public AddColumnChange() { |
38 | 34 | super("addColumn", "Add Column", ChangeMetaData.PRIORITY_DEFAULT); |
39 | 34 | columns = new ArrayList<ColumnConfig>(); |
40 | 34 | } |
41 | |
|
42 | |
public String getSchemaName() { |
43 | 188 | return schemaName; |
44 | |
} |
45 | |
|
46 | |
public void setSchemaName(String schemaName) { |
47 | 10 | this.schemaName = StringUtils.trimToNull(schemaName); |
48 | 10 | } |
49 | |
|
50 | |
public String getTableName() { |
51 | 192 | return tableName; |
52 | |
} |
53 | |
|
54 | |
public void setTableName(String tableName) { |
55 | 18 | this.tableName = tableName; |
56 | 18 | } |
57 | |
|
58 | |
@Override |
59 | |
public List<ColumnConfig> getColumns() { |
60 | 158 | return columns; |
61 | |
} |
62 | |
|
63 | |
@Override |
64 | |
public void addColumn(ColumnConfig column) { |
65 | 24 | columns.add(column); |
66 | 24 | } |
67 | |
|
68 | |
public void removeColumn(ColumnConfig column) { |
69 | 3 | columns.remove(column); |
70 | 3 | } |
71 | |
|
72 | |
@Override |
73 | |
public ValidationErrors validate(Database database) { |
74 | 15 | ValidationErrors validationErrors = super.validate(database); |
75 | 15 | if (columns.size() == 0) { |
76 | 15 | validationErrors.addError("'columns' is required"); |
77 | |
} |
78 | 15 | return validationErrors; |
79 | |
} |
80 | |
|
81 | |
@Override |
82 | |
public SqlStatement[] generateStatements(Database database) { |
83 | |
|
84 | 150 | List<SqlStatement> sql = new ArrayList<SqlStatement>(); |
85 | |
|
86 | 150 | for (ColumnConfig aColumn : getColumns()) { |
87 | 120 | Set<ColumnConstraint> constraints = new HashSet<ColumnConstraint>(); |
88 | 120 | if (aColumn.getConstraints() != null) { |
89 | 120 | if (aColumn.getConstraints().isNullable() != null && !aColumn.getConstraints().isNullable()) { |
90 | 90 | constraints.add(new NotNullConstraint()); |
91 | |
} |
92 | 120 | if (aColumn.getConstraints().isUnique() != null && aColumn.getConstraints().isUnique()) { |
93 | 0 | constraints.add(new UniqueConstraint()); |
94 | |
} |
95 | 120 | if (aColumn.getConstraints().isPrimaryKey() != null && aColumn.getConstraints().isPrimaryKey()) { |
96 | 60 | constraints.add(new PrimaryKeyConstraint(aColumn.getConstraints().getPrimaryKeyName())); |
97 | |
} |
98 | |
|
99 | 120 | if (aColumn.getConstraints().getReferences() != null) { |
100 | 15 | constraints.add(new ForeignKeyConstraint(aColumn.getConstraints().getForeignKeyName(), aColumn |
101 | |
.getConstraints().getReferences())); |
102 | |
} |
103 | |
|
104 | |
} |
105 | |
|
106 | 120 | if (aColumn.isAutoIncrement() != null && aColumn.isAutoIncrement()) { |
107 | 30 | constraints.add(new AutoIncrementConstraint(aColumn.getName())); |
108 | |
} |
109 | |
|
110 | 120 | AddColumnStatement addColumnStatement = new AddColumnStatement(getSchemaName(), getTableName(), |
111 | |
aColumn.getName(), aColumn.getType(), aColumn.getDefaultValueObject(), |
112 | |
constraints.toArray(new ColumnConstraint[constraints.size()])); |
113 | |
|
114 | 120 | sql.add(addColumnStatement); |
115 | |
|
116 | 120 | if (database instanceof DB2Database) { |
117 | 8 | sql.add(new ReorganizeTableStatement(getSchemaName(), getTableName())); |
118 | |
} |
119 | |
|
120 | 120 | if (aColumn.getValueObject() != null) { |
121 | 15 | UpdateStatement updateStatement = new UpdateStatement(getSchemaName(), getTableName()); |
122 | 15 | updateStatement.addNewColumnValue(aColumn.getName(), aColumn.getValueObject()); |
123 | 15 | sql.add(updateStatement); |
124 | |
} |
125 | 120 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | 150 | return sql.toArray(new SqlStatement[sql.size()]); |
141 | |
} |
142 | |
|
143 | |
@Override |
144 | |
protected Change[] createInverses() { |
145 | 30 | List<Change> inverses = new ArrayList<Change>(); |
146 | |
|
147 | 30 | for (ColumnConfig aColumn : columns) { |
148 | 45 | if (aColumn.hasDefaultValue()) { |
149 | 0 | DropDefaultValueChange dropChange = new DropDefaultValueChange(); |
150 | 0 | dropChange.setTableName(getTableName()); |
151 | 0 | dropChange.setColumnName(aColumn.getName()); |
152 | |
|
153 | 0 | inverses.add(dropChange); |
154 | |
} |
155 | |
|
156 | 45 | DropColumnChange inverse = new DropColumnChange(); |
157 | 45 | inverse.setSchemaName(getSchemaName()); |
158 | 45 | inverse.setColumnName(aColumn.getName()); |
159 | 45 | inverse.setTableName(getTableName()); |
160 | 45 | inverses.add(inverse); |
161 | 45 | } |
162 | |
|
163 | 30 | return inverses.toArray(new Change[inverses.size()]); |
164 | |
} |
165 | |
|
166 | |
@Override |
167 | |
public String getConfirmationMessage() { |
168 | 1 | List<String> names = new ArrayList<String>(columns.size()); |
169 | 1 | for (ColumnConfig col : columns) { |
170 | 1 | names.add(col.getName() + "(" + col.getType() + ")"); |
171 | |
} |
172 | |
|
173 | 1 | return "Columns " + StringUtils.join(names, ",") + " added to " + tableName; |
174 | |
} |
175 | |
} |