@@ -33,6 +33,8 @@ public class AlterTableTest extends PLTestBase {
33
33
private static final String SQL_RENAME_COLUMN =
34
34
"ALTER TABLE foo RENAME year to month;" ;
35
35
36
+ private static final double EPSILON = 1e-6 ;
37
+
36
38
37
39
@ Rule
38
40
public ExpectedException thrown = ExpectedException .none ();
@@ -195,18 +197,31 @@ public void test_RenameCol_Concurrent() throws SQLException {
195
197
// }
196
198
197
199
/**
198
- * Add a column to the table.
200
+ * Add a column to the table, and do some insertion .
199
201
*/
200
202
@ Test
201
203
public void test_AddCol_Basic () throws SQLException {
202
- String sql = "ALTER TABLE foo add month int;" ;
204
+ String sql = "ALTER TABLE foo ADD month int;" ;
203
205
conn .createStatement ().execute (sql );
204
206
ResultSet rs = conn .createStatement ().executeQuery (SQL_SELECT_STAR );
205
207
rs .next ();
206
208
checkRow (rs ,
207
209
new String [] {"id" , "year" , "month" },
208
210
new int [] {5 , 400 , 0 });
209
211
assertNoMoreRows (rs );
212
+
213
+ String sql2 = "INSERT INTO foo VALUES (6, 500, 1);" ;
214
+ conn .createStatement ().execute (sql2 );
215
+ rs = conn .createStatement ().executeQuery (SQL_SELECT_STAR );
216
+ rs .next ();
217
+ checkRow (rs ,
218
+ new String [] {"id" , "year" , "month" },
219
+ new int [] {5 , 400 , 0 });
220
+ rs .next ();
221
+ checkRow (rs ,
222
+ new String [] {"id" , "year" , "month" },
223
+ new int [] {6 , 500 , 1 });
224
+ assertNoMoreRows (rs );
210
225
}
211
226
212
227
/**
@@ -234,6 +249,19 @@ public void test_DropCol_Basic() throws SQLException {
234
249
new String [] {"id" },
235
250
new int [] {5 });
236
251
assertNoMoreRows (rs );
252
+
253
+ String sql2 = "INSERT INTO foo VALUES (6);" ;
254
+ conn .createStatement ().execute (sql2 );
255
+ rs = conn .createStatement ().executeQuery (SQL_SELECT_STAR );
256
+ rs .next ();
257
+ checkRow (rs ,
258
+ new String [] {"id" },
259
+ new int [] {5 });
260
+ rs .next ();
261
+ checkRow (rs ,
262
+ new String [] {"id" },
263
+ new int [] {6 });
264
+ assertNoMoreRows (rs );
237
265
}
238
266
239
267
/**
@@ -258,21 +286,20 @@ public void test_AlterType_Basic() throws SQLException {
258
286
ResultSet rs = conn .createStatement ().executeQuery (SQL_SELECT_STAR );
259
287
rs .next ();
260
288
assertEquals (rs .getInt ("id" ), 5 );
261
- assertEquals (rs .getFloat ("year" ), 400 , 1e-3 );
289
+ assertEquals (rs .getFloat ("year" ), 400 , EPSILON );
262
290
assertNoMoreRows (rs );
263
291
264
292
String sql2 = "INSERT INTO foo VALUES (6, 3.5);" ;
265
293
conn .createStatement ().execute (sql2 );
266
294
rs = conn .createStatement ().executeQuery (SQL_SELECT_STAR );
267
295
rs .next ();
268
296
assertEquals (rs .getInt ("id" ), 5 );
269
- assertEquals (rs .getFloat ("year" ), 400 , 1e-3 );
297
+ assertEquals (rs .getFloat ("year" ), 400 , EPSILON );
270
298
rs .next ();
271
299
assertEquals (rs .getInt ("id" ), 6 );
272
- assertEquals (rs .getFloat ("year" ), 3.5 , 1e-3 );
300
+ assertEquals (rs .getFloat ("year" ), 3.5 , EPSILON );
273
301
assertNoMoreRows (rs );
274
302
275
-
276
303
String sql3 = "ALTER TABLE foo ALTER year TYPE int;" ;
277
304
conn .createStatement ().execute (sql3 );
278
305
rs = conn .createStatement ().executeQuery (SQL_SELECT_STAR );
@@ -285,7 +312,6 @@ public void test_AlterType_Basic() throws SQLException {
285
312
assertNoMoreRows (rs );
286
313
}
287
314
288
-
289
315
/**
290
316
* Alter column type from int to varchar and backwards.
291
317
*/
@@ -307,4 +333,70 @@ public void test_AlterType_Varchar() throws SQLException {
307
333
assertEquals (rs .getInt ("year" ), 400 );
308
334
assertNoMoreRows (rs );
309
335
}
336
+
337
+ /**
338
+ * Alter type to column that does not exist
339
+ */
340
+ @ Test
341
+ public void test_AlterType_NonExist () throws SQLException {
342
+ String sql = "ALTER TABLE foo ALTER a TYPE int;" ;
343
+
344
+ thrown .expect (PSQLException .class );
345
+ conn .createStatement ().execute (sql );
346
+ }
347
+
348
+ /**
349
+ * Alter to an unsupported column type
350
+ */
351
+ @ Test
352
+ public void test_AlterType_UnSupported () throws SQLException {
353
+ String sql = "ALTER TABLE foo ALTER year TYPE non;" ;
354
+ thrown .expect (PSQLException .class );
355
+ conn .createStatement ().execute (sql );
356
+ }
357
+
358
+ /**
359
+ * Add columns, drop columns, change column type in one sql statement
360
+ */
361
+ @ Test
362
+ public void test_MultiOperation () throws SQLException {
363
+ String sql =
364
+ "ALTER TABLE foo ADD month INT, DROP year, ALTER id TYPE float;" ;
365
+ conn .createStatement ().execute (sql );
366
+ ResultSet rs = conn .createStatement ().executeQuery (SQL_SELECT_STAR );
367
+ rs .next ();
368
+ assertEquals (rs .getFloat ("id" ), 5 , EPSILON );
369
+ assertEquals (rs .getInt ("month" ), 0 );
370
+ assertNoMoreRows (rs );
371
+
372
+ String sql2 = "INSERT INTO foo VALUES (4.5, 3);" ;
373
+ conn .createStatement ().execute (sql2 );
374
+ rs = conn .createStatement ().executeQuery (SQL_SELECT_STAR );
375
+ rs .next ();
376
+ assertEquals (rs .getFloat ("id" ), 5 , EPSILON );
377
+ assertEquals (rs .getInt ("month" ), 0 );
378
+ rs .next ();
379
+ assertEquals (rs .getFloat ("id" ), 4.5 , EPSILON );
380
+ assertEquals (rs .getInt ("month" ), 3 );
381
+ assertNoMoreRows (rs );
382
+ }
383
+
384
+ /**
385
+ * If multiple operations in one statement failed, schema will not change
386
+ */
387
+ @ Test
388
+ public void test_MultiOperationFailed () throws SQLException {
389
+ String sql =
390
+ "ALTER TABLE foo ADD month int, DROP month, ALTER year TYPE float;" ;
391
+
392
+ thrown .expect (PSQLException .class );
393
+ conn .createStatement ().execute (sql );
394
+
395
+ ResultSet rs = conn .createStatement ().executeQuery (SQL_SELECT_STAR );
396
+ rs .next ();
397
+ checkRow (rs ,
398
+ new String [] {"id" , "year" },
399
+ new int [] {5 , 400 });
400
+ assertNoMoreRows (rs );
401
+ }
310
402
}
0 commit comments