alter3.test 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. # 2005 February 19
  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: alter3.test,v 1.11 2008/03/19 00:21:31 drh Exp $
  17. #
  18. set testdir [file dirname $argv0]
  19. source $testdir/tester.tcl
  20. # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
  21. ifcapable !altertable {
  22. finish_test
  23. return
  24. }
  25. # Determine if there is a codec available on this test.
  26. #
  27. if {[catch {sqlite3 -has-codec} r] || $r} {
  28. set has_codec 1
  29. } else {
  30. set has_codec 0
  31. }
  32. # Test Organisation:
  33. # ------------------
  34. #
  35. # alter3-1.*: Test that ALTER TABLE correctly modifies the CREATE TABLE sql.
  36. # alter3-2.*: Test error messages.
  37. # alter3-3.*: Test adding columns with default value NULL.
  38. # alter3-4.*: Test adding columns with default values other than NULL.
  39. # alter3-5.*: Test adding columns to tables in ATTACHed databases.
  40. # alter3-6.*: Test that temp triggers are not accidentally dropped.
  41. # alter3-7.*: Test that VACUUM resets the file-format.
  42. #
  43. # This procedure returns the value of the file-format in file 'test.db'.
  44. #
  45. proc get_file_format {{fname test.db}} {
  46. return [hexio_get_int [hexio_read $fname 44 4]]
  47. }
  48. do_test alter3-1.1 {
  49. execsql {
  50. PRAGMA legacy_file_format=ON;
  51. CREATE TABLE abc(a, b, c);
  52. SELECT sql FROM sqlite_master;
  53. }
  54. } {{CREATE TABLE abc(a, b, c)}}
  55. do_test alter3-1.2 {
  56. execsql {ALTER TABLE abc ADD d INTEGER;}
  57. execsql {
  58. SELECT sql FROM sqlite_master;
  59. }
  60. } {{CREATE TABLE abc(a, b, c, d INTEGER)}}
  61. do_test alter3-1.3 {
  62. execsql {ALTER TABLE abc ADD e}
  63. execsql {
  64. SELECT sql FROM sqlite_master;
  65. }
  66. } {{CREATE TABLE abc(a, b, c, d INTEGER, e)}}
  67. do_test alter3-1.4 {
  68. execsql {
  69. CREATE TABLE main.t1(a, b);
  70. ALTER TABLE t1 ADD c;
  71. SELECT sql FROM sqlite_master WHERE tbl_name = 't1';
  72. }
  73. } {{CREATE TABLE t1(a, b, c)}}
  74. do_test alter3-1.5 {
  75. execsql {
  76. ALTER TABLE t1 ADD d CHECK (a>d);
  77. SELECT sql FROM sqlite_master WHERE tbl_name = 't1';
  78. }
  79. } {{CREATE TABLE t1(a, b, c, d CHECK (a>d))}}
  80. ifcapable foreignkey {
  81. do_test alter3-1.6 {
  82. execsql {
  83. CREATE TABLE t2(a, b, UNIQUE(a, b));
  84. ALTER TABLE t2 ADD c REFERENCES t1(c) ;
  85. SELECT sql FROM sqlite_master WHERE tbl_name = 't2' AND type = 'table';
  86. }
  87. } {{CREATE TABLE t2(a, b, c REFERENCES t1(c), UNIQUE(a, b))}}
  88. }
  89. do_test alter3-1.7 {
  90. execsql {
  91. CREATE TABLE t3(a, b, UNIQUE(a, b));
  92. ALTER TABLE t3 ADD COLUMN c VARCHAR(10, 20);
  93. SELECT sql FROM sqlite_master WHERE tbl_name = 't3' AND type = 'table';
  94. }
  95. } {{CREATE TABLE t3(a, b, c VARCHAR(10, 20), UNIQUE(a, b))}}
  96. do_test alter3-1.99 {
  97. catchsql {
  98. # May not exist if foriegn-keys are omitted at compile time.
  99. DROP TABLE t2;
  100. }
  101. execsql {
  102. DROP TABLE abc;
  103. DROP TABLE t1;
  104. DROP TABLE t3;
  105. }
  106. } {}
  107. do_test alter3-2.1 {
  108. execsql {
  109. CREATE TABLE t1(a, b);
  110. }
  111. catchsql {
  112. ALTER TABLE t1 ADD c PRIMARY KEY;
  113. }
  114. } {1 {Cannot add a PRIMARY KEY column}}
  115. do_test alter3-2.2 {
  116. catchsql {
  117. ALTER TABLE t1 ADD c UNIQUE
  118. }
  119. } {1 {Cannot add a UNIQUE column}}
  120. do_test alter3-2.3 {
  121. catchsql {
  122. ALTER TABLE t1 ADD b VARCHAR(10)
  123. }
  124. } {1 {duplicate column name: b}}
  125. do_test alter3-2.3 {
  126. catchsql {
  127. ALTER TABLE t1 ADD c NOT NULL;
  128. }
  129. } {1 {Cannot add a NOT NULL column with default value NULL}}
  130. do_test alter3-2.4 {
  131. catchsql {
  132. ALTER TABLE t1 ADD c NOT NULL DEFAULT 10;
  133. }
  134. } {0 {}}
  135. ifcapable view {
  136. do_test alter3-2.5 {
  137. execsql {
  138. CREATE VIEW v1 AS SELECT * FROM t1;
  139. }
  140. catchsql {
  141. alter table v1 add column d;
  142. }
  143. } {1 {Cannot add a column to a view}}
  144. }
  145. do_test alter3-2.6 {
  146. catchsql {
  147. alter table t1 add column d DEFAULT CURRENT_TIME;
  148. }
  149. } {1 {Cannot add a column with non-constant default}}
  150. do_test alter3-2.99 {
  151. execsql {
  152. DROP TABLE t1;
  153. }
  154. } {}
  155. do_test alter3-3.1 {
  156. execsql {
  157. CREATE TABLE t1(a, b);
  158. INSERT INTO t1 VALUES(1, 100);
  159. INSERT INTO t1 VALUES(2, 300);
  160. SELECT * FROM t1;
  161. }
  162. } {1 100 2 300}
  163. do_test alter3-3.1 {
  164. execsql {
  165. PRAGMA schema_version = 10;
  166. }
  167. } {}
  168. do_test alter3-3.2 {
  169. execsql {
  170. ALTER TABLE t1 ADD c;
  171. SELECT * FROM t1;
  172. }
  173. } {1 100 {} 2 300 {}}
  174. if {!$has_codec} {
  175. do_test alter3-3.3 {
  176. get_file_format
  177. } {3}
  178. }
  179. ifcapable schema_version {
  180. do_test alter3-3.4 {
  181. execsql {
  182. PRAGMA schema_version;
  183. }
  184. } {11}
  185. }
  186. do_test alter3-4.1 {
  187. db close
  188. forcedelete test.db
  189. set ::DB [sqlite3 db test.db]
  190. execsql {
  191. PRAGMA legacy_file_format=ON;
  192. CREATE TABLE t1(a, b);
  193. INSERT INTO t1 VALUES(1, 100);
  194. INSERT INTO t1 VALUES(2, 300);
  195. SELECT * FROM t1;
  196. }
  197. } {1 100 2 300}
  198. do_test alter3-4.1 {
  199. execsql {
  200. PRAGMA schema_version = 20;
  201. }
  202. } {}
  203. do_test alter3-4.2 {
  204. execsql {
  205. ALTER TABLE t1 ADD c DEFAULT 'hello world';
  206. SELECT * FROM t1;
  207. }
  208. } {1 100 {hello world} 2 300 {hello world}}
  209. if {!$has_codec} {
  210. do_test alter3-4.3 {
  211. get_file_format
  212. } {3}
  213. }
  214. ifcapable schema_version {
  215. do_test alter3-4.4 {
  216. execsql {
  217. PRAGMA schema_version;
  218. }
  219. } {21}
  220. }
  221. do_test alter3-4.99 {
  222. execsql {
  223. DROP TABLE t1;
  224. }
  225. } {}
  226. ifcapable attach {
  227. do_test alter3-5.1 {
  228. forcedelete test2.db
  229. forcedelete test2.db-journal
  230. execsql {
  231. CREATE TABLE t1(a, b);
  232. INSERT INTO t1 VALUES(1, 'one');
  233. INSERT INTO t1 VALUES(2, 'two');
  234. ATTACH 'test2.db' AS aux;
  235. CREATE TABLE aux.t1 AS SELECT * FROM t1;
  236. PRAGMA aux.schema_version = 30;
  237. SELECT sql FROM aux.sqlite_master;
  238. }
  239. } {{CREATE TABLE t1(a,b)}}
  240. do_test alter3-5.2 {
  241. execsql {
  242. ALTER TABLE aux.t1 ADD COLUMN c VARCHAR(128);
  243. SELECT sql FROM aux.sqlite_master;
  244. }
  245. } {{CREATE TABLE t1(a,b, c VARCHAR(128))}}
  246. do_test alter3-5.3 {
  247. execsql {
  248. SELECT * FROM aux.t1;
  249. }
  250. } {1 one {} 2 two {}}
  251. ifcapable schema_version {
  252. do_test alter3-5.4 {
  253. execsql {
  254. PRAGMA aux.schema_version;
  255. }
  256. } {31}
  257. }
  258. if {!$has_codec} {
  259. do_test alter3-5.5 {
  260. list [get_file_format test2.db] [get_file_format]
  261. } {2 3}
  262. }
  263. do_test alter3-5.6 {
  264. execsql {
  265. ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000;
  266. SELECT sql FROM aux.sqlite_master;
  267. }
  268. } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}}
  269. do_test alter3-5.7 {
  270. execsql {
  271. SELECT * FROM aux.t1;
  272. }
  273. } {1 one {} 1000 2 two {} 1000}
  274. ifcapable schema_version {
  275. do_test alter3-5.8 {
  276. execsql {
  277. PRAGMA aux.schema_version;
  278. }
  279. } {32}
  280. }
  281. do_test alter3-5.9 {
  282. execsql {
  283. SELECT * FROM t1;
  284. }
  285. } {1 one 2 two}
  286. do_test alter3-5.99 {
  287. execsql {
  288. DROP TABLE aux.t1;
  289. DROP TABLE t1;
  290. }
  291. } {}
  292. }
  293. #----------------------------------------------------------------
  294. # Test that the table schema is correctly reloaded when a column
  295. # is added to a table.
  296. #
  297. ifcapable trigger&&tempdb {
  298. do_test alter3-6.1 {
  299. execsql {
  300. CREATE TABLE t1(a, b);
  301. CREATE TABLE log(trig, a, b);
  302. CREATE TRIGGER t1_a AFTER INSERT ON t1 BEGIN
  303. INSERT INTO log VALUES('a', new.a, new.b);
  304. END;
  305. CREATE TEMP TRIGGER t1_b AFTER INSERT ON t1 BEGIN
  306. INSERT INTO log VALUES('b', new.a, new.b);
  307. END;
  308. INSERT INTO t1 VALUES(1, 2);
  309. SELECT * FROM log;
  310. }
  311. } {b 1 2 a 1 2}
  312. do_test alter3-6.2 {
  313. execsql {
  314. ALTER TABLE t1 ADD COLUMN c DEFAULT 'c';
  315. INSERT INTO t1(a, b) VALUES(3, 4);
  316. SELECT * FROM log;
  317. }
  318. } {b 1 2 a 1 2 b 3 4 a 3 4}
  319. }
  320. if {!$has_codec} {
  321. ifcapable vacuum {
  322. do_test alter3-7.1 {
  323. execsql {
  324. VACUUM;
  325. }
  326. get_file_format
  327. } {1}
  328. do_test alter3-7.2 {
  329. execsql {
  330. CREATE TABLE abc(a, b, c);
  331. ALTER TABLE abc ADD d DEFAULT NULL;
  332. }
  333. get_file_format
  334. } {2}
  335. do_test alter3-7.3 {
  336. execsql {
  337. ALTER TABLE abc ADD e DEFAULT 10;
  338. }
  339. get_file_format
  340. } {3}
  341. do_test alter3-7.4 {
  342. execsql {
  343. ALTER TABLE abc ADD f DEFAULT NULL;
  344. }
  345. get_file_format
  346. } {3}
  347. do_test alter3-7.5 {
  348. execsql {
  349. VACUUM;
  350. }
  351. get_file_format
  352. } {1}
  353. }
  354. }
  355. # Ticket #1183 - Make sure adding columns to large tables does not cause
  356. # memory corruption (as was the case before this bug was fixed).
  357. do_test alter3-8.1 {
  358. execsql {
  359. CREATE TABLE t4(c1);
  360. }
  361. } {}
  362. set ::sql ""
  363. do_test alter3-8.2 {
  364. set cols c1
  365. for {set i 2} {$i < 100} {incr i} {
  366. execsql "
  367. ALTER TABLE t4 ADD c$i
  368. "
  369. lappend cols c$i
  370. }
  371. set ::sql "CREATE TABLE t4([join $cols {, }])"
  372. list
  373. } {}
  374. do_test alter3-8.2 {
  375. execsql {
  376. SELECT sql FROM sqlite_master WHERE name = 't4';
  377. }
  378. } [list $::sql]
  379. finish_test