alter.test 22 KB

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