-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
AddAutoIncrementExecuteTest.java
229 lines (215 loc) · 12.5 KB
/
AddAutoIncrementExecuteTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package liquibase.statementexecute;
import liquibase.database.Database;
import liquibase.database.core.*;
import liquibase.datatype.DataTypeFactory;
import liquibase.dbtest.AbstractIntegrationTest;
import liquibase.statement.ColumnConstraint;
import liquibase.statement.NotNullConstraint;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.AddAutoIncrementStatement;
import liquibase.statement.core.CreateTableStatement;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class AddAutoIncrementExecuteTest extends AbstractExecuteTest {
protected static final String TABLE_NAME = "table_name";
protected static final String COLUMN_NAME = "column_name";
@Override
protected List<? extends SqlStatement> setupStatements(Database database) {
ArrayList<CreateTableStatement> statements = new ArrayList<CreateTableStatement>();
CreateTableStatement table = new CreateTableStatement(null, null, TABLE_NAME);
if (database instanceof MySQLDatabase) {
table.addPrimaryKeyColumn("id", DataTypeFactory.getInstance().fromDescription("int", database), null, "pk_", null);
} else {
table.addColumn("id", DataTypeFactory.getInstance().fromDescription("int", database), null, new ColumnConstraint[]{new NotNullConstraint()});
}
statements.add(table);
// if (database.supportsSchemas()) {
// table = new CreateTableStatement(AbstractIntegrationTest.ALT_CATALOG, AbstractIntegrationTest.ALT_SCHEMA, TABLE_NAME);
// table.addColumn("id", DataTypeFactory.getInstance().fromDescription("int", database), null, new ColumnConstraint[]{new NotNullConstraint()});
// statements.add(table);
// }
return statements;
}
@SuppressWarnings("unchecked")
@Test
public void noSchema() throws Exception {
this.statementUnderTest = new AddAutoIncrementStatement(null, null, TABLE_NAME, COLUMN_NAME, "int", null, null, null, null);
assertCorrect("alter table [table_name] modify column_name integer generated by default as identity", PostgresDatabase.class, CockroachDatabase.class, EnterpriseDBDatabase.class);
assertCorrect("alter table table_name modify column_name int auto_increment", MySQLDatabase.class);
assertCorrect("ALTER TABLE [table_name] ALTER COLUMN [column_name] SET GENERATED BY DEFAULT AS IDENTITY", DB2Database.class);
assertCorrect("alter table table_name alter column column_name int generated by default as identity", HsqlDatabase.class, H2Database.class);
assertCorrect("ALTER TABLE [table_name] MODIFY [column_name] serial", InformixDatabase.class);
assertCorrect("ALTER TABLE [table_name] ALTER [column_name] SET DEFAULT AUTOINCREMENT", SybaseASADatabase.class);
assertCorrect("ALTER TABLE [table_name] MODIFY [column_name] int identity", SybaseDatabase.class);
assertCorrect("ALTER TABLE [table_name] ALTER column [column_name] SET GENERATED BY DEFAULT AS IDENTITY", Db2zDatabase.class);
assertCorrect("ALTER TABLE [table_name] MODIFY [column_name] int GENERATED BY DEFAULT AS IDENTITY", Ingres9Database.class);
assertCorrectOnRest("ALTER TABLE [table_name] MODIFY [column_name] int AUTO_INCREMENT");
}
// protected void setupDatabase(Database database) throws Exception {
// dropAndCreateTable(new CreateTableStatement(null, TABLE_NAME).addColumn("existingCol", "int"), database);
// dropAndCreateTable(new CreateTableStatement(TestContext.ALT_SCHEMA, TABLE_NAME).addColumn("existingCol", "int"), database);
// }
//
// protected AddColumnStatement createGeneratorUnderTest() {
// return new AddColumnStatement(null, null, null, null, null);
// }
//
// @Test
// public void execute_stringDefault() throws Exception {
// new DatabaseTestTemplate().testOnAvailableDatabases(
// new SqlStatementDatabaseTest(null, new AddColumnStatement(null, TABLE_NAME, NEW_COLUMN_NAME, "varchar(50)", "new default")) {
// protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// assertNull(snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME));
// }
//
// protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// Column columnSnapshot = snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME);
// assertNotNull(columnSnapshot);
// assertEquals(NEW_COLUMN_NAME.toUpperCase(), columnSnapshot.getName().toUpperCase());
// assertEquals("varchar".toUpperCase(), columnSnapshot.getShortName().toUpperCase().replaceAll("VARCHAR2", "VARCHAR"));
// assertEquals(50, columnSnapshot.getColumnSize());
// assertEquals("new default", columnSnapshot.getDefaultValue());
//
// assertEquals(true, columnSnapshot.isNullable());
// }
// });
// }
//
// @Test
// public void execute_intDefault() throws Exception {
// new DatabaseTestTemplate().testOnAvailableDatabases(
// new SqlStatementDatabaseTest(null, new AddColumnStatement(null, TABLE_NAME, NEW_COLUMN_NAME, "int", 42)) {
// protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// assertNull(snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME));
// }
//
// protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// Column columnSnapshot = snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME);
// assertNotNull(columnSnapshot);
// assertEquals(NEW_COLUMN_NAME.toUpperCase(), columnSnapshot.getName().toUpperCase());
// if (snapshot.getDatabase() instanceof OracleDatabase) {
// assertEquals("NUMBER", columnSnapshot.getShortName().toUpperCase());
// } else {
// assertTrue(columnSnapshot.getShortName().toUpperCase().startsWith("INT"));
// }
// assertEquals(42, ((Number) columnSnapshot.getDefaultValue()).intValue());
//
// assertEquals(true, columnSnapshot.isNullable());
// }
//
// }
//
// );
// }
//
// @Test
// public void execute_floatDefault() throws Exception {
// new DatabaseTestTemplate().testOnAvailableDatabases(
// new SqlStatementDatabaseTest(null, new AddColumnStatement(null, TABLE_NAME, NEW_COLUMN_NAME, "float", 42.5)) {
// protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// assertNull(snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME));
// }
//
// protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// Column columnSnapshot = snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME);
// assertNotNull(columnSnapshot);
// assertEquals(NEW_COLUMN_NAME.toUpperCase(), columnSnapshot.getName().toUpperCase());
// assertEquals(new Double(42.5), new Double(((Number) columnSnapshot.getDefaultValue()).doubleValue()));
//
// assertEquals(true, columnSnapshot.isNullable());
// }
// });
// }
//
// @Test
// public void execute_notNull() throws Exception {
// new DatabaseTestTemplate().testOnAvailableDatabases(
// new SqlStatementDatabaseTest(null, new AddColumnStatement(null, TABLE_NAME, NEW_COLUMN_NAME, "int", 42, new NotNullConstraint())) {
// protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// assertNull(snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME));
// }
//
// protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// Column columnSnapshot = snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME);
// assertNotNull(columnSnapshot);
// assertEquals(false, columnSnapshot.isNullable());
// }
// }
//
// );
// }
//
// @Test
// public void execute_primaryKey_nonAutoIncrement() throws Exception {
// new DatabaseTestTemplate().testOnAvailableDatabases(
// new SqlStatementDatabaseTest(null, new AddColumnStatement(null, TABLE_NAME, NEW_COLUMN_NAME, "int", null, new PrimaryKeyConstraint())) {
//
// protected boolean expectedException(Database database, DatabaseException exception) {
// return (database instanceof DB2Database
// || database instanceof DerbyDatabase
// || database instanceof H2Database
// || database instanceof CacheDatabase);
// }
//
// protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// assertNull(snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME));
// }
//
// protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// Column columnSnapshot = snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME);
// assertNotNull(columnSnapshot);
// assertEquals(false, columnSnapshot.isNullable());
// assertTrue(columnSnapshot.isPrimaryKey());
// assertEquals(false, columnSnapshot.isAutoIncrement());
// }
// });
// }
//
// @Test
// public void execute_altSchema() throws Exception {
// new DatabaseTestTemplate().testOnAvailableDatabases(
// new SqlStatementDatabaseTest(TestContext.ALT_SCHEMA, new AddColumnStatement(TestContext.ALT_SCHEMA, TABLE_NAME, NEW_COLUMN_NAME, "varchar(50)", "new default")) {
// protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// assertNull(snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME));
// }
//
// protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// Column columnSnapshot = snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME);
// assertNotNull(columnSnapshot);
// assertEquals(NEW_COLUMN_NAME.toUpperCase(), columnSnapshot.getName().toUpperCase());
// assertEquals("new default", columnSnapshot.getDefaultValue());
//
// assertEquals(true, columnSnapshot.isNullable());
// }
//
// });
// }
//
// @Test
// public void execute_primaryKeyAutoIncrement() throws Exception {
// new DatabaseTestTemplate().testOnAvailableDatabases(
// new SqlStatementDatabaseTest(null, new AddColumnStatement(null, TABLE_NAME, NEW_COLUMN_NAME, "int", null, new PrimaryKeyConstraint(), new AutoIncrementConstraint())) {
//
// protected boolean expectedException(Database database, DatabaseException exception) {
// return (database instanceof DB2Database
// || database instanceof DerbyDatabase
// || database instanceof H2Database
// || database instanceof CacheDatabase
// || !database.supportsAutoIncrement());
// }
//
// protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// assertNull(snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME));
// }
//
// protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
// Column columnSnapshot = snapshot.getTable(TABLE_NAME).getColumn(NEW_COLUMN_NAME);
// assertNotNull(columnSnapshot);
// assertEquals(false, columnSnapshot.isNullable());
// assertTrue(columnSnapshot.isPrimaryKey());
// assertEquals(true, columnSnapshot.isAutoIncrement());
// }
// });
// }
}