File: Changes10_12.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (325 lines) | stat: -rw-r--r-- 14,177 bytes parent folder | download | duplicates (4)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*

Derby - Class org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_12

Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/
package org.apache.derbyTesting.functionTests.tests.upgradeTests;

import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.JDBC;


/**
 * Upgrade test cases for 10.12.
 */
public class Changes10_12 extends UpgradeChange
{

    //////////////////////////////////////////////////////////////////
    //
    // CONSTANTS
    //
    //////////////////////////////////////////////////////////////////

    private static  final   String  LANG_AI_CANNOT_MODIFY_AI = "42Z23";
    private static  final   String  LANG_NULL_INTO_NON_NULL = "23502";

    //////////////////////////////////////////////////////////////////
    //
    // CONSTRUCTOR
    //
    //////////////////////////////////////////////////////////////////

    public Changes10_12(String name) {
        super(name);
    }

    //////////////////////////////////////////////////////////////////
    //
    // JUnit BEHAVIOR
    //
    //////////////////////////////////////////////////////////////////

    /**
     * Return the suite of tests to test the changes made in 10.12.
     *
     * @param phase an integer that indicates the current phase in
     *              the upgrade test.
     * @return the test suite created.
     */
    public static Test suite(int phase) {
        return new BaseTestSuite(Changes10_12.class, "Upgrade test for 10.12");
    }

    //////////////////////////////////////////////////////////////////
    //
    // TESTS
    //
    //////////////////////////////////////////////////////////////////

    /**
     * DERBY-6414(Incorrect handling when using an UPDATE to SET an 
     *  identity column to DEFAULT)
     * Starting Derby 10.12, we support updating an identity column using
     *  the keyword DEFAULT on 10.11 and higher dbs. A 10.11 database in 
     *  soft upgrade mode can use this feature to update identity columns. 
     *  Database versions earlier than that will not be able to use this 
     *  feature. The reason for restricting the functionality to 10.11 and 
     *  higher dbs is that starting 10.11, we started using sequence generator 
     *  to create unique values for identity columns. Prior to that, we had 
     *  really old code to generate unique values. In order to keep the code 
     *  clean in 10.12, DERBY-6414 is fixed only for identity columns using 
     *  sequence generator to create the new ids.
     * @throws SQLException
     */
    public void testDerby6414UpdateIdentityColumn() throws SQLException {
        //10.0 release does not support "generated by default as 
        // identity" columns. 
        if (!oldAtLeast(10, 1)) return;
        
        Statement s = createStatement();
        switch (getPhase()) {
            case PH_CREATE:
            	//Create the necessary tables and show that update of
            	// identity columns is not supported in these older
            	// releases
                s.execute("create table t1_6414(a int, "+
                        "c int generated always as identity," +
                        "d char(3)," +
                        "e char(5)" +
                        ")");
                s.execute("insert into t1_6414(a) values "+
                        "(1), (2)");
                //Update of identity columns using DEFAULT keyword is not 
                // supported in pre-10.12 releases
                assertCompileError(LANG_AI_CANNOT_MODIFY_AI,
                        "update t1_6414 set e='ccccc', a=-a, c=default");

                s.execute("create table t2_6414(a int, "+
                        "c int generated by default as identity," +
                        "d char(3)," +
                        "e char(5)" +
                        ")");
                s.execute("insert into t2_6414(a,d,e,c) values "+
                        "(1,'aaa','aaaaa',1)");
                s.execute("insert into t2_6414(a,d,e,c) values "+
                		"(2,'bbb','bbbbb',default)");
                //Update of identity columns using DEFAULT keyword is not 
                // supported in pre-10.12 releases
                assertStatementError(LANG_NULL_INTO_NON_NULL,
                		s,
                        "update t2_6414 set e='ccccc', a=-a, c=default, d='ccc'");
                break;

            case PH_SOFT_UPGRADE:
            	if (!oldAtLeast(10, 11)) {
                	//If the soft upgrade is on a pre10.11 db, then update of
                	// identity column will still not be supported. This is
                	// because those releases do not use sequence generator
                	// to generate a new value for identity columns
                    assertCompileError(LANG_AI_CANNOT_MODIFY_AI,
                            "update t1_6414 set e='ccccc', a=-a, c=default");
                    assertStatementError(LANG_NULL_INTO_NON_NULL,
                    		s,
	                        "update t2_6414 set e='ccccc', a=-a, c=default, d='ccc'");
                } else {
                	//We are dealing with 10.11 and higher dbs. These dbs use 
                	// sequence generator to create the new identity value. On 
                	// such dbs, on soft upgrade, we allow update of identity
                	// column using DEFAULT keyword
                	s.execute("update t1_6414 set e='ccccc', a=-a, c=default");
                	JDBC.assertFullResultSet(
                			s.executeQuery("select * from t1_6414"),
                            new String[][]
                            {
                                { "-1", "3", null, "ccccc" },
                                { "-2", "4", null, "ccccc" },
                            }
                    );

                	s.execute(
                            "update t2_6414 set e='ccccc', a=-a, c=default, d='ccc'");
                	JDBC.assertFullResultSet(
                			s.executeQuery("select * from t2_6414"),
                            new String[][]
                            {
                                { "-1", "2", "ccc", "ccccc" },
                                { "-2", "3", "ccc", "ccccc" },
                            }
                    );
                }
                break;
            case PH_POST_SOFT_UPGRADE:
            	//We are back to the release where the db was created. Those 
            	// releases do not have fix for DERBY-6414 and hence the
            	// following UPDATE of identity columns using DEFAULT 
            	// keyword will fail.
                assertCompileError(LANG_AI_CANNOT_MODIFY_AI,
                        "update t1_6414 set e='ccccc', a=-a, c=default");
                assertStatementError(LANG_NULL_INTO_NON_NULL,
                		s,
                        "update t2_6414 set e='ccccc', a=-a, c=default, d='ccc'");
                break;
            case PH_HARD_UPGRADE:
            	s.execute("update t1_6414 set e='ccccc', a=-a, c=default");
                if (!oldAtLeast(10, 11)) 
                	JDBC.assertFullResultSet(
                			s.executeQuery("select * from t1_6414"),
                            new String[][]
                            {
                                { "-1", "3", null, "ccccc" },
                                { "-2", "4", null, "ccccc" },
                            }
                    );
                else
                	JDBC.assertFullResultSet(
                			s.executeQuery("select * from t1_6414"),
                            new String[][]
                            {
                	             { "1", "5", null, "ccccc" },
                	             { "2", "6", null, "ccccc" },
                            }
                    );

            	s.execute(
                        "update t2_6414 set e='ccccc', a=-a, c=default, d='ccc'");
                if (!oldAtLeast(10, 11)) 
                	JDBC.assertFullResultSet(
                			s.executeQuery("select * from t2_6414"),
                            new String[][]
                            {
                                { "-1", "2", "ccc", "ccccc" },
                                { "-2", "3", "ccc", "ccccc" },
                            }
                    );
                else
                	JDBC.assertFullResultSet(
                			s.executeQuery("select * from t2_6414"),
                            new String[][]
                            {
                                { "1", "4", "ccc", "ccccc" },
                                { "2", "5", "ccc", "ccccc" },
                            }
                    );                	
                break;
        }
    }

