alter2.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. # 2005 February 18
  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 that SQLite can handle a subtle
  13. # file format change that may be used in the future to implement
  14. # "ALTER TABLE ... ADD COLUMN".
  15. #
  16. # $Id: alter2.test,v 1.14 2009/04/07 14:14:22 danielk1977 Exp $
  17. #
  18. set testdir [file dirname $argv0]
  19. source $testdir/tester.tcl
  20. # We have to have pragmas in order to do this test
  21. ifcapable {!pragma} return
  22. # Do not use a codec for tests in this file, as the database file is
  23. # manipulated directly using tcl scripts. See proc [set_file_format].
  24. #
  25. do_not_use_codec
  26. # The file format change affects the way row-records stored in tables (but
  27. # not indices) are interpreted. Before version 3.1.3, a row-record for a
  28. # table with N columns was guaranteed to contain exactly N fields. As
  29. # of version 3.1.3, the record may contain up to N fields. In this case
  30. # the M fields that are present are the values for the left-most M
  31. # columns. The (N-M) rightmost columns contain NULL.
  32. #
  33. # If any records in the database contain less fields than their table
  34. # has columns, then the file-format meta value should be set to (at least) 2.
  35. #
  36. # This procedure sets the value of the file-format in file 'test.db'
  37. # to $newval. Also, the schema cookie is incremented.
  38. #
  39. proc set_file_format {newval} {
  40. hexio_write test.db 44 [hexio_render_int32 $newval]
  41. set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
  42. incr schemacookie
  43. hexio_write test.db 40 [hexio_render_int32 $schemacookie]
  44. return {}
  45. }
  46. # This procedure returns the value of the file-format in file 'test.db'.
  47. #
  48. proc get_file_format {{fname test.db}} {
  49. return [hexio_get_int [hexio_read $fname 44 4]]
  50. }
  51. # This procedure sets the SQL statement stored for table $tbl in the
  52. # sqlite_master table of file 'test.db' to $sql. Also set the file format
  53. # to the supplied value. This is 2 if the added column has a default that is
  54. # NULL, or 3 otherwise.
  55. #
  56. proc alter_table {tbl sql {file_format 2}} {
  57. sqlite3 dbat test.db
  58. set s [string map {' ''} $sql]
  59. set t [string map {' ''} $tbl]
  60. dbat eval [subst {
  61. PRAGMA writable_schema = 1;
  62. UPDATE sqlite_master SET sql = '$s' WHERE name = '$t' AND type = 'table';
  63. PRAGMA writable_schema = 0;
  64. }]
  65. dbat close
  66. set_file_format 2
  67. }
  68. # Create bogus application-defined functions for functions used
  69. # internally by ALTER TABLE, to ensure that ALTER TABLE falls back
  70. # to the built-in functions.
  71. #
  72. proc failing_app_func {args} {error "bad function"}
  73. do_test alter2-1.0 {
  74. db func substr failing_app_func
  75. db func like failing_app_func
  76. db func sqlite_rename_table failing_app_func
  77. db func sqlite_rename_trigger failing_app_func
  78. db func sqlite_rename_parent failing_app_func
  79. catchsql {SELECT substr('abcdefg',1,3)}
  80. } {1 {bad function}}
  81. #-----------------------------------------------------------------------
  82. # Some basic tests to make sure short rows are handled.
  83. #
  84. do_test alter2-1.1 {
  85. execsql {
  86. CREATE TABLE abc(a, b);
  87. INSERT INTO abc VALUES(1, 2);
  88. INSERT INTO abc VALUES(3, 4);
  89. INSERT INTO abc VALUES(5, 6);
  90. }
  91. } {}
  92. do_test alter2-1.2 {
  93. # ALTER TABLE abc ADD COLUMN c;
  94. alter_table abc {CREATE TABLE abc(a, b, c);}
  95. } {}
  96. do_test alter2-1.3 {
  97. execsql {
  98. SELECT * FROM abc;
  99. }
  100. } {1 2 {} 3 4 {} 5 6 {}}
  101. do_test alter2-1.4 {
  102. execsql {
  103. UPDATE abc SET c = 10 WHERE a = 1;
  104. SELECT * FROM abc;
  105. }
  106. } {1 2 10 3 4 {} 5 6 {}}
  107. do_test alter2-1.5 {
  108. execsql {
  109. CREATE INDEX abc_i ON abc(c);
  110. }
  111. } {}
  112. do_test alter2-1.6 {
  113. execsql {
  114. SELECT c FROM abc ORDER BY c;
  115. }
  116. } {{} {} 10}
  117. do_test alter2-1.7 {
  118. execsql {
  119. SELECT * FROM abc WHERE c = 10;
  120. }
  121. } {1 2 10}
  122. do_test alter2-1.8 {
  123. execsql {
  124. SELECT sum(a), c FROM abc GROUP BY c;
  125. }
  126. } {8 {} 1 10}
  127. do_test alter2-1.9 {
  128. # ALTER TABLE abc ADD COLUMN d;
  129. alter_table abc {CREATE TABLE abc(a, b, c, d);}
  130. if {[permutation] == "prepare"} { db cache flush }
  131. execsql { SELECT * FROM abc; }
  132. execsql {
  133. UPDATE abc SET d = 11 WHERE c IS NULL AND a<4;
  134. SELECT * FROM abc;
  135. }
  136. } {1 2 10 {} 3 4 {} 11 5 6 {} {}}
  137. do_test alter2-1.10 {
  138. execsql {
  139. SELECT typeof(d) FROM abc;
  140. }
  141. } {null integer null}
  142. do_test alter2-1.99 {
  143. execsql {
  144. DROP TABLE abc;
  145. }
  146. } {}
  147. #-----------------------------------------------------------------------
  148. # Test that views work when the underlying table structure is changed.
  149. #
  150. ifcapable view {
  151. do_test alter2-2.1 {
  152. execsql {
  153. CREATE TABLE abc2(a, b, c);
  154. INSERT INTO abc2 VALUES(1, 2, 10);
  155. INSERT INTO abc2 VALUES(3, 4, NULL);
  156. INSERT INTO abc2 VALUES(5, 6, NULL);
  157. CREATE VIEW abc2_v AS SELECT * FROM abc2;
  158. SELECT * FROM abc2_v;
  159. }
  160. } {1 2 10 3 4 {} 5 6 {}}
  161. do_test alter2-2.2 {
  162. # ALTER TABLE abc ADD COLUMN d;
  163. alter_table abc2 {CREATE TABLE abc2(a, b, c, d);}
  164. execsql {
  165. SELECT * FROM abc2_v;
  166. }
  167. } {1 2 10 {} 3 4 {} {} 5 6 {} {}}
  168. do_test alter2-2.3 {
  169. execsql {
  170. DROP TABLE abc2;
  171. DROP VIEW abc2_v;
  172. }
  173. } {}
  174. }
  175. #-----------------------------------------------------------------------
  176. # Test that triggers work when a short row is copied to the old.*
  177. # trigger pseudo-table.
  178. #
  179. ifcapable trigger {
  180. do_test alter2-3.1 {
  181. execsql {
  182. CREATE TABLE abc3(a, b);
  183. CREATE TABLE blog(o, n);
  184. CREATE TRIGGER abc3_t AFTER UPDATE OF b ON abc3 BEGIN
  185. INSERT INTO blog VALUES(old.b, new.b);
  186. END;
  187. }
  188. } {}
  189. do_test alter2-3.2 {
  190. execsql {
  191. INSERT INTO abc3 VALUES(1, 4);
  192. UPDATE abc3 SET b = 2 WHERE b = 4;
  193. SELECT * FROM blog;
  194. }
  195. } {4 2}
  196. do_test alter2-3.3 {
  197. execsql {
  198. INSERT INTO abc3 VALUES(3, 4);
  199. INSERT INTO abc3 VALUES(5, 6);
  200. }
  201. alter_table abc3 {CREATE TABLE abc3(a, b, c);}
  202. execsql {
  203. SELECT * FROM abc3;
  204. }
  205. } {1 2 {} 3 4 {} 5 6 {}}
  206. do_test alter2-3.4 {
  207. execsql {
  208. UPDATE abc3 SET b = b*2 WHERE a<4;
  209. SELECT * FROM abc3;
  210. }
  211. } {1 4 {} 3 8 {} 5 6 {}}
  212. do_test alter2-3.5 {
  213. execsql {
  214. SELECT * FROM blog;
  215. }
  216. } {4 2 2 4 4 8}
  217. do_test alter2-3.6 {
  218. execsql {
  219. CREATE TABLE clog(o, n);
  220. CREATE TRIGGER abc3_t2 AFTER UPDATE OF c ON abc3 BEGIN
  221. INSERT INTO clog VALUES(old.c, new.c);
  222. END;
  223. UPDATE abc3 SET c = a*2;
  224. SELECT * FROM clog;
  225. }
  226. } {{} 2 {} 6 {} 10}
  227. } else {
  228. execsql { CREATE TABLE abc3(a, b); }
  229. }
  230. #---------------------------------------------------------------------
  231. # Check that an error occurs if the database is upgraded to a file
  232. # format that SQLite does not support (in this case 5). Note: The
  233. # file format is checked each time the schema is read, so changing the
  234. # file format requires incrementing the schema cookie.
  235. #
  236. do_test alter2-4.1 {
  237. db close
  238. set_file_format 5
  239. catch { sqlite3 db test.db }
  240. set {} {}
  241. } {}
  242. do_test alter2-4.2 {
  243. # We have to run two queries here because the Tcl interface uses
  244. # sqlite3_prepare_v2(). In this case, the first query encounters an
  245. # SQLITE_SCHEMA error. Then, when trying to recompile the statement, the
  246. # "unsupported file format" error is encountered. So the error code
  247. # returned is SQLITE_SCHEMA, not SQLITE_ERROR as required by the following
  248. # test case.
  249. #
  250. # When the query is attempted a second time, the same error message is
  251. # returned but the error code is SQLITE_ERROR, because the unsupported
  252. # file format was detected during a call to sqlite3_prepare(), not
  253. # sqlite3_step().
  254. #
  255. catchsql { SELECT * FROM sqlite_master; }
  256. catchsql { SELECT * FROM sqlite_master; }
  257. } {1 {unsupported file format}}
  258. do_test alter2-4.3 {
  259. sqlite3_errcode db
  260. } {SQLITE_ERROR}
  261. do_test alter2-4.4 {
  262. set ::DB [sqlite3_connection_pointer db]
  263. catchsql {
  264. SELECT * FROM sqlite_master;
  265. }
  266. } {1 {unsupported file format}}
  267. do_test alter2-4.5 {
  268. sqlite3_errcode db
  269. } {SQLITE_ERROR}
  270. #---------------------------------------------------------------------
  271. # Check that executing VACUUM on a file with file-format version 2
  272. # resets the file format to 1.
  273. #
  274. set default_file_format [expr $SQLITE_DEFAULT_FILE_FORMAT==4 ? 4 : 1]
  275. ifcapable vacuum {
  276. do_test alter2-5.1 {
  277. set_file_format 2
  278. db close
  279. sqlite3 db test.db
  280. execsql {SELECT 1 FROM sqlite_master LIMIT 1;}
  281. get_file_format
  282. } {2}
  283. do_test alter2-5.2 {
  284. execsql { VACUUM }
  285. } {}
  286. do_test alter2-5.3 {
  287. get_file_format
  288. } $default_file_format
  289. }
  290. #---------------------------------------------------------------------
  291. # Test that when a database with file-format 2 is opened, new
  292. # databases are still created with file-format 1.
  293. #
  294. do_test alter2-6.1 {
  295. db close
  296. set_file_format 2
  297. sqlite3 db test.db
  298. get_file_format
  299. } {2}
  300. ifcapable attach {
  301. do_test alter2-6.2 {
  302. forcedelete test2.db-journal
  303. forcedelete test2.db
  304. execsql {
  305. ATTACH 'test2.db' AS aux;
  306. CREATE TABLE aux.t1(a, b);
  307. }
  308. get_file_format test2.db
  309. } $default_file_format
  310. }
  311. do_test alter2-6.3 {
  312. execsql {
  313. CREATE TABLE t1(a, b);
  314. }
  315. get_file_format
  316. } {2}
  317. #---------------------------------------------------------------------
  318. # Test that types and values for columns added with default values
  319. # other than NULL work with SELECT statements.
  320. #
  321. do_test alter2-7.1 {
  322. execsql {
  323. DROP TABLE t1;
  324. CREATE TABLE t1(a);
  325. INSERT INTO t1 VALUES(1);
  326. INSERT INTO t1 VALUES(2);
  327. INSERT INTO t1 VALUES(3);
  328. INSERT INTO t1 VALUES(4);
  329. SELECT * FROM t1;
  330. }
  331. } {1 2 3 4}
  332. do_test alter2-7.2 {
  333. set sql {CREATE TABLE t1(a, b DEFAULT '123', c INTEGER DEFAULT '123')}
  334. alter_table t1 $sql 3
  335. execsql {
  336. SELECT * FROM t1 LIMIT 1;
  337. }
  338. } {1 123 123}
  339. do_test alter2-7.3 {
  340. execsql {
  341. SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
  342. }
  343. } {1 integer 123 text 123 integer}
  344. do_test alter2-7.4 {
  345. execsql {
  346. SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
  347. }
  348. } {1 integer 123 text 123 integer}
  349. do_test alter2-7.5 {
  350. set sql {CREATE TABLE t1(a, b DEFAULT -123.0, c VARCHAR(10) default 5)}
  351. alter_table t1 $sql 3
  352. execsql {
  353. SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
  354. }
  355. } {1 integer -123 integer 5 text}
  356. #-----------------------------------------------------------------------
  357. # Test that UPDATE trigger tables work with default values, and that when
  358. # a row is updated the default values are correctly transfered to the
  359. # new row.
  360. #
  361. ifcapable trigger {
  362. db function set_val {set ::val}
  363. do_test alter2-8.1 {
  364. execsql {
  365. CREATE TRIGGER trig1 BEFORE UPDATE ON t1 BEGIN
  366. SELECT set_val(
  367. old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)||' '||
  368. new.b||' '||typeof(new.b)||' '||new.c||' '||typeof(new.c)
  369. );
  370. END;
  371. }
  372. list
  373. } {}
  374. }
  375. do_test alter2-8.2 {
  376. execsql {
  377. UPDATE t1 SET c = 10 WHERE a = 1;
  378. SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t1 LIMIT 1;
  379. }
  380. } {1 integer -123 integer 10 text}
  381. ifcapable trigger {
  382. do_test alter2-8.3 {
  383. set ::val
  384. } {-123 integer 5 text -123 integer 10 text}
  385. }
  386. #-----------------------------------------------------------------------
  387. # Test that DELETE trigger tables work with default values, and that when
  388. # a row is updated the default values are correctly transfered to the
  389. # new row.
  390. #
  391. ifcapable trigger {
  392. do_test alter2-9.1 {
  393. execsql {
  394. CREATE TRIGGER trig2 BEFORE DELETE ON t1 BEGIN
  395. SELECT set_val(
  396. old.b||' '||typeof(old.b)||' '||old.c||' '||typeof(old.c)
  397. );
  398. END;
  399. }
  400. list
  401. } {}
  402. do_test alter2-9.2 {
  403. execsql {
  404. DELETE FROM t1 WHERE a = 2;
  405. }
  406. set ::val
  407. } {-123 integer 5 text}
  408. }
  409. #-----------------------------------------------------------------------
  410. # Test creating an index on a column added with a default value.
  411. #
  412. ifcapable bloblit {
  413. do_test alter2-10.1 {
  414. execsql {
  415. CREATE TABLE t2(a);
  416. INSERT INTO t2 VALUES('a');
  417. INSERT INTO t2 VALUES('b');
  418. INSERT INTO t2 VALUES('c');
  419. INSERT INTO t2 VALUES('d');
  420. }
  421. alter_table t2 {CREATE TABLE t2(a, b DEFAULT X'ABCD', c DEFAULT NULL);} 3
  422. catchsql {
  423. SELECT * FROM sqlite_master;
  424. }
  425. execsql {
  426. SELECT quote(a), quote(b), quote(c) FROM t2 LIMIT 1;
  427. }
  428. } {'a' X'ABCD' NULL}
  429. do_test alter2-10.2 {
  430. execsql {
  431. CREATE INDEX i1 ON t2(b);
  432. SELECT a FROM t2 WHERE b = X'ABCD';
  433. }
  434. } {a b c d}
  435. do_test alter2-10.3 {
  436. execsql {
  437. DELETE FROM t2 WHERE a = 'c';
  438. SELECT a FROM t2 WHERE b = X'ABCD';
  439. }
  440. } {a b d}
  441. do_test alter2-10.4 {
  442. execsql {
  443. SELECT count(b) FROM t2 WHERE b = X'ABCD';
  444. }
  445. } {3}
  446. }
  447. finish_test