alter4.test 7.8 KB

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