    /**
     * Test the addition of support for adding identity columns with
     * an ALTER TABLE statement. DERBY-3888.
     */
    public void testAlterTableAddIdentity() throws SQLException {
        Statement s = createStatement();

        String addIdToT1 = "alter table derby_3888_t1 add column "
                            + "id int generated always as identity";
        String addIdToT2 = "alter table derby_3888_t2 add column "
                            + "id int generated by default as identity";

        // GENERATED BY DEFAULT AS IDENTITY is not supported prior to
        // Derby 10.1.1.0 (DERBY-167). If we're running on an older version,
        // change the syntax to something it recognizes.
        if (!oldAtLeast(10, 1) &&
                (getPhase() == PH_CREATE ||
                 getPhase() == PH_POST_SOFT_UPGRADE)) {
            addIdToT2 = addIdToT2.replace("by default", "always");
        }

        switch (getPhase()) {
            case PH_CREATE:
                // The old version doesn't support adding identity columns
                // via ALTER TABLE, so expect failures.
                s.execute("create table derby_3888_t1(x int)");
                s.execute("insert into derby_3888_t1 values 1");
                assertCompileError("42601", addIdToT1);
                s.execute("create table derby_3888_t2(x int)");
                s.execute("insert into derby_3888_t2 values 1");
                assertCompileError("42601", addIdToT2);
                break;
            case PH_SOFT_UPGRADE:
                // In soft upgrade, adding identity columns is only supported
                // if the database format is 10.11 or higher (needs identity
                // columns backed by sequences, added in DERBY-6542).
                if (oldAtLeast(10, 11)) {
                    s.execute(addIdToT1);
                    s.execute("insert into derby_3888_t1(x) values 2");
                    JDBC.assertFullResultSet(
                        s.executeQuery("select * from derby_3888_t1 order by x"),
                        new String[][] {
                            { "1", "1" },
                            { "2", "2" },
                        });
                } else {
                    assertCompileError("XCL47", addIdToT1);
                    assertCompileError("XCL47", addIdToT2);
                    s.execute("insert into derby_3888_t1(x) values 2");
                }
                break;
            case PH_POST_SOFT_UPGRADE:
                s.execute("insert into derby_3888_t1(x) values 3");
                // The result depends on whether or not the identity column
                // was added in the soft upgrade phase.
                String[][] expectedRows = oldAtLeast(10, 11)
                        ? new String[][] {
                            { "1", "1" },
                            { "2", "2" },
                            { "3", "3" },
                        }
                        : new String[][] {{"1"}, {"2"}, {"3"}};
                JDBC.assertFullResultSet(
                    s.executeQuery("select * from derby_3888_t1 order by x"),
                    expectedRows);

                // Adding identity columns in the old version should fail.
                assertCompileError("42601", addIdToT2);
                break;
            case PH_HARD_UPGRADE:
                // Adding identity columns should work in hard upgrade.
                if (!oldAtLeast(10, 11)) {
                    // If the old version is older than 10.11, the identity
                    // column of T1 was not added in the soft upgrade phase.
                    // Add it now.
                    s.execute(addIdToT1);
                }
                s.execute(addIdToT2);
                s.execute("insert into derby_3888_t2(x) values 2");
                s.execute("insert into derby_3888_t1(x) values 4");
                JDBC.assertFullResultSet(
                    s.executeQuery("select * from derby_3888_t1 order by x"),
                    new String[][] {
                        { "1", "1" },
                        { "2", "2" },
                        { "3", "3" },
                        { "4", "4" },
                    });
                JDBC.assertFullResultSet(
                    s.executeQuery("select * from derby_3888_t2 order by x"),
                    new String[][] {
                        { "1", "1" },
                        { "2", "2" },
                    });
                s.execute("drop table derby_3888_t1");
                s.execute("drop table derby_3888_t2");
                break;
        };
    }
}