17
17
18
18
public class AlterTableTest extends PLTestBase {
19
19
private Connection conn ;
20
- private String s_sql = "SELECT * FROM foo;" ;
20
+ private Connection conn2 ;
21
21
22
22
private static final String SQL_DROP_TABLE =
23
23
"DROP TABLE IF EXISTS foo;" ;
@@ -27,6 +27,12 @@ public class AlterTableTest extends PLTestBase {
27
27
"id integer, " +
28
28
"year integer);" ;
29
29
30
+ private static final String SQL_SELECT_STAR = "SELECT * FROM foo;" ;
31
+
32
+ private static final String SQL_RENAME_COLUMN =
33
+ "ALTER TABLE foo RENAME year to month;" ;
34
+
35
+
30
36
@ Rule
31
37
public ExpectedException thrown = ExpectedException .none ();
32
38
@@ -37,12 +43,16 @@ private void InitDatabase() throws SQLException {
37
43
Statement stmt = conn .createStatement ();
38
44
stmt .execute (SQL_DROP_TABLE );
39
45
stmt .execute (SQL_CREATE_TABLE );
46
+
47
+ String sql = "INSERT INTO foo VALUES (5, 400);" ;
48
+ conn .createStatement ().execute (sql );
40
49
}
41
50
42
51
@ Before
43
52
public void Setup () throws SQLException {
44
53
conn = makeDefaultConnection ();
45
54
conn .setAutoCommit (true );
55
+ conn2 = makeDefaultConnection ();
46
56
InitDatabase ();
47
57
}
48
58
@@ -57,29 +67,17 @@ public void Teardown() throws SQLException {
57
67
*/
58
68
@ Test
59
69
public void test_RenameColumn_1 () throws SQLException {
60
- String sql_1 = "INSERT INTO foo VALUES (5, 400);" ;
61
- conn .createStatement ().execute (sql_1 );
62
-
63
- ResultSet rs_1 = conn .createStatement ().executeQuery (s_sql );
64
- rs_1 .next ();
65
- checkRow (rs_1 ,
66
- new String [] {"id" , "year" },
67
- new int [] {5 , 400 });
68
- assertNoMoreRows (rs_1 );
69
-
70
- String sql_2 = "ALTER TABLE foo RENAME year to month;" ;
71
- conn .createStatement ().execute (sql_2 );
72
- ResultSet rs_2 = conn .createStatement ().executeQuery (s_sql );
73
-
74
- rs_2 .next ();
75
- checkRow (rs_2 ,
70
+ conn .createStatement ().execute (SQL_RENAME_COLUMN );
71
+ ResultSet rs = conn .createStatement ().executeQuery (SQL_SELECT_STAR );
72
+ rs .next ();
73
+ checkRow (rs ,
76
74
new String [] {"id" , "month" },
77
75
new int [] {5 , 400 });
78
- assertNoMoreRows (rs_2 );
76
+ assertNoMoreRows (rs );
79
77
}
80
78
81
79
/**
82
- * Rename a column that does not exist , should throw exception
80
+ * Rename a column that does not exists , should throw exception
83
81
*/
84
82
@ Test
85
83
public void test_RenameColumn_2 () throws SQLException {
@@ -91,7 +89,7 @@ public void test_RenameColumn_2() throws SQLException {
91
89
}
92
90
93
91
/**
94
- * Rename a column to a name that already exists, should throw exception
92
+ * Rename a column that does not exists, should throw exception
95
93
*/
96
94
@ Test
97
95
public void test_RenameColumn_3 () throws SQLException {
@@ -102,4 +100,89 @@ public void test_RenameColumn_3() throws SQLException {
102
100
conn .createStatement ().execute (sql );
103
101
}
104
102
103
+ /**
104
+ * Two transactions try to rename at the same time, should throw exception
105
+ */
106
+ @ Test
107
+ public void test_RenameColumn_4 () throws SQLException {
108
+ conn .setAutoCommit (false );
109
+ conn2 .setAutoCommit (false );
110
+
111
+ conn .createStatement ().execute (SQL_RENAME_COLUMN );
112
+
113
+ thrown .expect (PSQLException .class );
114
+ conn2 .createStatement ().execute (SQL_RENAME_COLUMN );
115
+
116
+ conn .commit ();
117
+ conn2 .commit ();
118
+ }
119
+
120
+ // The following tests are currently broken.
121
+ // @Test
122
+ // public void test_RenameColumn_5() throws SQLException {
123
+ // conn.setAutoCommit(false);
124
+ // conn2.setAutoCommit(false);
125
+ //
126
+ // String sql = "ALTER TABLE foo RENAME year to month;";
127
+ // conn.createStatement().execute(sql);
128
+ //
129
+ // ResultSet rs = conn2.createStatement().executeQuery(SQL_SELECT_STAR);
130
+ // rs.next();
131
+ // checkRow(rs,
132
+ // new String [] {"id", "year"},
133
+ // new int [] {5, 400});
134
+ // assertNoMoreRows(rs);
135
+ //
136
+ // conn.commit();
137
+ // conn2.commit();
138
+ // }
139
+ //
140
+ // /**
141
+ // * 2 transactions, t2 read the table before and after t1 change the column
142
+ // * name and should not see the changes.
143
+ // */
144
+ // @Test
145
+ // public void test_RenameColumn_6() throws SQLException {
146
+ // conn.setAutoCommit(false);
147
+ // conn2.setAutoCommit(false);
148
+ //
149
+ // ResultSet rs_1 = conn2.createStatement().executeQuery(SQL_SELECT_STAR);
150
+ // rs_1.next();
151
+ // checkRow(rs_1,
152
+ // new String [] {"id", "year"},
153
+ // new int [] {5, 400});
154
+ // assertNoMoreRows(rs_1);
155
+ //
156
+ // conn.createStatement().execute(SQL_RENAME_COLUMN);
157
+ // conn.commit();
158
+ //
159
+ // ResultSet rs_2 = conn2.createStatement().executeQuery(SQL_SELECT_STAR);
160
+ // rs_2.next();
161
+ // checkRow(rs_2,
162
+ // new String [] {"id", "year"},
163
+ // new int [] {5, 400});
164
+ // assertNoMoreRows(rs_2);
165
+ // conn2.commit();
166
+ // }
167
+
168
+ // @Test
169
+ // public void test_RenameColumn_7() throws SQLException {
170
+ // conn.setAutoCommit(false);
171
+ // conn2.setAutoCommit(false);
172
+ //
173
+ // Statement alter_statement = conn.createStatement();
174
+ // Statement select_statement = conn2.createStatement();
175
+ //
176
+ // alter_statement.execute(SQL_RENAME_COLUMN);
177
+ // conn.commit();
178
+ //
179
+ // ResultSet rs = select_statement.executeQuery(SQL_SELECT_STAR);
180
+ // rs.next();
181
+ // checkRow(rs,
182
+ // new String [] {"id", "year"},
183
+ // new int [] {5, 400});
184
+ // assertNoMoreRows(rs);
185
+ // conn2.commit();
186
+ // }
187
+
105
188
}
0 commit comments