jrnlmode.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. # 2008 April 17
  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 focus
  12. # of these tests is the journal mode pragma.
  13. #
  14. # $Id: jrnlmode.test,v 1.16 2009/06/05 17:09:12 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. ifcapable {!pager_pragmas} {
  18. finish_test
  19. return
  20. }
  21. if {[info exists TEMP_STORE] && $TEMP_STORE>=2} {
  22. set temp_persist memory
  23. set temp_delete memory
  24. set temp_truncate memory
  25. set temp_off off
  26. } else {
  27. set temp_persist persist
  28. set temp_delete delete
  29. set temp_truncate truncate
  30. set temp_off off
  31. }
  32. proc temp_journal_mode {newmode} {
  33. if {[info exists ::TEMP_STORE] && $::TEMP_STORE>=2} {
  34. if {$newmode ne "off" && $newmode ne "memory"} {
  35. execsql {PRAGMA temp.journal_mode}
  36. set newmode [db one {PRAGMA temp.journal_mode}]
  37. }
  38. }
  39. set newmode
  40. }
  41. #----------------------------------------------------------------------
  42. # Test cases jrnlmode-1.X test the PRAGMA logic.
  43. #
  44. do_test jrnlmode-1.0 {
  45. execsql {
  46. PRAGMA journal_mode;
  47. PRAGMA main.journal_mode;
  48. PRAGMA temp.journal_mode;
  49. }
  50. } [list delete delete [temp_journal_mode delete]]
  51. do_test jrnlmode-1.1 {
  52. execsql {
  53. PRAGMA journal_mode = persist;
  54. }
  55. } {persist}
  56. do_test jrnlmode-1.2 {
  57. execsql {
  58. PRAGMA journal_mode;
  59. PRAGMA main.journal_mode;
  60. PRAGMA temp.journal_mode;
  61. }
  62. } [list persist persist [temp_journal_mode persist]]
  63. do_test jrnlmode-1.4 {
  64. execsql {
  65. PRAGMA journal_mode = off;
  66. }
  67. } {off}
  68. do_test jrnlmode-1.5 {
  69. execsql {
  70. PRAGMA journal_mode;
  71. PRAGMA main.journal_mode;
  72. PRAGMA temp.journal_mode;
  73. }
  74. } [list off off [temp_journal_mode off]]
  75. do_test jrnlmode-1.6 {
  76. execsql {
  77. PRAGMA journal_mode = delete;
  78. }
  79. } {delete}
  80. do_test jrnlmode-1.7 {
  81. execsql {
  82. PRAGMA journal_mode;
  83. PRAGMA main.journal_mode;
  84. PRAGMA Temp.journal_mode;
  85. }
  86. } [list delete delete [temp_journal_mode delete]]
  87. do_test jrnlmode-1.7.1 {
  88. execsql {
  89. PRAGMA journal_mode = truncate;
  90. }
  91. } {truncate}
  92. do_test jrnlmode-1.7.2 {
  93. execsql {
  94. PRAGMA journal_mode;
  95. PRAGMA main.journal_mode;
  96. PRAGMA temp.journal_mode;
  97. }
  98. } [list truncate truncate [temp_journal_mode truncate]]
  99. do_test jrnlmode-1.8 {
  100. execsql {
  101. PRAGMA journal_mode = off;
  102. PRAGMA journal_mode = invalid;
  103. }
  104. } {off off}
  105. ifcapable attach {
  106. do_test jrnlmode-1.9 {
  107. execsql {
  108. PRAGMA journal_mode = PERSIST;
  109. ATTACH ':memory:' as aux1;
  110. }
  111. execsql {
  112. PRAGMA main.journal_mode;
  113. PRAGMA aux1.journal_mode;
  114. }
  115. } {persist memory}
  116. do_test jrnlmode-1.10 {
  117. execsql {
  118. PRAGMA main.journal_mode = OFF;
  119. }
  120. execsql {
  121. PRAGMA main.journal_mode;
  122. PRAGMA temp.journal_mode;
  123. PRAGMA aux1.journal_mode;
  124. }
  125. } [list off [temp_journal_mode persist] memory]
  126. do_test jrnlmode-1.11 {
  127. execsql {
  128. PRAGMA journal_mode;
  129. }
  130. } {off}
  131. do_test jrnlmode-1.12 {
  132. execsql {
  133. ATTACH ':memory:' as aux2;
  134. }
  135. execsql {
  136. PRAGMA main.journal_mode;
  137. PRAGMA aux1.journal_mode;
  138. PRAGMA aux2.journal_mode;
  139. }
  140. } {off memory memory}
  141. do_test jrnlmode-1.13 {
  142. # The journal-mode used by in-memory databases cannot be changed.
  143. execsql {
  144. PRAGMA aux1.journal_mode = DELETE;
  145. }
  146. execsql {
  147. PRAGMA main.journal_mode;
  148. PRAGMA aux1.journal_mode;
  149. PRAGMA aux2.journal_mode;
  150. }
  151. } {off memory memory}
  152. do_test jrnlmode-1.14 {
  153. execsql {
  154. PRAGMA journal_mode = delete;
  155. }
  156. execsql {
  157. PRAGMA main.journal_mode;
  158. PRAGMA temp.journal_mode;
  159. PRAGMA aux1.journal_mode;
  160. PRAGMA aux2.journal_mode;
  161. }
  162. } [list delete [temp_journal_mode delete] memory memory]
  163. do_test jrnlmode-1.15 {
  164. execsql {
  165. ATTACH ':memory:' as aux3;
  166. }
  167. execsql {
  168. PRAGMA main.journal_mode;
  169. PRAGMA temp.journal_mode;
  170. PRAGMA aux1.journal_mode;
  171. PRAGMA aux2.journal_mode;
  172. PRAGMA aux3.journal_mode;
  173. }
  174. } [list delete [temp_journal_mode delete] memory memory memory]
  175. do_test jrnlmode-1.16 {
  176. execsql {
  177. PRAGMA journal_mode = TRUNCATE;
  178. }
  179. execsql {
  180. PRAGMA main.journal_mode;
  181. PRAGMA temp.journal_mode;
  182. PRAGMA aux1.journal_mode;
  183. PRAGMA aux2.journal_mode;
  184. PRAGMA aux3.journal_mode;
  185. }
  186. } [list truncate [temp_journal_mode truncate] memory memory memory]
  187. do_test jrnlmode-1.99 {
  188. execsql {
  189. DETACH aux1;
  190. DETACH aux2;
  191. DETACH aux3;
  192. }
  193. } {}
  194. }
  195. ifcapable attach {
  196. forcedelete test2.db
  197. do_test jrnlmode-2.1 {
  198. execsql {
  199. ATTACH 'test2.db' AS aux;
  200. PRAGMA main.journal_mode = persist;
  201. PRAGMA aux.journal_mode = persist;
  202. CREATE TABLE abc(a, b, c);
  203. CREATE TABLE aux.def(d, e, f);
  204. }
  205. execsql {
  206. BEGIN;
  207. INSERT INTO abc VALUES(1, 2, 3);
  208. INSERT INTO def VALUES(4, 5, 6);
  209. COMMIT;
  210. }
  211. list [file exists test.db-journal] [file exists test2.db-journal]
  212. } {1 1}
  213. do_test jrnlmode-2.2 {
  214. file size test.db-journal
  215. } {0}
  216. do_test jrnlmode-2.3 {
  217. execsql {
  218. SELECT * FROM abc;
  219. }
  220. } {1 2 3}
  221. do_test jrnlmode-2.4 {
  222. file size test.db-journal
  223. } {0}
  224. do_test jrnlmode-2.5 {
  225. execsql {
  226. SELECT * FROM def;
  227. }
  228. } {4 5 6}
  229. #----------------------------------------------------------------------
  230. # Test caes jrnlmode-3.X verify that ticket #3127 has been fixed.
  231. #
  232. db close
  233. forcedelete test2.db
  234. forcedelete test.db
  235. sqlite3 db test.db
  236. do_test jrnlmode-3.1 {
  237. execsql {
  238. CREATE TABLE x(n INTEGER);
  239. ATTACH 'test2.db' AS a;
  240. create table a.x ( n integer );
  241. insert into a.x values(1);
  242. insert into a.x values (2);
  243. insert into a.x values (3);
  244. insert into a.x values (4);
  245. }
  246. } {}
  247. do_test jrnlmode-3.2 {
  248. execsql { PRAGMA journal_mode=off; }
  249. execsql {
  250. BEGIN IMMEDIATE;
  251. INSERT OR IGNORE INTO main.x SELECT * FROM a.x;
  252. COMMIT;
  253. }
  254. } {}
  255. }
  256. ifcapable autovacuum&&pragma {
  257. db close
  258. forcedelete test.db
  259. sqlite3 db test.db
  260. do_test jrnlmode-4.1 {
  261. execsql {
  262. PRAGMA cache_size = 1;
  263. PRAGMA auto_vacuum = 1;
  264. CREATE TABLE abc(a, b, c);
  265. }
  266. execsql { PRAGMA page_count }
  267. } {3}
  268. do_test jrnlmode-4.2 {
  269. execsql { PRAGMA journal_mode = off }
  270. } {off}
  271. do_test jrnlmode-4.3 {
  272. execsql { INSERT INTO abc VALUES(1, 2, randomblob(2000)) }
  273. } {}
  274. # This will attempt to truncate the database file. Check that this
  275. # is not a problem when journal_mode=off.
  276. do_test jrnlmode-4.4 {
  277. execsql { DELETE FROM abc }
  278. } {}
  279. integrity_check jrnlmode-4.5
  280. }
  281. #------------------------------------------------------------------------
  282. # The following test caes, jrnlmode-5.*, test the journal_size_limit
  283. # pragma.
  284. ifcapable pragma {
  285. db close
  286. forcedelete test.db test2.db test3.db
  287. sqlite3 db test.db
  288. do_test jrnlmode-5.1 {
  289. execsql {pragma page_size=1024}
  290. execsql {pragma journal_mode=persist}
  291. } {persist}
  292. do_test jrnlmode-5.2 {
  293. execsql { PRAGMA journal_size_limit }
  294. } {-1}
  295. do_test jrnlmode-5.3 {
  296. execsql {
  297. ATTACH 'test2.db' AS aux;
  298. PRAGMA aux.journal_mode=persist;
  299. PRAGMA aux.journal_size_limit;
  300. }
  301. } {persist -1}
  302. do_test jrnlmode-5.4.1 {
  303. execsql { PRAGMA aux.journal_size_limit = 999999999999 }
  304. } {999999999999}
  305. do_test jrnlmode-5.4.2 {
  306. execsql { PRAGMA aux.journal_size_limit = 10240 }
  307. } {10240}
  308. do_test jrnlmode-5.5 {
  309. execsql { PRAGMA main.journal_size_limit = 20480 }
  310. } {20480}
  311. do_test jrnlmode-5.6 {
  312. execsql { PRAGMA journal_size_limit }
  313. } {20480}
  314. do_test jrnlmode-5.7 {
  315. execsql { PRAGMA aux.journal_size_limit }
  316. } {10240}
  317. do_test jrnlmode-5.8 {
  318. execsql {
  319. ATTACH 'test3.db' AS aux2;
  320. PRAGMA aux2.journal_mode=persist;
  321. }
  322. } {persist}
  323. do_test jrnlmode-5.9 {
  324. execsql {
  325. CREATE TABLE main.t1(a, b, c);
  326. CREATE TABLE aux.t2(a, b, c);
  327. CREATE TABLE aux2.t3(a, b, c);
  328. }
  329. } {}
  330. do_test jrnlmode-5.10 {
  331. list \
  332. [file exists test.db-journal] \
  333. [file exists test2.db-journal] \
  334. [file exists test3.db-journal]
  335. } {1 1 1}
  336. do_test jrnlmode-5.11 {
  337. execsql {
  338. BEGIN;
  339. INSERT INTO t3 VALUES(randomblob(1000),randomblob(1000),randomblob(1000));
  340. INSERT INTO t3
  341. SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
  342. INSERT INTO t3
  343. SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
  344. INSERT INTO t3
  345. SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
  346. INSERT INTO t3
  347. SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
  348. INSERT INTO t3
  349. SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
  350. INSERT INTO t2 SELECT * FROM t3;
  351. INSERT INTO t1 SELECT * FROM t2;
  352. COMMIT;
  353. }
  354. list \
  355. [file exists test.db-journal] \
  356. [file exists test2.db-journal] \
  357. [file exists test3.db-journal] \
  358. [file size test.db-journal] \
  359. [file size test2.db-journal] \
  360. [file size test3.db-journal]
  361. } {1 1 1 0 0 0}
  362. do_test jrnlmode-5.12 {
  363. execsql {
  364. BEGIN;
  365. UPDATE t1 SET a = randomblob(1000);
  366. }
  367. expr {[file size test.db-journal]>30000}
  368. } {1}
  369. do_test jrnlmode-5.13 {
  370. execsql COMMIT
  371. file size test.db-journal
  372. } {20480}
  373. do_test jrnlmode-5.14 {
  374. execsql {
  375. BEGIN;
  376. UPDATE t2 SET a = randomblob(1000);
  377. }
  378. expr {[file size test2.db-journal]>30000}
  379. } {1}
  380. do_test jrnlmode-5.15 {
  381. execsql COMMIT
  382. file size test2.db-journal
  383. } {10240}
  384. do_test jrnlmode-5.16 {
  385. execsql {
  386. BEGIN;
  387. UPDATE t3 SET a = randomblob(1000);
  388. }
  389. set journalsize [file size test3.db-journal]
  390. expr {$journalsize>30000}
  391. } {1}
  392. do_test jrnlmode-5.17 {
  393. execsql COMMIT
  394. set sz [file size test3.db-journal]
  395. expr {$sz>=$journalsize}
  396. } {1}
  397. do_test jrnlmode-5.18 {
  398. execsql {
  399. PRAGMA journal_size_limit = -4;
  400. BEGIN;
  401. UPDATE t1 SET a = randomblob(1000);
  402. }
  403. set journalsize [file size test.db-journal]
  404. expr {$journalsize>30000}
  405. } {1}
  406. do_test jrnlmode-5.19 {
  407. execsql COMMIT
  408. set sz [file size test.db-journal]
  409. expr {$sz>=$journalsize}
  410. } {1}
  411. # Test a size-limit of 0.
  412. #
  413. do_test jrnlmode-5.20 {
  414. execsql {
  415. PRAGMA journal_size_limit = 0;
  416. BEGIN;
  417. UPDATE t1 SET a = randomblob(1000);
  418. }
  419. } {0}
  420. do_test jrnlmode-5.21 {
  421. expr {[file size test.db-journal] > 1024}
  422. } {1}
  423. do_test jrnlmode-5.22 {
  424. execsql COMMIT
  425. list [file exists test.db-journal] [file size test.db-journal]
  426. } {1 0}
  427. }
  428. ifcapable pragma {
  429. # These tests are not run as part of the "journaltest" permutation,
  430. # as the test_journal.c layer is incompatible with in-memory journaling.
  431. if {[permutation] ne "journaltest"} {
  432. do_test jrnlmode-6.1 {
  433. execsql {
  434. PRAGMA journal_mode = truncate;
  435. CREATE TABLE t4(a, b);
  436. BEGIN;
  437. INSERT INTO t4 VALUES(1, 2);
  438. PRAGMA journal_mode = memory;
  439. }
  440. } {truncate truncate}
  441. do_test jrnlmode-6.2 {
  442. file exists test.db-journal
  443. } {1}
  444. do_test jrnlmode-6.3 {
  445. execsql {
  446. COMMIT;
  447. SELECT * FROM t4;
  448. }
  449. } {1 2}
  450. do_test jrnlmode-6.4 {
  451. file exists test.db-journal
  452. } {1}
  453. do_test jrnlmode-6.5 {
  454. execsql {
  455. PRAGMA journal_mode = MEMORY;
  456. BEGIN;
  457. INSERT INTO t4 VALUES(3, 4);
  458. }
  459. file exists test.db-journal
  460. } {0}
  461. do_test jrnlmode-6.7 {
  462. execsql {
  463. COMMIT;
  464. SELECT * FROM t4;
  465. }
  466. } {1 2 3 4}
  467. do_test jrnlmode-6.8 {
  468. file exists test.db-journal
  469. } {0}
  470. do_test jrnlmode-6.9 {
  471. execsql {
  472. PRAGMA journal_mode = DELETE;
  473. BEGIN IMMEDIATE; INSERT INTO t4 VALUES(1,2); COMMIT;
  474. }
  475. file exists test.db-journal
  476. } {0}
  477. }
  478. }
  479. ifcapable pragma {
  480. catch { db close }
  481. do_test jrnlmode-7.1 {
  482. foreach f [glob -nocomplain test.db*] { forcedelete $f }
  483. sqlite3 db test.db
  484. execsql {
  485. PRAGMA journal_mode = memory;
  486. PRAGMA auto_vacuum = 0;
  487. PRAGMA page_size = 1024;
  488. PRAGMA user_version = 5;
  489. PRAGMA user_version;
  490. }
  491. } {memory 5}
  492. do_test jrnlmode-7.2 { file size test.db } {1024}
  493. }
  494. do_execsql_test jrnlmode-8.1 { PRAGMA locking_mode=EXCLUSIVE } {exclusive}
  495. do_execsql_test jrnlmode-8.2 { CREATE TABLE t1(x) } {}
  496. do_execsql_test jrnlmode-8.3 { INSERT INTO t1 VALUES(123) } {}
  497. do_execsql_test jrnlmode-8.4 { SELECT * FROM t1 } {123}
  498. do_execsql_test jrnlmode-8.5 { PRAGMA journal_mode=PERSIST } {persist}
  499. do_execsql_test jrnlmode-8.6 { PRAGMA journal_mode=DELETE } {delete}
  500. do_execsql_test jrnlmode-8.7 { PRAGMA journal_mode=TRUNCATE } {truncate}
  501. do_execsql_test jrnlmode-8.8 { PRAGMA journal_mode=DELETE } {delete}
  502. do_execsql_test jrnlmode-8.9 { CREATE TABLE t2(y) } {}
  503. do_execsql_test jrnlmode-8.10 { INSERT INTO t2 VALUES(456) } {}
  504. do_execsql_test jrnlmode-8.11 { SELECT * FROM t1, t2 } {123 456}
  505. do_execsql_test jrnlmode-8.12 { PRAGMA locking_mode=NORMAL } {normal}
  506. do_execsql_test jrnlmode-8.13 { PRAGMA journal_mode=PERSIST } {persist}
  507. do_execsql_test jrnlmode-8.14 { PRAGMA journal_mode=TRUNCATE } {truncate}
  508. do_execsql_test jrnlmode-8.15 { PRAGMA journal_mode=PERSIST } {persist}
  509. do_execsql_test jrnlmode-8.16 { PRAGMA journal_mode=DELETE } {delete}
  510. do_execsql_test jrnlmode-8.17 { PRAGMA journal_mode=TRUNCATE } {truncate}
  511. do_execsql_test jrnlmode-8.18 { PRAGMA locking_mode=EXCLUSIVE } {exclusive}
  512. do_execsql_test jrnlmode-8.19 { CREATE TABLE t3(z) } {}
  513. do_execsql_test jrnlmode-8.20 { BEGIN IMMEDIATE } {}
  514. do_execsql_test jrnlmode-8.21 { PRAGMA journal_mode=DELETE } {delete}
  515. do_execsql_test jrnlmode-8.22 { COMMIT } {}
  516. do_execsql_test jrnlmode-8.23 { PRAGMA journal_mode=DELETE } {delete}
  517. do_execsql_test jrnlmode-8.24 { PRAGMA journal_mode=TRUNCATE } {truncate}
  518. do_execsql_test jrnlmode-8.25 { PRAGMA locking_mode=NORMAL } {normal}
  519. do_execsql_test jrnlmode-8.26 { CREATE TABLE t4(w) } {}
  520. do_execsql_test jrnlmode-8.27 { BEGIN IMMEDIATE } {}
  521. do_execsql_test jrnlmode-8.28 { PRAGMA journal_mode=DELETE } {delete}
  522. do_execsql_test jrnlmode-8.29 { COMMIT } {}
  523. do_execsql_test jrnlmode-8.30 { PRAGMA journal_mode=DELETE } {delete}
  524. finish_test