drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 1 | # 2004 November 10 |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 2 | # |
drh | b6c2989 | 2004-11-22 19:12:19 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
| 5 | # |
| 6 | # May you do good and not evil. |
| 7 | # May you find forgiveness for yourself and forgive others. |
| 8 | # May you share freely, never taking more than you give. |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 9 | # |
| 10 | #************************************************************************* |
| 11 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this script is testing the ALTER TABLE statement. |
| 13 | # |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 14 | # $Id: alter.test,v 1.32 2009/03/24 15:08:10 drh Exp $ |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 15 | # |
| 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
| 19 | |
danielk1977 | 1c8c23c | 2004-11-12 15:53:37 +0000 | [diff] [blame] | 20 | # If SQLITE_OMIT_ALTERTABLE is defined, omit this file. |
| 21 | ifcapable !altertable { |
| 22 | finish_test |
| 23 | return |
| 24 | } |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 25 | |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame] | 26 | #---------------------------------------------------------------------- |
| 27 | # Test organization: |
| 28 | # |
| 29 | # alter-1.1.* - alter-1.7.*: Basic tests of ALTER TABLE, including tables |
| 30 | # with implicit and explicit indices. These tests came from an earlier |
| 31 | # fork of SQLite that also supported ALTER TABLE. |
| 32 | # alter-1.8.*: Tests for ALTER TABLE when the table resides in an |
| 33 | # attached database. |
| 34 | # alter-1.9.*: Tests for ALTER TABLE when their is whitespace between the |
| 35 | # table name and left parenthesis token. i.e: |
| 36 | # "CREATE TABLE abc (a, b, c);" |
| 37 | # alter-2.*: Test error conditions and messages. |
| 38 | # alter-3.*: Test ALTER TABLE on tables that have TRIGGERs attached to them. |
danielk1977 | aacd732 | 2004-11-19 08:02:14 +0000 | [diff] [blame] | 39 | # alter-4.*: Test ALTER TABLE on tables that have AUTOINCREMENT fields. |
danielk1977 | 61116ae | 2007-12-13 08:15:30 +0000 | [diff] [blame] | 40 | # ... |
| 41 | # alter-12.*: Test ALTER TABLE on views. |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame] | 42 | # |
| 43 | |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 44 | # Create some tables to rename. Be sure to include some TEMP tables |
| 45 | # and some tables with odd names. |
| 46 | # |
| 47 | do_test alter-1.1 { |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 48 | ifcapable tempdb { |
| 49 | set ::temp TEMP |
| 50 | } else { |
| 51 | set ::temp {} |
| 52 | } |
| 53 | execsql [subst -nocommands { |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 54 | CREATE TABLE t1(a,b); |
| 55 | INSERT INTO t1 VALUES(1,2); |
| 56 | CREATE TABLE [t1'x1](c UNIQUE, b PRIMARY KEY); |
| 57 | INSERT INTO [t1'x1] VALUES(3,4); |
| 58 | CREATE INDEX t1i1 ON T1(B); |
| 59 | CREATE INDEX t1i2 ON t1(a,b); |
| 60 | CREATE INDEX i3 ON [t1'x1](b,c); |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 61 | CREATE $::temp TABLE "temp table"(e,f,g UNIQUE); |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 62 | CREATE INDEX i2 ON [temp table](f); |
| 63 | INSERT INTO [temp table] VALUES(5,6,7); |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 64 | }] |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 65 | execsql { |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 66 | SELECT 't1', * FROM t1; |
| 67 | SELECT 't1''x1', * FROM "t1'x1"; |
| 68 | SELECT * FROM [temp table]; |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 69 | } |
| 70 | } {t1 1 2 t1'x1 3 4 5 6 7} |
| 71 | do_test alter-1.2 { |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 72 | execsql [subst { |
| 73 | CREATE $::temp TABLE objlist(type, name, tbl_name); |
| 74 | INSERT INTO objlist SELECT type, name, tbl_name |
| 75 | FROM sqlite_master WHERE NAME!='objlist'; |
| 76 | }] |
| 77 | ifcapable tempdb { |
| 78 | execsql { |
| 79 | INSERT INTO objlist SELECT type, name, tbl_name |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 80 | FROM temp.sqlite_master WHERE NAME!='objlist'; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 84 | execsql { |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 85 | SELECT type, name, tbl_name FROM objlist ORDER BY tbl_name, type desc, name; |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 86 | } |
| 87 | } [list \ |
| 88 | table t1 t1 \ |
| 89 | index t1i1 t1 \ |
| 90 | index t1i2 t1 \ |
| 91 | table t1'x1 t1'x1 \ |
| 92 | index i3 t1'x1 \ |
| 93 | index {sqlite_autoindex_t1'x1_1} t1'x1 \ |
| 94 | index {sqlite_autoindex_t1'x1_2} t1'x1 \ |
| 95 | table {temp table} {temp table} \ |
| 96 | index i2 {temp table} \ |
| 97 | index {sqlite_autoindex_temp table_1} {temp table} \ |
| 98 | ] |
| 99 | |
| 100 | # Make some changes |
| 101 | # |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 102 | integrity_check alter-1.3.0 |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 103 | do_test alter-1.3 { |
| 104 | execsql { |
| 105 | ALTER TABLE [T1] RENAME to [-t1-]; |
| 106 | ALTER TABLE "t1'x1" RENAME TO T2; |
| 107 | ALTER TABLE [temp table] RENAME to TempTab; |
| 108 | } |
| 109 | } {} |
| 110 | integrity_check alter-1.3.1 |
| 111 | do_test alter-1.4 { |
| 112 | execsql { |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 113 | SELECT 't1', * FROM [-t1-]; |
| 114 | SELECT 't2', * FROM t2; |
| 115 | SELECT * FROM temptab; |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 116 | } |
| 117 | } {t1 1 2 t2 3 4 5 6 7} |
| 118 | do_test alter-1.5 { |
| 119 | execsql { |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 120 | DELETE FROM objlist; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 121 | INSERT INTO objlist SELECT type, name, tbl_name |
| 122 | FROM sqlite_master WHERE NAME!='objlist'; |
| 123 | } |
| 124 | catchsql { |
| 125 | INSERT INTO objlist SELECT type, name, tbl_name |
| 126 | FROM sqlite_temp_master WHERE NAME!='objlist'; |
| 127 | } |
| 128 | execsql { |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 129 | SELECT type, name, tbl_name FROM objlist ORDER BY tbl_name, type desc, name; |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 130 | } |
| 131 | } [list \ |
| 132 | table -t1- -t1- \ |
| 133 | index t1i1 -t1- \ |
| 134 | index t1i2 -t1- \ |
| 135 | table T2 T2 \ |
| 136 | index i3 T2 \ |
| 137 | index {sqlite_autoindex_T2_1} T2 \ |
| 138 | index {sqlite_autoindex_T2_2} T2 \ |
| 139 | table {TempTab} {TempTab} \ |
| 140 | index i2 {TempTab} \ |
| 141 | index {sqlite_autoindex_TempTab_1} {TempTab} \ |
| 142 | ] |
| 143 | |
| 144 | # Make sure the changes persist after restarting the database. |
| 145 | # (The TEMP table will not persist, of course.) |
| 146 | # |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 147 | ifcapable tempdb { |
| 148 | do_test alter-1.6 { |
| 149 | db close |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 150 | sqlite3 db test.db |
| 151 | set DB [sqlite3_connection_pointer db] |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 152 | execsql { |
| 153 | CREATE TEMP TABLE objlist(type, name, tbl_name); |
| 154 | INSERT INTO objlist SELECT type, name, tbl_name FROM sqlite_master; |
| 155 | INSERT INTO objlist |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 156 | SELECT type, name, tbl_name FROM temp.sqlite_master |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 157 | WHERE NAME!='objlist'; |
| 158 | SELECT type, name, tbl_name FROM objlist |
| 159 | ORDER BY tbl_name, type desc, name; |
| 160 | } |
| 161 | } [list \ |
| 162 | table -t1- -t1- \ |
| 163 | index t1i1 -t1- \ |
| 164 | index t1i2 -t1- \ |
| 165 | table T2 T2 \ |
| 166 | index i3 T2 \ |
| 167 | index {sqlite_autoindex_T2_1} T2 \ |
| 168 | index {sqlite_autoindex_T2_2} T2 \ |
| 169 | ] |
| 170 | } else { |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 171 | execsql { |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 172 | DROP TABLE TempTab; |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 173 | } |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 174 | } |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 175 | |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 176 | # Create bogus application-defined functions for functions used |
| 177 | # internally by ALTER TABLE, to ensure that ALTER TABLE falls back |
| 178 | # to the built-in functions. |
| 179 | # |
| 180 | proc failing_app_func {args} {error "bad function"} |
| 181 | do_test alter-1.7-prep { |
| 182 | db func substr failing_app_func |
| 183 | db func like failing_app_func |
| 184 | db func sqlite_rename_table failing_app_func |
| 185 | db func sqlite_rename_trigger failing_app_func |
| 186 | db func sqlite_rename_parent failing_app_func |
| 187 | catchsql {SELECT substr(name,1,3) FROM sqlite_master} |
| 188 | } {1 {bad function}} |
| 189 | |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 190 | # Make sure the ALTER TABLE statements work with the |
| 191 | # non-callback API |
| 192 | # |
| 193 | do_test alter-1.7 { |
| 194 | stepsql $DB { |
| 195 | ALTER TABLE [-t1-] RENAME to [*t1*]; |
| 196 | ALTER TABLE T2 RENAME TO [<t2>]; |
| 197 | } |
| 198 | execsql { |
danielk1977 | 27c7743 | 2004-11-22 13:35:41 +0000 | [diff] [blame] | 199 | DELETE FROM objlist; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 200 | INSERT INTO objlist SELECT type, name, tbl_name |
| 201 | FROM sqlite_master WHERE NAME!='objlist'; |
| 202 | } |
| 203 | catchsql { |
| 204 | INSERT INTO objlist SELECT type, name, tbl_name |
| 205 | FROM sqlite_temp_master WHERE NAME!='objlist'; |
| 206 | } |
| 207 | execsql { |
| 208 | SELECT type, name, tbl_name FROM objlist ORDER BY tbl_name, type desc, name; |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 209 | } |
| 210 | } [list \ |
| 211 | table *t1* *t1* \ |
| 212 | index t1i1 *t1* \ |
| 213 | index t1i2 *t1* \ |
| 214 | table <t2> <t2> \ |
| 215 | index i3 <t2> \ |
| 216 | index {sqlite_autoindex_<t2>_1} <t2> \ |
| 217 | index {sqlite_autoindex_<t2>_2} <t2> \ |
| 218 | ] |
| 219 | |
danielk1977 | 1c8c23c | 2004-11-12 15:53:37 +0000 | [diff] [blame] | 220 | # Check that ALTER TABLE works on attached databases. |
| 221 | # |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 222 | ifcapable attach { |
| 223 | do_test alter-1.8.1 { |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 224 | forcedelete test2.db |
| 225 | forcedelete test2.db-journal |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 226 | execsql { |
| 227 | ATTACH 'test2.db' AS aux; |
| 228 | } |
| 229 | } {} |
| 230 | do_test alter-1.8.2 { |
| 231 | execsql { |
| 232 | CREATE TABLE t4(a PRIMARY KEY, b, c); |
| 233 | CREATE TABLE aux.t4(a PRIMARY KEY, b, c); |
| 234 | CREATE INDEX i4 ON t4(b); |
| 235 | CREATE INDEX aux.i4 ON t4(b); |
| 236 | } |
| 237 | } {} |
| 238 | do_test alter-1.8.3 { |
| 239 | execsql { |
| 240 | INSERT INTO t4 VALUES('main', 'main', 'main'); |
| 241 | INSERT INTO aux.t4 VALUES('aux', 'aux', 'aux'); |
| 242 | SELECT * FROM t4 WHERE a = 'main'; |
| 243 | } |
| 244 | } {main main main} |
| 245 | do_test alter-1.8.4 { |
| 246 | execsql { |
| 247 | ALTER TABLE t4 RENAME TO t5; |
| 248 | SELECT * FROM t4 WHERE a = 'aux'; |
| 249 | } |
| 250 | } {aux aux aux} |
| 251 | do_test alter-1.8.5 { |
| 252 | execsql { |
| 253 | SELECT * FROM t5; |
| 254 | } |
| 255 | } {main main main} |
| 256 | do_test alter-1.8.6 { |
| 257 | execsql { |
| 258 | SELECT * FROM t5 WHERE b = 'main'; |
| 259 | } |
| 260 | } {main main main} |
| 261 | do_test alter-1.8.7 { |
| 262 | execsql { |
| 263 | ALTER TABLE aux.t4 RENAME TO t5; |
| 264 | SELECT * FROM aux.t5 WHERE b = 'aux'; |
| 265 | } |
| 266 | } {aux aux aux} |
| 267 | } |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 268 | |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame] | 269 | do_test alter-1.9.1 { |
| 270 | execsql { |
| 271 | CREATE TABLE tbl1 (a, b, c); |
| 272 | INSERT INTO tbl1 VALUES(1, 2, 3); |
| 273 | } |
| 274 | } {} |
| 275 | do_test alter-1.9.2 { |
| 276 | execsql { |
| 277 | SELECT * FROM tbl1; |
| 278 | } |
| 279 | } {1 2 3} |
| 280 | do_test alter-1.9.3 { |
| 281 | execsql { |
| 282 | ALTER TABLE tbl1 RENAME TO tbl2; |
| 283 | SELECT * FROM tbl2; |
| 284 | } |
| 285 | } {1 2 3} |
| 286 | do_test alter-1.9.4 { |
| 287 | execsql { |
| 288 | DROP TABLE tbl2; |
| 289 | } |
| 290 | } {} |
| 291 | |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 292 | # Test error messages |
| 293 | # |
| 294 | do_test alter-2.1 { |
| 295 | catchsql { |
| 296 | ALTER TABLE none RENAME TO hi; |
| 297 | } |
| 298 | } {1 {no such table: none}} |
| 299 | do_test alter-2.2 { |
| 300 | execsql { |
| 301 | CREATE TABLE t3(p,q,r); |
| 302 | } |
| 303 | catchsql { |
| 304 | ALTER TABLE [<t2>] RENAME TO t3; |
| 305 | } |
| 306 | } {1 {there is already another table or index with this name: t3}} |
| 307 | do_test alter-2.3 { |
| 308 | catchsql { |
| 309 | ALTER TABLE [<t2>] RENAME TO i3; |
| 310 | } |
| 311 | } {1 {there is already another table or index with this name: i3}} |
danielk1977 | 023f417 | 2004-11-19 08:41:34 +0000 | [diff] [blame] | 312 | do_test alter-2.4 { |
| 313 | catchsql { |
| 314 | ALTER TABLE SqLiTe_master RENAME TO master; |
| 315 | } |
| 316 | } {1 {table sqlite_master may not be altered}} |
| 317 | do_test alter-2.5 { |
| 318 | catchsql { |
| 319 | ALTER TABLE t3 RENAME TO sqlite_t3; |
| 320 | } |
| 321 | } {1 {object name reserved for internal use: sqlite_t3}} |
drh | 2a9abf6 | 2007-05-15 00:09:13 +0000 | [diff] [blame] | 322 | do_test alter-2.6 { |
| 323 | catchsql { |
| 324 | ALTER TABLE t3 ADD COLUMN (ALTER TABLE t3 ADD COLUMN); |
| 325 | } |
| 326 | } {1 {near "(": syntax error}} |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 327 | |
danielk1977 | aacd732 | 2004-11-19 08:02:14 +0000 | [diff] [blame] | 328 | # If this compilation does not include triggers, omit the alter-3.* tests. |
| 329 | ifcapable trigger { |
danielk1977 | 9fd2a9a | 2004-11-12 13:42:30 +0000 | [diff] [blame] | 330 | |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame] | 331 | #----------------------------------------------------------------------- |
| 332 | # Tests alter-3.* test ALTER TABLE on tables that have triggers. |
| 333 | # |
| 334 | # alter-3.1.*: ALTER TABLE with triggers. |
| 335 | # alter-3.2.*: Test that the ON keyword cannot be used as a database, |
| 336 | # table or column name unquoted. This is done because part of the |
| 337 | # ALTER TABLE code (specifically the implementation of SQL function |
| 338 | # "sqlite_alter_trigger") will break in this case. |
| 339 | # alter-3.3.*: ALTER TABLE with TEMP triggers (todo). |
| 340 | # |
| 341 | |
danielk1977 | d641d64 | 2004-11-18 15:44:29 +0000 | [diff] [blame] | 342 | # An SQL user-function for triggers to fire, so that we know they |
| 343 | # are working. |
| 344 | proc trigfunc {args} { |
| 345 | set ::TRIGGER $args |
| 346 | } |
| 347 | db func trigfunc trigfunc |
| 348 | |
| 349 | do_test alter-3.1.0 { |
| 350 | execsql { |
| 351 | CREATE TABLE t6(a, b, c); |
drh | 0152268 | 2012-02-01 01:13:10 +0000 | [diff] [blame] | 352 | -- Different case for the table name in the trigger. |
| 353 | CREATE TRIGGER trig1 AFTER INSERT ON T6 BEGIN |
danielk1977 | d641d64 | 2004-11-18 15:44:29 +0000 | [diff] [blame] | 354 | SELECT trigfunc('trig1', new.a, new.b, new.c); |
| 355 | END; |
| 356 | } |
| 357 | } {} |
| 358 | do_test alter-3.1.1 { |
| 359 | execsql { |
| 360 | INSERT INTO t6 VALUES(1, 2, 3); |
| 361 | } |
| 362 | set ::TRIGGER |
| 363 | } {trig1 1 2 3} |
| 364 | do_test alter-3.1.2 { |
| 365 | execsql { |
| 366 | ALTER TABLE t6 RENAME TO t7; |
| 367 | INSERT INTO t7 VALUES(4, 5, 6); |
| 368 | } |
| 369 | set ::TRIGGER |
| 370 | } {trig1 4 5 6} |
| 371 | do_test alter-3.1.3 { |
| 372 | execsql { |
| 373 | DROP TRIGGER trig1; |
| 374 | } |
| 375 | } {} |
| 376 | do_test alter-3.1.4 { |
| 377 | execsql { |
| 378 | CREATE TRIGGER trig2 AFTER INSERT ON main.t7 BEGIN |
| 379 | SELECT trigfunc('trig2', new.a, new.b, new.c); |
| 380 | END; |
| 381 | INSERT INTO t7 VALUES(1, 2, 3); |
| 382 | } |
| 383 | set ::TRIGGER |
| 384 | } {trig2 1 2 3} |
| 385 | do_test alter-3.1.5 { |
| 386 | execsql { |
| 387 | ALTER TABLE t7 RENAME TO t8; |
| 388 | INSERT INTO t8 VALUES(4, 5, 6); |
| 389 | } |
| 390 | set ::TRIGGER |
| 391 | } {trig2 4 5 6} |
| 392 | do_test alter-3.1.6 { |
| 393 | execsql { |
| 394 | DROP TRIGGER trig2; |
| 395 | } |
| 396 | } {} |
| 397 | do_test alter-3.1.7 { |
| 398 | execsql { |
| 399 | CREATE TRIGGER trig3 AFTER INSERT ON main.'t8'BEGIN |
| 400 | SELECT trigfunc('trig3', new.a, new.b, new.c); |
| 401 | END; |
| 402 | INSERT INTO t8 VALUES(1, 2, 3); |
| 403 | } |
| 404 | set ::TRIGGER |
| 405 | } {trig3 1 2 3} |
| 406 | do_test alter-3.1.8 { |
| 407 | execsql { |
| 408 | ALTER TABLE t8 RENAME TO t9; |
| 409 | INSERT INTO t9 VALUES(4, 5, 6); |
| 410 | } |
| 411 | set ::TRIGGER |
| 412 | } {trig3 4 5 6} |
| 413 | |
| 414 | # Make sure "ON" cannot be used as a database, table or column name without |
| 415 | # quoting. Otherwise the sqlite_alter_trigger() function might not work. |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 416 | forcedelete test3.db |
| 417 | forcedelete test3.db-journal |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 418 | ifcapable attach { |
| 419 | do_test alter-3.2.1 { |
| 420 | catchsql { |
| 421 | ATTACH 'test3.db' AS ON; |
| 422 | } |
| 423 | } {1 {near "ON": syntax error}} |
| 424 | do_test alter-3.2.2 { |
| 425 | catchsql { |
| 426 | ATTACH 'test3.db' AS 'ON'; |
| 427 | } |
| 428 | } {0 {}} |
| 429 | do_test alter-3.2.3 { |
| 430 | catchsql { |
| 431 | CREATE TABLE ON.t1(a, b, c); |
| 432 | } |
| 433 | } {1 {near "ON": syntax error}} |
| 434 | do_test alter-3.2.4 { |
| 435 | catchsql { |
| 436 | CREATE TABLE 'ON'.t1(a, b, c); |
| 437 | } |
| 438 | } {0 {}} |
| 439 | do_test alter-3.2.4 { |
| 440 | catchsql { |
| 441 | CREATE TABLE 'ON'.ON(a, b, c); |
| 442 | } |
| 443 | } {1 {near "ON": syntax error}} |
| 444 | do_test alter-3.2.5 { |
| 445 | catchsql { |
| 446 | CREATE TABLE 'ON'.'ON'(a, b, c); |
| 447 | } |
| 448 | } {0 {}} |
| 449 | } |
danielk1977 | d641d64 | 2004-11-18 15:44:29 +0000 | [diff] [blame] | 450 | do_test alter-3.2.6 { |
| 451 | catchsql { |
| 452 | CREATE TABLE t10(a, ON, c); |
| 453 | } |
| 454 | } {1 {near "ON": syntax error}} |
| 455 | do_test alter-3.2.7 { |
| 456 | catchsql { |
| 457 | CREATE TABLE t10(a, 'ON', c); |
| 458 | } |
| 459 | } {0 {}} |
| 460 | do_test alter-3.2.8 { |
| 461 | catchsql { |
| 462 | CREATE TRIGGER trig4 AFTER INSERT ON ON BEGIN SELECT 1; END; |
| 463 | } |
| 464 | } {1 {near "ON": syntax error}} |
danielk1977 | 5a8f937 | 2007-10-09 08:29:32 +0000 | [diff] [blame] | 465 | ifcapable attach { |
| 466 | do_test alter-3.2.9 { |
| 467 | catchsql { |
| 468 | CREATE TRIGGER 'on'.trig4 AFTER INSERT ON 'ON' BEGIN SELECT 1; END; |
| 469 | } |
| 470 | } {0 {}} |
| 471 | } |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame] | 472 | do_test alter-3.2.10 { |
| 473 | execsql { |
| 474 | DROP TABLE t10; |
| 475 | } |
| 476 | } {} |
danielk1977 | d641d64 | 2004-11-18 15:44:29 +0000 | [diff] [blame] | 477 | |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame] | 478 | do_test alter-3.3.1 { |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 479 | execsql [subst { |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame] | 480 | CREATE TABLE tbl1(a, b, c); |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 481 | CREATE $::temp TRIGGER trig1 AFTER INSERT ON tbl1 BEGIN |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame] | 482 | SELECT trigfunc('trig1', new.a, new.b, new.c); |
| 483 | END; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 484 | }] |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame] | 485 | } {} |
| 486 | do_test alter-3.3.2 { |
| 487 | execsql { |
| 488 | INSERT INTO tbl1 VALUES('a', 'b', 'c'); |
| 489 | } |
| 490 | set ::TRIGGER |
| 491 | } {trig1 a b c} |
| 492 | do_test alter-3.3.3 { |
| 493 | execsql { |
| 494 | ALTER TABLE tbl1 RENAME TO tbl2; |
| 495 | INSERT INTO tbl2 VALUES('d', 'e', 'f'); |
| 496 | } |
| 497 | set ::TRIGGER |
| 498 | } {trig1 d e f} |
| 499 | do_test alter-3.3.4 { |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 500 | execsql [subst { |
| 501 | CREATE $::temp TRIGGER trig2 AFTER UPDATE ON tbl2 BEGIN |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame] | 502 | SELECT trigfunc('trig2', new.a, new.b, new.c); |
| 503 | END; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 504 | }] |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame] | 505 | } {} |
| 506 | do_test alter-3.3.5 { |
| 507 | execsql { |
| 508 | ALTER TABLE tbl2 RENAME TO tbl3; |
| 509 | INSERT INTO tbl3 VALUES('g', 'h', 'i'); |
| 510 | } |
| 511 | set ::TRIGGER |
| 512 | } {trig1 g h i} |
| 513 | do_test alter-3.3.6 { |
| 514 | execsql { |
| 515 | UPDATE tbl3 SET a = 'G' where a = 'g'; |
| 516 | } |
| 517 | set ::TRIGGER |
| 518 | } {trig2 G h i} |
| 519 | do_test alter-3.3.7 { |
| 520 | execsql { |
| 521 | DROP TABLE tbl3; |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame] | 522 | } |
| 523 | } {} |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 524 | ifcapable tempdb { |
| 525 | do_test alter-3.3.8 { |
| 526 | execsql { |
drh | e0a04a3 | 2016-12-16 01:00:21 +0000 | [diff] [blame] | 527 | SELECT * FROM temp.sqlite_master WHERE type = 'trigger'; |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 528 | } |
| 529 | } {} |
| 530 | } |
danielk1977 | 343e926 | 2004-11-19 05:14:54 +0000 | [diff] [blame] | 531 | |
danielk1977 | aacd732 | 2004-11-19 08:02:14 +0000 | [diff] [blame] | 532 | } ;# ifcapable trigger |
| 533 | |
| 534 | # If the build does not include AUTOINCREMENT fields, omit alter-4.*. |
| 535 | ifcapable autoinc { |
| 536 | |
| 537 | do_test alter-4.1 { |
| 538 | execsql { |
| 539 | CREATE TABLE tbl1(a INTEGER PRIMARY KEY AUTOINCREMENT); |
| 540 | INSERT INTO tbl1 VALUES(10); |
| 541 | } |
| 542 | } {} |
| 543 | do_test alter-4.2 { |
| 544 | execsql { |
| 545 | INSERT INTO tbl1 VALUES(NULL); |
| 546 | SELECT a FROM tbl1; |
| 547 | } |
| 548 | } {10 11} |
| 549 | do_test alter-4.3 { |
| 550 | execsql { |
| 551 | ALTER TABLE tbl1 RENAME TO tbl2; |
| 552 | DELETE FROM tbl2; |
| 553 | INSERT INTO tbl2 VALUES(NULL); |
| 554 | SELECT a FROM tbl2; |
| 555 | } |
| 556 | } {12} |
danielk1977 | 81e9674 | 2005-01-27 00:30:52 +0000 | [diff] [blame] | 557 | do_test alter-4.4 { |
| 558 | execsql { |
| 559 | DROP TABLE tbl2; |
| 560 | } |
| 561 | } {} |
danielk1977 | aacd732 | 2004-11-19 08:02:14 +0000 | [diff] [blame] | 562 | |
| 563 | } ;# ifcapable autoinc |
| 564 | |
danielk1977 | 81e9674 | 2005-01-27 00:30:52 +0000 | [diff] [blame] | 565 | # Test that it is Ok to execute an ALTER TABLE immediately after |
| 566 | # opening a database. |
| 567 | do_test alter-5.1 { |
| 568 | execsql { |
| 569 | CREATE TABLE tbl1(a, b, c); |
| 570 | INSERT INTO tbl1 VALUES('x', 'y', 'z'); |
| 571 | } |
| 572 | } {} |
| 573 | do_test alter-5.2 { |
| 574 | sqlite3 db2 test.db |
| 575 | execsql { |
| 576 | ALTER TABLE tbl1 RENAME TO tbl2; |
| 577 | SELECT * FROM tbl2; |
| 578 | } db2 |
| 579 | } {x y z} |
| 580 | do_test alter-5.3 { |
| 581 | db2 close |
| 582 | } {} |
| 583 | |
danielk1977 | 819d7f4 | 2006-01-15 14:11:48 +0000 | [diff] [blame] | 584 | foreach tblname [execsql { |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 585 | SELECT name FROM sqlite_master |
| 586 | WHERE type='table' AND name NOT GLOB 'sqlite*' |
danielk1977 | 819d7f4 | 2006-01-15 14:11:48 +0000 | [diff] [blame] | 587 | }] { |
| 588 | execsql "DROP TABLE \"$tblname\"" |
| 589 | } |
| 590 | |
danielk1977 | 79f27df | 2006-01-13 18:06:40 +0000 | [diff] [blame] | 591 | set ::tbl_name "abc\uABCDdef" |
danielk1977 | 819d7f4 | 2006-01-15 14:11:48 +0000 | [diff] [blame] | 592 | do_test alter-6.1 { |
danielk1977 | 79f27df | 2006-01-13 18:06:40 +0000 | [diff] [blame] | 593 | string length $::tbl_name |
| 594 | } {7} |
danielk1977 | 819d7f4 | 2006-01-15 14:11:48 +0000 | [diff] [blame] | 595 | do_test alter-6.2 { |
danielk1977 | 79f27df | 2006-01-13 18:06:40 +0000 | [diff] [blame] | 596 | execsql " |
| 597 | CREATE TABLE ${tbl_name}(a, b, c); |
| 598 | " |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 599 | set ::oid [execsql {SELECT max(oid) FROM sqlite_master}] |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 600 | execsql " |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 601 | SELECT sql FROM sqlite_master WHERE oid = $::oid; |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 602 | " |
danielk1977 | 79f27df | 2006-01-13 18:06:40 +0000 | [diff] [blame] | 603 | } "{CREATE TABLE ${::tbl_name}(a, b, c)}" |
danielk1977 | 819d7f4 | 2006-01-15 14:11:48 +0000 | [diff] [blame] | 604 | execsql " |
| 605 | SELECT * FROM ${::tbl_name} |
| 606 | " |
danielk1977 | 79f27df | 2006-01-13 18:06:40 +0000 | [diff] [blame] | 607 | set ::tbl_name2 "abcXdef" |
danielk1977 | 819d7f4 | 2006-01-15 14:11:48 +0000 | [diff] [blame] | 608 | do_test alter-6.3 { |
danielk1977 | 79f27df | 2006-01-13 18:06:40 +0000 | [diff] [blame] | 609 | execsql " |
| 610 | ALTER TABLE $::tbl_name RENAME TO $::tbl_name2 |
| 611 | " |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 612 | execsql " |
| 613 | SELECT sql FROM sqlite_master WHERE oid = $::oid |
| 614 | " |
drh | 8e5b5f8 | 2008-02-09 14:30:29 +0000 | [diff] [blame] | 615 | } "{CREATE TABLE \"${::tbl_name2}\"(a, b, c)}" |
danielk1977 | 819d7f4 | 2006-01-15 14:11:48 +0000 | [diff] [blame] | 616 | do_test alter-6.4 { |
danielk1977 | 79f27df | 2006-01-13 18:06:40 +0000 | [diff] [blame] | 617 | execsql " |
| 618 | ALTER TABLE $::tbl_name2 RENAME TO $::tbl_name |
| 619 | " |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 620 | execsql " |
| 621 | SELECT sql FROM sqlite_master WHERE oid = $::oid |
| 622 | " |
drh | 8e5b5f8 | 2008-02-09 14:30:29 +0000 | [diff] [blame] | 623 | } "{CREATE TABLE \"${::tbl_name}\"(a, b, c)}" |
danielk1977 | 79f27df | 2006-01-13 18:06:40 +0000 | [diff] [blame] | 624 | set ::col_name ghi\1234\jkl |
danielk1977 | 819d7f4 | 2006-01-15 14:11:48 +0000 | [diff] [blame] | 625 | do_test alter-6.5 { |
danielk1977 | 79f27df | 2006-01-13 18:06:40 +0000 | [diff] [blame] | 626 | execsql " |
| 627 | ALTER TABLE $::tbl_name ADD COLUMN $::col_name VARCHAR |
| 628 | " |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 629 | execsql " |
| 630 | SELECT sql FROM sqlite_master WHERE oid = $::oid |
| 631 | " |
drh | 8e5b5f8 | 2008-02-09 14:30:29 +0000 | [diff] [blame] | 632 | } "{CREATE TABLE \"${::tbl_name}\"(a, b, c, $::col_name VARCHAR)}" |
danielk1977 | 79f27df | 2006-01-13 18:06:40 +0000 | [diff] [blame] | 633 | set ::col_name2 B\3421\A |
danielk1977 | 819d7f4 | 2006-01-15 14:11:48 +0000 | [diff] [blame] | 634 | do_test alter-6.6 { |
danielk1977 | 79f27df | 2006-01-13 18:06:40 +0000 | [diff] [blame] | 635 | db close |
| 636 | sqlite3 db test.db |
| 637 | execsql " |
| 638 | ALTER TABLE $::tbl_name ADD COLUMN $::col_name2 |
| 639 | " |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 640 | execsql " |
| 641 | SELECT sql FROM sqlite_master WHERE oid = $::oid |
| 642 | " |
drh | 8e5b5f8 | 2008-02-09 14:30:29 +0000 | [diff] [blame] | 643 | } "{CREATE TABLE \"${::tbl_name}\"(a, b, c, $::col_name VARCHAR, $::col_name2)}" |
danielk1977 | 819d7f4 | 2006-01-15 14:11:48 +0000 | [diff] [blame] | 644 | do_test alter-6.7 { |
danielk1977 | 79f27df | 2006-01-13 18:06:40 +0000 | [diff] [blame] | 645 | execsql " |
| 646 | INSERT INTO ${::tbl_name} VALUES(1, 2, 3, 4, 5); |
| 647 | SELECT $::col_name, $::col_name2 FROM $::tbl_name; |
| 648 | " |
| 649 | } {4 5} |
| 650 | |
drh | ff22e18 | 2006-02-09 02:56:02 +0000 | [diff] [blame] | 651 | # Ticket #1665: Make sure ALTER TABLE ADD COLUMN works on a table |
| 652 | # that includes a COLLATE clause. |
| 653 | # |
dan | 33f5379 | 2011-05-05 19:44:22 +0000 | [diff] [blame] | 654 | do_realnum_test alter-7.1 { |
drh | ff22e18 | 2006-02-09 02:56:02 +0000 | [diff] [blame] | 655 | execsql { |
| 656 | CREATE TABLE t1(a TEXT COLLATE BINARY); |
| 657 | ALTER TABLE t1 ADD COLUMN b INTEGER COLLATE NOCASE; |
drh | 05f7c19 | 2007-04-06 02:32:33 +0000 | [diff] [blame] | 658 | INSERT INTO t1 VALUES(1,'-2'); |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 659 | INSERT INTO t1 VALUES(5.4e-08,'5.4e-08'); |
drh | ff22e18 | 2006-02-09 02:56:02 +0000 | [diff] [blame] | 660 | SELECT typeof(a), a, typeof(b), b FROM t1; |
| 661 | } |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 662 | } {text 1 integer -2 text 5.4e-08 real 5.4e-08} |
danielk1977 | 79f27df | 2006-01-13 18:06:40 +0000 | [diff] [blame] | 663 | |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 664 | # Make sure that when a column is added by ALTER TABLE ADD COLUMN and has |
| 665 | # a default value that the default value is used by aggregate functions. |
| 666 | # |
| 667 | do_test alter-8.1 { |
| 668 | execsql { |
| 669 | CREATE TABLE t2(a INTEGER); |
| 670 | INSERT INTO t2 VALUES(1); |
| 671 | INSERT INTO t2 VALUES(1); |
| 672 | INSERT INTO t2 VALUES(2); |
| 673 | ALTER TABLE t2 ADD COLUMN b INTEGER DEFAULT 9; |
| 674 | SELECT sum(b) FROM t2; |
| 675 | } |
| 676 | } {27} |
| 677 | do_test alter-8.2 { |
| 678 | execsql { |
| 679 | SELECT a, sum(b) FROM t2 GROUP BY a; |
| 680 | } |
| 681 | } {1 18 2 9} |
| 682 | |
danielk1977 | dce872b | 2007-05-08 12:37:45 +0000 | [diff] [blame] | 683 | #-------------------------------------------------------------------------- |
dan | 141e119 | 2018-08-31 18:23:53 +0000 | [diff] [blame] | 684 | # alter-9.X - Special test: Make sure the sqlite_rename_column() and |
danielk1977 | dce872b | 2007-05-08 12:37:45 +0000 | [diff] [blame] | 685 | # rename_table() functions do not crash when handed bad input. |
| 686 | # |
drh | 171c50e | 2020-01-01 15:43:30 +0000 | [diff] [blame] | 687 | sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db |
dan | 141e119 | 2018-08-31 18:23:53 +0000 | [diff] [blame] | 688 | do_test alter-9.1 { |
dan | b87a9a8 | 2018-09-01 20:23:28 +0000 | [diff] [blame] | 689 | execsql {SELECT SQLITE_RENAME_COLUMN(0,0,0,0,0,0,0,0,0)} |
dan | 141e119 | 2018-08-31 18:23:53 +0000 | [diff] [blame] | 690 | } {{}} |
| 691 | foreach {tn sql} { |
dan | 65372fa | 2018-09-03 20:05:15 +0000 | [diff] [blame] | 692 | 1 { SELECT SQLITE_RENAME_TABLE(0,0,0,0,0,0,0) } |
| 693 | 2 { SELECT SQLITE_RENAME_TABLE(10,20,30,40,50,60,70) } |
| 694 | 3 { SELECT SQLITE_RENAME_TABLE('foo','foo','foo','foo','foo','foo','foo') } |
dan | 141e119 | 2018-08-31 18:23:53 +0000 | [diff] [blame] | 695 | } { |
dan | 65372fa | 2018-09-03 20:05:15 +0000 | [diff] [blame] | 696 | do_test alter-9.2.$tn { |
| 697 | catch { execsql $sql } |
| 698 | } 1 |
danielk1977 | dce872b | 2007-05-08 12:37:45 +0000 | [diff] [blame] | 699 | } |
drh | 171c50e | 2020-01-01 15:43:30 +0000 | [diff] [blame] | 700 | sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db |
drh | eea8eb6 | 2018-11-26 18:09:15 +0000 | [diff] [blame] | 701 | |
| 702 | # If the INTERNAL_FUNCTIONS test-control is disabled (which is the default), |
| 703 | # then the sqlite_rename_table() SQL function is not accessible to ordinary SQL. |
| 704 | # |
| 705 | do_catchsql_test alter-9.3 { |
| 706 | SELECT sqlite_rename_table(0,0,0,0,0,0,0); |
| 707 | } {1 {no such function: sqlite_rename_table}} |
danielk1977 | dce872b | 2007-05-08 12:37:45 +0000 | [diff] [blame] | 708 | |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 709 | #------------------------------------------------------------------------ |
| 710 | # alter-10.X - Make sure ALTER TABLE works with multi-byte UTF-8 characters |
| 711 | # in the names. |
| 712 | # |
| 713 | do_test alter-10.1 { |
| 714 | execsql "CREATE TABLE xyz(x UNIQUE)" |
| 715 | execsql "ALTER TABLE xyz RENAME TO xyz\u1234abc" |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 716 | execsql {SELECT name FROM sqlite_master WHERE name GLOB 'xyz*'} |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 717 | } [list xyz\u1234abc] |
| 718 | do_test alter-10.2 { |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 719 | execsql {SELECT name FROM sqlite_master WHERE name GLOB 'sqlite_autoindex*'} |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 720 | } [list sqlite_autoindex_xyz\u1234abc_1] |
| 721 | do_test alter-10.3 { |
| 722 | execsql "ALTER TABLE xyz\u1234abc RENAME TO xyzabc" |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 723 | execsql {SELECT name FROM sqlite_master WHERE name GLOB 'xyz*'} |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 724 | } [list xyzabc] |
| 725 | do_test alter-10.4 { |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 726 | execsql {SELECT name FROM sqlite_master WHERE name GLOB 'sqlite_autoindex*'} |
drh | 4e5dd85 | 2007-05-15 03:56:49 +0000 | [diff] [blame] | 727 | } [list sqlite_autoindex_xyzabc_1] |
| 728 | |
| 729 | do_test alter-11.1 { |
| 730 | sqlite3_exec db {CREATE TABLE t11(%c6%c6)} |
| 731 | execsql { |
| 732 | ALTER TABLE t11 ADD COLUMN abc; |
| 733 | } |
| 734 | catchsql { |
| 735 | ALTER TABLE t11 ADD COLUMN abc; |
| 736 | } |
| 737 | } {1 {duplicate column name: abc}} |
drh | 7e326c0 | 2007-05-15 16:51:37 +0000 | [diff] [blame] | 738 | set isutf16 [regexp 16 [db one {PRAGMA encoding}]] |
| 739 | if {!$isutf16} { |
| 740 | do_test alter-11.2 { |
| 741 | execsql {INSERT INTO t11 VALUES(1,2)} |
| 742 | sqlite3_exec db {SELECT %c6%c6 AS xyz, abc FROM t11} |
| 743 | } {0 {xyz abc 1 2}} |
| 744 | } |
drh | 9a087a9 | 2007-05-15 14:34:32 +0000 | [diff] [blame] | 745 | do_test alter-11.3 { |
drh | eab7f3f | 2007-05-15 09:00:14 +0000 | [diff] [blame] | 746 | sqlite3_exec db {CREATE TABLE t11b("%81%82%83" text)} |
| 747 | execsql { |
| 748 | ALTER TABLE t11b ADD COLUMN abc; |
| 749 | } |
| 750 | catchsql { |
| 751 | ALTER TABLE t11b ADD COLUMN abc; |
| 752 | } |
| 753 | } {1 {duplicate column name: abc}} |
drh | 7e326c0 | 2007-05-15 16:51:37 +0000 | [diff] [blame] | 754 | if {!$isutf16} { |
| 755 | do_test alter-11.4 { |
| 756 | execsql {INSERT INTO t11b VALUES(3,4)} |
| 757 | sqlite3_exec db {SELECT %81%82%83 AS xyz, abc FROM t11b} |
| 758 | } {0 {xyz abc 3 4}} |
| 759 | do_test alter-11.5 { |
| 760 | sqlite3_exec db {SELECT [%81%82%83] AS xyz, abc FROM t11b} |
| 761 | } {0 {xyz abc 3 4}} |
| 762 | do_test alter-11.6 { |
| 763 | sqlite3_exec db {SELECT "%81%82%83" AS xyz, abc FROM t11b} |
| 764 | } {0 {xyz abc 3 4}} |
| 765 | } |
drh | 9a087a9 | 2007-05-15 14:34:32 +0000 | [diff] [blame] | 766 | do_test alter-11.7 { |
| 767 | sqlite3_exec db {CREATE TABLE t11c(%81%82%83 text)} |
| 768 | execsql { |
| 769 | ALTER TABLE t11c ADD COLUMN abc; |
| 770 | } |
| 771 | catchsql { |
| 772 | ALTER TABLE t11c ADD COLUMN abc; |
| 773 | } |
| 774 | } {1 {duplicate column name: abc}} |
drh | 7e326c0 | 2007-05-15 16:51:37 +0000 | [diff] [blame] | 775 | if {!$isutf16} { |
| 776 | do_test alter-11.8 { |
| 777 | execsql {INSERT INTO t11c VALUES(5,6)} |
| 778 | sqlite3_exec db {SELECT %81%82%83 AS xyz, abc FROM t11c} |
| 779 | } {0 {xyz abc 5 6}} |
| 780 | do_test alter-11.9 { |
| 781 | sqlite3_exec db {SELECT [%81%82%83] AS xyz, abc FROM t11c} |
| 782 | } {0 {xyz abc 5 6}} |
| 783 | do_test alter-11.10 { |
| 784 | sqlite3_exec db {SELECT "%81%82%83" AS xyz, abc FROM t11c} |
| 785 | } {0 {xyz abc 5 6}} |
| 786 | } |
drh | 9a087a9 | 2007-05-15 14:34:32 +0000 | [diff] [blame] | 787 | |
danielk1977 | 61116ae | 2007-12-13 08:15:30 +0000 | [diff] [blame] | 788 | do_test alter-12.1 { |
| 789 | execsql { |
| 790 | CREATE TABLE t12(a, b, c); |
| 791 | CREATE VIEW v1 AS SELECT * FROM t12; |
| 792 | } |
| 793 | } {} |
| 794 | do_test alter-12.2 { |
| 795 | catchsql { |
| 796 | ALTER TABLE v1 RENAME TO v2; |
| 797 | } |
| 798 | } {1 {view v1 may not be altered}} |
| 799 | do_test alter-12.3 { |
| 800 | execsql { SELECT * FROM v1; } |
| 801 | } {} |
| 802 | do_test alter-12.4 { |
| 803 | db close |
| 804 | sqlite3 db test.db |
| 805 | execsql { SELECT * FROM v1; } |
| 806 | } {} |
| 807 | do_test alter-12.5 { |
| 808 | catchsql { |
| 809 | ALTER TABLE v1 ADD COLUMN new_column; |
| 810 | } |
| 811 | } {1 {Cannot add a column to a view}} |
| 812 | |
drh | 7382945 | 2008-05-09 14:17:51 +0000 | [diff] [blame] | 813 | # Ticket #3102: |
| 814 | # Verify that comments do not interfere with the table rename |
| 815 | # algorithm. |
| 816 | # |
| 817 | do_test alter-13.1 { |
| 818 | execsql { |
| 819 | CREATE TABLE /* hi */ t3102a(x); |
| 820 | CREATE TABLE t3102b -- comment |
| 821 | (y); |
| 822 | CREATE INDEX t3102c ON t3102a(x); |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 823 | SELECT name FROM sqlite_master WHERE name GLOB 't3102*' ORDER BY 1; |
drh | 7382945 | 2008-05-09 14:17:51 +0000 | [diff] [blame] | 824 | } |
| 825 | } {t3102a t3102b t3102c} |
| 826 | do_test alter-13.2 { |
| 827 | execsql { |
| 828 | ALTER TABLE t3102a RENAME TO t3102a_rename; |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 829 | SELECT name FROM sqlite_master WHERE name GLOB 't3102*' ORDER BY 1; |
drh | 7382945 | 2008-05-09 14:17:51 +0000 | [diff] [blame] | 830 | } |
| 831 | } {t3102a_rename t3102b t3102c} |
| 832 | do_test alter-13.3 { |
| 833 | execsql { |
| 834 | ALTER TABLE t3102b RENAME TO t3102b_rename; |
drh | 545f587 | 2010-04-24 14:02:59 +0000 | [diff] [blame] | 835 | SELECT name FROM sqlite_master WHERE name GLOB 't3102*' ORDER BY 1; |
drh | 7382945 | 2008-05-09 14:17:51 +0000 | [diff] [blame] | 836 | } |
| 837 | } {t3102a_rename t3102b_rename t3102c} |
drh | ff22e18 | 2006-02-09 02:56:02 +0000 | [diff] [blame] | 838 | |
drh | 0388123 | 2009-02-13 03:43:31 +0000 | [diff] [blame] | 839 | # Ticket #3651 |
| 840 | do_test alter-14.1 { |
| 841 | catchsql { |
| 842 | CREATE TABLE t3651(a UNIQUE); |
drh | 9e5fdc4 | 2020-05-08 19:02:21 +0000 | [diff] [blame] | 843 | INSERT INTO t3651 VALUES(5); |
drh | 0388123 | 2009-02-13 03:43:31 +0000 | [diff] [blame] | 844 | ALTER TABLE t3651 ADD COLUMN b UNIQUE; |
| 845 | } |
| 846 | } {1 {Cannot add a UNIQUE column}} |
| 847 | do_test alter-14.2 { |
| 848 | catchsql { |
| 849 | ALTER TABLE t3651 ADD COLUMN b PRIMARY KEY; |
| 850 | } |
| 851 | } {1 {Cannot add a PRIMARY KEY column}} |
| 852 | |
| 853 | |
dan | be53500 | 2011-04-01 15:15:58 +0000 | [diff] [blame] | 854 | #------------------------------------------------------------------------- |
| 855 | # Test that it is not possible to use ALTER TABLE on any system table. |
| 856 | # |
| 857 | set system_table_list {1 sqlite_master} |
| 858 | catchsql ANALYZE |
| 859 | ifcapable analyze { lappend system_table_list 2 sqlite_stat1 } |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 860 | ifcapable stat4 { lappend system_table_list 4 sqlite_stat4 } |
dan | be53500 | 2011-04-01 15:15:58 +0000 | [diff] [blame] | 861 | |
| 862 | foreach {tn tbl} $system_table_list { |
| 863 | do_test alter-15.$tn.1 { |
| 864 | catchsql "ALTER TABLE $tbl RENAME TO xyz" |
| 865 | } [list 1 "table $tbl may not be altered"] |
| 866 | |
| 867 | do_test alter-15.$tn.2 { |
| 868 | catchsql "ALTER TABLE $tbl ADD COLUMN xyz" |
| 869 | } [list 1 "table $tbl may not be altered"] |
| 870 | } |
| 871 | |
drh | 81eba73 | 2013-10-19 23:31:56 +0000 | [diff] [blame] | 872 | #------------------------------------------------------------------------ |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 873 | # Verify that ALTER TABLE works on tables with the WITHOUT rowid option. |
drh | 81eba73 | 2013-10-19 23:31:56 +0000 | [diff] [blame] | 874 | # |
| 875 | do_execsql_test alter-16.1 { |
drh | 5969da4 | 2013-10-21 02:14:45 +0000 | [diff] [blame] | 876 | CREATE TABLE t16a(a TEXT, b REAL, c INT, PRIMARY KEY(a,b)) WITHOUT rowid; |
drh | 81eba73 | 2013-10-19 23:31:56 +0000 | [diff] [blame] | 877 | INSERT INTO t16a VALUES('abc',1.25,99); |
| 878 | ALTER TABLE t16a ADD COLUMN d TEXT DEFAULT 'xyzzy'; |
| 879 | INSERT INTO t16a VALUES('cba',5.5,98,'fizzle'); |
| 880 | SELECT * FROM t16a ORDER BY a; |
| 881 | } {abc 1.25 99 xyzzy cba 5.5 98 fizzle} |
| 882 | do_execsql_test alter-16.2 { |
| 883 | ALTER TABLE t16a RENAME TO t16a_rn; |
| 884 | SELECT * FROM t16a_rn ORDER BY a; |
| 885 | } {abc 1.25 99 xyzzy cba 5.5 98 fizzle} |
dan | be53500 | 2011-04-01 15:15:58 +0000 | [diff] [blame] | 886 | |
drh | 95f78d9 | 2018-09-16 23:27:37 +0000 | [diff] [blame] | 887 | # 2018-09-16 ticket b41031ea2b5372378cb3d2d43cf9fe2a4a5c2510 |
| 888 | # |
| 889 | ifcapable rtree { |
| 890 | db close |
| 891 | sqlite3 db :memory: |
| 892 | do_execsql_test alter-17.100 { |
| 893 | CREATE TABLE t1(a INTEGER PRIMARY KEY, b); |
| 894 | CREATE VIRTUAL TABLE t2 USING rtree(id,x0,x1); |
| 895 | INSERT INTO t1 VALUES(1,'apple'),(2,'fig'),(3,'pear'); |
| 896 | INSERT INTO t2 VALUES(1,1.0,2.0),(2,2.0,3.0),(3,1.5,3.5); |
| 897 | CREATE TRIGGER r1 AFTER UPDATE ON t1 BEGIN |
| 898 | DELETE FROM t2 WHERE id = OLD.a; |
| 899 | END; |
| 900 | ALTER TABLE t1 RENAME TO t3; |
| 901 | UPDATE t3 SET b='peach' WHERE a=2; |
| 902 | SELECT * FROM t2 ORDER BY 1; |
| 903 | } {1 1.0 2.0 3 1.5 3.5} |
| 904 | } |
dan | 141e119 | 2018-08-31 18:23:53 +0000 | [diff] [blame] | 905 | |
drh | 95f78d9 | 2018-09-16 23:27:37 +0000 | [diff] [blame] | 906 | finish_test |