avtrans.test 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. # 2001 September 15
  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. This
  12. # file is a copy of "trans.test" modified to run under autovacuum mode.
  13. # the point is to stress the autovacuum logic and try to get it to fail.
  14. #
  15. # $Id: avtrans.test,v 1.6 2007/09/12 17:01:45 danielk1977 Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Create several tables to work with.
  19. #
  20. do_test avtrans-1.0 {
  21. execsql { PRAGMA auto_vacuum=ON }
  22. wal_set_journal_mode
  23. execsql {
  24. CREATE TABLE one(a int PRIMARY KEY, b text);
  25. INSERT INTO one VALUES(1,'one');
  26. INSERT INTO one VALUES(2,'two');
  27. INSERT INTO one VALUES(3,'three');
  28. SELECT b FROM one ORDER BY a;
  29. }
  30. } {one two three}
  31. do_test avtrans-1.1 {
  32. execsql {
  33. CREATE TABLE two(a int PRIMARY KEY, b text);
  34. INSERT INTO two VALUES(1,'I');
  35. INSERT INTO two VALUES(5,'V');
  36. INSERT INTO two VALUES(10,'X');
  37. SELECT b FROM two ORDER BY a;
  38. }
  39. } {I V X}
  40. do_test avtrans-1.9 {
  41. sqlite3 altdb test.db
  42. execsql {SELECT b FROM one ORDER BY a} altdb
  43. } {one two three}
  44. do_test avtrans-1.10 {
  45. execsql {SELECT b FROM two ORDER BY a} altdb
  46. } {I V X}
  47. integrity_check avtrans-1.11
  48. wal_check_journal_mode avtrans-1.12
  49. # Basic transactions
  50. #
  51. do_test avtrans-2.1 {
  52. set v [catch {execsql {BEGIN}} msg]
  53. lappend v $msg
  54. } {0 {}}
  55. do_test avtrans-2.2 {
  56. set v [catch {execsql {END}} msg]
  57. lappend v $msg
  58. } {0 {}}
  59. do_test avtrans-2.3 {
  60. set v [catch {execsql {BEGIN TRANSACTION}} msg]
  61. lappend v $msg
  62. } {0 {}}
  63. do_test avtrans-2.4 {
  64. set v [catch {execsql {COMMIT TRANSACTION}} msg]
  65. lappend v $msg
  66. } {0 {}}
  67. do_test avtrans-2.5 {
  68. set v [catch {execsql {BEGIN TRANSACTION 'foo'}} msg]
  69. lappend v $msg
  70. } {0 {}}
  71. do_test avtrans-2.6 {
  72. set v [catch {execsql {ROLLBACK TRANSACTION 'foo'}} msg]
  73. lappend v $msg
  74. } {0 {}}
  75. do_test avtrans-2.10 {
  76. execsql {
  77. BEGIN;
  78. SELECT a FROM one ORDER BY a;
  79. SELECT a FROM two ORDER BY a;
  80. END;
  81. }
  82. } {1 2 3 1 5 10}
  83. integrity_check avtrans-2.11
  84. wal_check_journal_mode avtrans-2.12
  85. # Check the locking behavior
  86. #
  87. sqlite3_soft_heap_limit 0
  88. do_test avtrans-3.1 {
  89. execsql {
  90. BEGIN;
  91. UPDATE one SET a = 0 WHERE 0;
  92. SELECT a FROM one ORDER BY a;
  93. }
  94. } {1 2 3}
  95. do_test avtrans-3.2 {
  96. catchsql {
  97. SELECT a FROM two ORDER BY a;
  98. } altdb
  99. } {0 {1 5 10}}
  100. do_test avtrans-3.3 {
  101. catchsql {
  102. SELECT a FROM one ORDER BY a;
  103. } altdb
  104. } {0 {1 2 3}}
  105. do_test avtrans-3.4 {
  106. catchsql {
  107. INSERT INTO one VALUES(4,'four');
  108. }
  109. } {0 {}}
  110. do_test avtrans-3.5 {
  111. catchsql {
  112. SELECT a FROM two ORDER BY a;
  113. } altdb
  114. } {0 {1 5 10}}
  115. do_test avtrans-3.6 {
  116. catchsql {
  117. SELECT a FROM one ORDER BY a;
  118. } altdb
  119. } {0 {1 2 3}}
  120. do_test avtrans-3.7 {
  121. catchsql {
  122. INSERT INTO two VALUES(4,'IV');
  123. }
  124. } {0 {}}
  125. do_test avtrans-3.8 {
  126. catchsql {
  127. SELECT a FROM two ORDER BY a;
  128. } altdb
  129. } {0 {1 5 10}}
  130. do_test avtrans-3.9 {
  131. catchsql {
  132. SELECT a FROM one ORDER BY a;
  133. } altdb
  134. } {0 {1 2 3}}
  135. do_test avtrans-3.10 {
  136. execsql {END TRANSACTION}
  137. } {}
  138. do_test avtrans-3.11 {
  139. set v [catch {execsql {
  140. SELECT a FROM two ORDER BY a;
  141. } altdb} msg]
  142. lappend v $msg
  143. } {0 {1 4 5 10}}
  144. do_test avtrans-3.12 {
  145. set v [catch {execsql {
  146. SELECT a FROM one ORDER BY a;
  147. } altdb} msg]
  148. lappend v $msg
  149. } {0 {1 2 3 4}}
  150. do_test avtrans-3.13 {
  151. set v [catch {execsql {
  152. SELECT a FROM two ORDER BY a;
  153. } db} msg]
  154. lappend v $msg
  155. } {0 {1 4 5 10}}
  156. do_test avtrans-3.14 {
  157. set v [catch {execsql {
  158. SELECT a FROM one ORDER BY a;
  159. } db} msg]
  160. lappend v $msg
  161. } {0 {1 2 3 4}}
  162. sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
  163. integrity_check avtrans-3.15
  164. do_test avtrans-4.1 {
  165. set v [catch {execsql {
  166. COMMIT;
  167. } db} msg]
  168. lappend v $msg
  169. } {1 {cannot commit - no transaction is active}}
  170. do_test avtrans-4.2 {
  171. set v [catch {execsql {
  172. ROLLBACK;
  173. } db} msg]
  174. lappend v $msg
  175. } {1 {cannot rollback - no transaction is active}}
  176. do_test avtrans-4.3 {
  177. catchsql {
  178. BEGIN TRANSACTION;
  179. UPDATE two SET a = 0 WHERE 0;
  180. SELECT a FROM two ORDER BY a;
  181. } db
  182. } {0 {1 4 5 10}}
  183. do_test avtrans-4.4 {
  184. catchsql {
  185. SELECT a FROM two ORDER BY a;
  186. } altdb
  187. } {0 {1 4 5 10}}
  188. do_test avtrans-4.5 {
  189. catchsql {
  190. SELECT a FROM one ORDER BY a;
  191. } altdb
  192. } {0 {1 2 3 4}}
  193. do_test avtrans-4.6 {
  194. catchsql {
  195. BEGIN TRANSACTION;
  196. SELECT a FROM one ORDER BY a;
  197. } db
  198. } {1 {cannot start a transaction within a transaction}}
  199. do_test avtrans-4.7 {
  200. catchsql {
  201. SELECT a FROM two ORDER BY a;
  202. } altdb
  203. } {0 {1 4 5 10}}
  204. do_test avtrans-4.8 {
  205. catchsql {
  206. SELECT a FROM one ORDER BY a;
  207. } altdb
  208. } {0 {1 2 3 4}}
  209. do_test avtrans-4.9 {
  210. set v [catch {execsql {
  211. END TRANSACTION;
  212. SELECT a FROM two ORDER BY a;
  213. } db} msg]
  214. lappend v $msg
  215. } {0 {1 4 5 10}}
  216. do_test avtrans-4.10 {
  217. set v [catch {execsql {
  218. SELECT a FROM two ORDER BY a;
  219. } altdb} msg]
  220. lappend v $msg
  221. } {0 {1 4 5 10}}
  222. do_test avtrans-4.11 {
  223. set v [catch {execsql {
  224. SELECT a FROM one ORDER BY a;
  225. } altdb} msg]
  226. lappend v $msg
  227. } {0 {1 2 3 4}}
  228. integrity_check avtrans-4.12
  229. do_test avtrans-4.98 {
  230. altdb close
  231. execsql {
  232. DROP TABLE one;
  233. DROP TABLE two;
  234. }
  235. } {}
  236. integrity_check avtrans-4.99
  237. # Check out the commit/rollback behavior of the database
  238. #
  239. do_test avtrans-5.1 {
  240. execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
  241. } {}
  242. do_test avtrans-5.2 {
  243. execsql {BEGIN TRANSACTION}
  244. execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
  245. } {}
  246. do_test avtrans-5.3 {
  247. execsql {CREATE TABLE one(a text, b int)}
  248. execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
  249. } {one}
  250. do_test avtrans-5.4 {
  251. execsql {SELECT a,b FROM one ORDER BY b}
  252. } {}
  253. do_test avtrans-5.5 {
  254. execsql {INSERT INTO one(a,b) VALUES('hello', 1)}
  255. execsql {SELECT a,b FROM one ORDER BY b}
  256. } {hello 1}
  257. do_test avtrans-5.6 {
  258. execsql {ROLLBACK}
  259. execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
  260. } {}
  261. do_test avtrans-5.7 {
  262. set v [catch {
  263. execsql {SELECT a,b FROM one ORDER BY b}
  264. } msg]
  265. lappend v $msg
  266. } {1 {no such table: one}}
  267. # Test commits and rollbacks of table CREATE TABLEs, CREATE INDEXs
  268. # DROP TABLEs and DROP INDEXs
  269. #
  270. do_test avtrans-5.8 {
  271. execsql {
  272. SELECT name fROM sqlite_master
  273. WHERE type='table' OR type='index'
  274. ORDER BY name
  275. }
  276. } {}
  277. do_test avtrans-5.9 {
  278. execsql {
  279. BEGIN TRANSACTION;
  280. CREATE TABLE t1(a int, b int, c int);
  281. SELECT name fROM sqlite_master
  282. WHERE type='table' OR type='index'
  283. ORDER BY name;
  284. }
  285. } {t1}
  286. do_test avtrans-5.10 {
  287. execsql {
  288. CREATE INDEX i1 ON t1(a);
  289. SELECT name fROM sqlite_master
  290. WHERE type='table' OR type='index'
  291. ORDER BY name;
  292. }
  293. } {i1 t1}
  294. do_test avtrans-5.11 {
  295. execsql {
  296. COMMIT;
  297. SELECT name fROM sqlite_master
  298. WHERE type='table' OR type='index'
  299. ORDER BY name;
  300. }
  301. } {i1 t1}
  302. do_test avtrans-5.12 {
  303. execsql {
  304. BEGIN TRANSACTION;
  305. CREATE TABLE t2(a int, b int, c int);
  306. CREATE INDEX i2a ON t2(a);
  307. CREATE INDEX i2b ON t2(b);
  308. DROP TABLE t1;
  309. SELECT name fROM sqlite_master
  310. WHERE type='table' OR type='index'
  311. ORDER BY name;
  312. }
  313. } {i2a i2b t2}
  314. do_test avtrans-5.13 {
  315. execsql {
  316. ROLLBACK;
  317. SELECT name fROM sqlite_master
  318. WHERE type='table' OR type='index'
  319. ORDER BY name;
  320. }
  321. } {i1 t1}
  322. do_test avtrans-5.14 {
  323. execsql {
  324. BEGIN TRANSACTION;
  325. DROP INDEX i1;
  326. SELECT name fROM sqlite_master
  327. WHERE type='table' OR type='index'
  328. ORDER BY name;
  329. }
  330. } {t1}
  331. do_test avtrans-5.15 {
  332. execsql {
  333. ROLLBACK;
  334. SELECT name fROM sqlite_master
  335. WHERE type='table' OR type='index'
  336. ORDER BY name;
  337. }
  338. } {i1 t1}
  339. do_test avtrans-5.16 {
  340. execsql {
  341. BEGIN TRANSACTION;
  342. DROP INDEX i1;
  343. CREATE TABLE t2(x int, y int, z int);
  344. CREATE INDEX i2x ON t2(x);
  345. CREATE INDEX i2y ON t2(y);
  346. INSERT INTO t2 VALUES(1,2,3);
  347. SELECT name fROM sqlite_master
  348. WHERE type='table' OR type='index'
  349. ORDER BY name;
  350. }
  351. } {i2x i2y t1 t2}
  352. do_test avtrans-5.17 {
  353. execsql {
  354. COMMIT;
  355. SELECT name fROM sqlite_master
  356. WHERE type='table' OR type='index'
  357. ORDER BY name;
  358. }
  359. } {i2x i2y t1 t2}
  360. do_test avtrans-5.18 {
  361. execsql {
  362. SELECT * FROM t2;
  363. }
  364. } {1 2 3}
  365. do_test avtrans-5.19 {
  366. execsql {
  367. SELECT x FROM t2 WHERE y=2;
  368. }
  369. } {1}
  370. do_test avtrans-5.20 {
  371. execsql {
  372. BEGIN TRANSACTION;
  373. DROP TABLE t1;
  374. DROP TABLE t2;
  375. SELECT name fROM sqlite_master
  376. WHERE type='table' OR type='index'
  377. ORDER BY name;
  378. }
  379. } {}
  380. do_test avtrans-5.21 {
  381. set r [catch {execsql {
  382. SELECT * FROM t2
  383. }} msg]
  384. lappend r $msg
  385. } {1 {no such table: t2}}
  386. do_test avtrans-5.22 {
  387. execsql {
  388. ROLLBACK;
  389. SELECT name fROM sqlite_master
  390. WHERE type='table' OR type='index'
  391. ORDER BY name;
  392. }
  393. } {i2x i2y t1 t2}
  394. do_test avtrans-5.23 {
  395. execsql {
  396. SELECT * FROM t2;
  397. }
  398. } {1 2 3}
  399. integrity_check avtrans-5.23
  400. # Try to DROP and CREATE tables and indices with the same name
  401. # within a transaction. Make sure ROLLBACK works.
  402. #
  403. do_test avtrans-6.1 {
  404. execsql2 {
  405. INSERT INTO t1 VALUES(1,2,3);
  406. BEGIN TRANSACTION;
  407. DROP TABLE t1;
  408. CREATE TABLE t1(p,q,r);
  409. ROLLBACK;
  410. SELECT * FROM t1;
  411. }
  412. } {a 1 b 2 c 3}
  413. do_test avtrans-6.2 {
  414. execsql2 {
  415. INSERT INTO t1 VALUES(1,2,3);
  416. BEGIN TRANSACTION;
  417. DROP TABLE t1;
  418. CREATE TABLE t1(p,q,r);
  419. COMMIT;
  420. SELECT * FROM t1;
  421. }
  422. } {}
  423. do_test avtrans-6.3 {
  424. execsql2 {
  425. INSERT INTO t1 VALUES(1,2,3);
  426. SELECT * FROM t1;
  427. }
  428. } {p 1 q 2 r 3}
  429. do_test avtrans-6.4 {
  430. execsql2 {
  431. BEGIN TRANSACTION;
  432. DROP TABLE t1;
  433. CREATE TABLE t1(a,b,c);
  434. INSERT INTO t1 VALUES(4,5,6);
  435. SELECT * FROM t1;
  436. DROP TABLE t1;
  437. }
  438. } {a 4 b 5 c 6}
  439. do_test avtrans-6.5 {
  440. execsql2 {
  441. ROLLBACK;
  442. SELECT * FROM t1;
  443. }
  444. } {p 1 q 2 r 3}
  445. do_test avtrans-6.6 {
  446. execsql2 {
  447. BEGIN TRANSACTION;
  448. DROP TABLE t1;
  449. CREATE TABLE t1(a,b,c);
  450. INSERT INTO t1 VALUES(4,5,6);
  451. SELECT * FROM t1;
  452. DROP TABLE t1;
  453. }
  454. } {a 4 b 5 c 6}
  455. do_test avtrans-6.7 {
  456. catchsql {
  457. COMMIT;
  458. SELECT * FROM t1;
  459. }
  460. } {1 {no such table: t1}}
  461. # Repeat on a table with an automatically generated index.
  462. #
  463. do_test avtrans-6.10 {
  464. execsql2 {
  465. CREATE TABLE t1(a unique,b,c);
  466. INSERT INTO t1 VALUES(1,2,3);
  467. BEGIN TRANSACTION;
  468. DROP TABLE t1;
  469. CREATE TABLE t1(p unique,q,r);
  470. ROLLBACK;
  471. SELECT * FROM t1;
  472. }
  473. } {a 1 b 2 c 3}
  474. do_test avtrans-6.11 {
  475. execsql2 {
  476. BEGIN TRANSACTION;
  477. DROP TABLE t1;
  478. CREATE TABLE t1(p unique,q,r);
  479. COMMIT;
  480. SELECT * FROM t1;
  481. }
  482. } {}
  483. do_test avtrans-6.12 {
  484. execsql2 {
  485. INSERT INTO t1 VALUES(1,2,3);
  486. SELECT * FROM t1;
  487. }
  488. } {p 1 q 2 r 3}
  489. do_test avtrans-6.13 {
  490. execsql2 {
  491. BEGIN TRANSACTION;
  492. DROP TABLE t1;
  493. CREATE TABLE t1(a unique,b,c);
  494. INSERT INTO t1 VALUES(4,5,6);
  495. SELECT * FROM t1;
  496. DROP TABLE t1;
  497. }
  498. } {a 4 b 5 c 6}
  499. do_test avtrans-6.14 {
  500. execsql2 {
  501. ROLLBACK;
  502. SELECT * FROM t1;
  503. }
  504. } {p 1 q 2 r 3}
  505. do_test avtrans-6.15 {
  506. execsql2 {
  507. BEGIN TRANSACTION;
  508. DROP TABLE t1;
  509. CREATE TABLE t1(a unique,b,c);
  510. INSERT INTO t1 VALUES(4,5,6);
  511. SELECT * FROM t1;
  512. DROP TABLE t1;
  513. }
  514. } {a 4 b 5 c 6}
  515. do_test avtrans-6.16 {
  516. catchsql {
  517. COMMIT;
  518. SELECT * FROM t1;
  519. }
  520. } {1 {no such table: t1}}
  521. do_test avtrans-6.20 {
  522. execsql {
  523. CREATE TABLE t1(a integer primary key,b,c);
  524. INSERT INTO t1 VALUES(1,-2,-3);
  525. INSERT INTO t1 VALUES(4,-5,-6);
  526. SELECT * FROM t1;
  527. }
  528. } {1 -2 -3 4 -5 -6}
  529. do_test avtrans-6.21 {
  530. execsql {
  531. CREATE INDEX i1 ON t1(b);
  532. SELECT * FROM t1 WHERE b<1;
  533. }
  534. } {4 -5 -6 1 -2 -3}
  535. do_test avtrans-6.22 {
  536. execsql {
  537. BEGIN TRANSACTION;
  538. DROP INDEX i1;
  539. SELECT * FROM t1 WHERE b<1;
  540. ROLLBACK;
  541. }
  542. } {1 -2 -3 4 -5 -6}
  543. do_test avtrans-6.23 {
  544. execsql {
  545. SELECT * FROM t1 WHERE b<1;
  546. }
  547. } {4 -5 -6 1 -2 -3}
  548. do_test avtrans-6.24 {
  549. execsql {
  550. BEGIN TRANSACTION;
  551. DROP TABLE t1;
  552. ROLLBACK;
  553. SELECT * FROM t1 WHERE b<1;
  554. }
  555. } {4 -5 -6 1 -2 -3}
  556. do_test avtrans-6.25 {
  557. execsql {
  558. BEGIN TRANSACTION;
  559. DROP INDEX i1;
  560. CREATE INDEX i1 ON t1(c);
  561. SELECT * FROM t1 WHERE b<1;
  562. }
  563. } {1 -2 -3 4 -5 -6}
  564. do_test avtrans-6.26 {
  565. execsql {
  566. SELECT * FROM t1 WHERE c<1;
  567. }
  568. } {4 -5 -6 1 -2 -3}
  569. do_test avtrans-6.27 {
  570. execsql {
  571. ROLLBACK;
  572. SELECT * FROM t1 WHERE b<1;
  573. }
  574. } {4 -5 -6 1 -2 -3}
  575. do_test avtrans-6.28 {
  576. execsql {
  577. SELECT * FROM t1 WHERE c<1;
  578. }
  579. } {1 -2 -3 4 -5 -6}
  580. # The following repeats steps 6.20 through 6.28, but puts a "unique"
  581. # constraint the first field of the table in order to generate an
  582. # automatic index.
  583. #
  584. do_test avtrans-6.30 {
  585. execsql {
  586. BEGIN TRANSACTION;
  587. DROP TABLE t1;
  588. CREATE TABLE t1(a int unique,b,c);
  589. COMMIT;
  590. INSERT INTO t1 VALUES(1,-2,-3);
  591. INSERT INTO t1 VALUES(4,-5,-6);
  592. SELECT * FROM t1 ORDER BY a;
  593. }
  594. } {1 -2 -3 4 -5 -6}
  595. do_test avtrans-6.31 {
  596. execsql {
  597. CREATE INDEX i1 ON t1(b);
  598. SELECT * FROM t1 WHERE b<1;
  599. }
  600. } {4 -5 -6 1 -2 -3}
  601. do_test avtrans-6.32 {
  602. execsql {
  603. BEGIN TRANSACTION;
  604. DROP INDEX i1;
  605. SELECT * FROM t1 WHERE b<1;
  606. ROLLBACK;
  607. }
  608. } {1 -2 -3 4 -5 -6}
  609. do_test avtrans-6.33 {
  610. execsql {
  611. SELECT * FROM t1 WHERE b<1;
  612. }
  613. } {4 -5 -6 1 -2 -3}
  614. do_test avtrans-6.34 {
  615. execsql {
  616. BEGIN TRANSACTION;
  617. DROP TABLE t1;
  618. ROLLBACK;
  619. SELECT * FROM t1 WHERE b<1;
  620. }
  621. } {4 -5 -6 1 -2 -3}
  622. do_test avtrans-6.35 {
  623. execsql {
  624. BEGIN TRANSACTION;
  625. DROP INDEX i1;
  626. CREATE INDEX i1 ON t1(c);
  627. SELECT * FROM t1 WHERE b<1;
  628. }
  629. } {1 -2 -3 4 -5 -6}
  630. do_test avtrans-6.36 {
  631. execsql {
  632. SELECT * FROM t1 WHERE c<1;
  633. }
  634. } {4 -5 -6 1 -2 -3}
  635. do_test avtrans-6.37 {
  636. execsql {
  637. DROP INDEX i1;
  638. SELECT * FROM t1 WHERE c<1;
  639. }
  640. } {1 -2 -3 4 -5 -6}
  641. do_test avtrans-6.38 {
  642. execsql {
  643. ROLLBACK;
  644. SELECT * FROM t1 WHERE b<1;
  645. }
  646. } {4 -5 -6 1 -2 -3}
  647. do_test avtrans-6.39 {
  648. execsql {
  649. SELECT * FROM t1 WHERE c<1;
  650. }
  651. } {1 -2 -3 4 -5 -6}
  652. integrity_check avtrans-6.40
  653. ifcapable !floatingpoint {
  654. finish_test
  655. return
  656. }
  657. # Test to make sure rollback restores the database back to its original
  658. # state.
  659. #
  660. do_test avtrans-7.1 {
  661. execsql {BEGIN}
  662. for {set i 0} {$i<1000} {incr i} {
  663. set r1 [expr {rand()}]
  664. set r2 [expr {rand()}]
  665. set r3 [expr {rand()}]
  666. execsql "INSERT INTO t2 VALUES($r1,$r2,$r3)"
  667. }
  668. execsql {COMMIT}
  669. set ::checksum [execsql {SELECT md5sum(x,y,z) FROM t2}]
  670. set ::checksum2 [
  671. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  672. ]
  673. execsql {SELECT count(*) FROM t2}
  674. } {1001}
  675. do_test avtrans-7.2 {
  676. execsql {SELECT md5sum(x,y,z) FROM t2}
  677. } $checksum
  678. do_test avtrans-7.2.1 {
  679. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  680. } $checksum2
  681. do_test avtrans-7.3 {
  682. execsql {
  683. BEGIN;
  684. DELETE FROM t2;
  685. ROLLBACK;
  686. SELECT md5sum(x,y,z) FROM t2;
  687. }
  688. } $checksum
  689. do_test avtrans-7.4 {
  690. execsql {
  691. BEGIN;
  692. INSERT INTO t2 SELECT * FROM t2;
  693. ROLLBACK;
  694. SELECT md5sum(x,y,z) FROM t2;
  695. }
  696. } $checksum
  697. do_test avtrans-7.5 {
  698. execsql {
  699. BEGIN;
  700. DELETE FROM t2;
  701. ROLLBACK;
  702. SELECT md5sum(x,y,z) FROM t2;
  703. }
  704. } $checksum
  705. do_test avtrans-7.6 {
  706. execsql {
  707. BEGIN;
  708. INSERT INTO t2 SELECT * FROM t2;
  709. ROLLBACK;
  710. SELECT md5sum(x,y,z) FROM t2;
  711. }
  712. } $checksum
  713. do_test avtrans-7.7 {
  714. execsql {
  715. BEGIN;
  716. CREATE TABLE t3 AS SELECT * FROM t2;
  717. INSERT INTO t2 SELECT * FROM t3;
  718. ROLLBACK;
  719. SELECT md5sum(x,y,z) FROM t2;
  720. }
  721. } $checksum
  722. do_test avtrans-7.8 {
  723. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  724. } $checksum2
  725. ifcapable tempdb {
  726. do_test avtrans-7.9 {
  727. execsql {
  728. BEGIN;
  729. CREATE TEMP TABLE t3 AS SELECT * FROM t2;
  730. INSERT INTO t2 SELECT * FROM t3;
  731. ROLLBACK;
  732. SELECT md5sum(x,y,z) FROM t2;
  733. }
  734. } $checksum
  735. }
  736. do_test avtrans-7.10 {
  737. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  738. } $checksum2
  739. ifcapable tempdb {
  740. do_test avtrans-7.11 {
  741. execsql {
  742. BEGIN;
  743. CREATE TEMP TABLE t3 AS SELECT * FROM t2;
  744. INSERT INTO t2 SELECT * FROM t3;
  745. DROP INDEX i2x;
  746. DROP INDEX i2y;
  747. CREATE INDEX i3a ON t3(x);
  748. ROLLBACK;
  749. SELECT md5sum(x,y,z) FROM t2;
  750. }
  751. } $checksum
  752. }
  753. do_test avtrans-7.12 {
  754. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  755. } $checksum2
  756. ifcapable tempdb {
  757. do_test avtrans-7.13 {
  758. execsql {
  759. BEGIN;
  760. DROP TABLE t2;
  761. ROLLBACK;
  762. SELECT md5sum(x,y,z) FROM t2;
  763. }
  764. } $checksum
  765. }
  766. do_test avtrans-7.14 {
  767. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  768. } $checksum2
  769. integrity_check avtrans-7.15
  770. # Arrange for another process to begin modifying the database but abort
  771. # and die in the middle of the modification. Then have this process read
  772. # the database. This process should detect the journal file and roll it
  773. # back. Verify that this happens correctly.
  774. #
  775. set fd [open test.tcl w]
  776. puts $fd {
  777. sqlite3 db test.db
  778. db eval {
  779. PRAGMA default_cache_size=20;
  780. BEGIN;
  781. CREATE TABLE t3 AS SELECT * FROM t2;
  782. DELETE FROM t2;
  783. }
  784. sqlite_abort
  785. }
  786. close $fd
  787. do_test avtrans-8.1 {
  788. catch {exec [info nameofexec] test.tcl}
  789. execsql {SELECT md5sum(x,y,z) FROM t2}
  790. } $checksum
  791. do_test avtrans-8.2 {
  792. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  793. } $checksum2
  794. integrity_check avtrans-8.3
  795. # In the following sequence of tests, compute the MD5 sum of the content
  796. # of a table, make lots of modifications to that table, then do a rollback.
  797. # Verify that after the rollback, the MD5 checksum is unchanged.
  798. #
  799. do_test avtrans-9.1 {
  800. execsql {
  801. PRAGMA default_cache_size=10;
  802. }
  803. db close
  804. sqlite3 db test.db
  805. execsql {
  806. BEGIN;
  807. CREATE TABLE t3(x TEXT);
  808. INSERT INTO t3 VALUES(randstr(10,400));
  809. INSERT INTO t3 VALUES(randstr(10,400));
  810. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  811. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  812. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  813. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  814. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  815. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  816. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  817. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  818. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  819. COMMIT;
  820. SELECT count(*) FROM t3;
  821. }
  822. } {1024}
  823. # The following procedure computes a "signature" for table "t3". If
  824. # T3 changes in any way, the signature should change.
  825. #
  826. # This is used to test ROLLBACK. We gather a signature for t3, then
  827. # make lots of changes to t3, then rollback and take another signature.
  828. # The two signatures should be the same.
  829. #
  830. proc signature {} {
  831. return [db eval {SELECT count(*), md5sum(x) FROM t3}]
  832. }
  833. # Repeat the following group of tests 20 times for quick testing and
  834. # 40 times for full testing. Each iteration of the test makes table
  835. # t3 a little larger, and thus takes a little longer, so doing 40 tests
  836. # is more than 2.0 times slower than doing 20 tests. Considerably more.
  837. #
  838. if {[info exists G(isquick)]} {
  839. set limit 20
  840. } else {
  841. set limit 40
  842. }
  843. # Do rollbacks. Make sure the signature does not change.
  844. #
  845. for {set i 2} {$i<=$limit} {incr i} {
  846. set ::sig [signature]
  847. set cnt [lindex $::sig 0]
  848. if {$i%2==0} {
  849. execsql {PRAGMA fullfsync=ON}
  850. } else {
  851. execsql {PRAGMA fullfsync=OFF}
  852. }
  853. set sqlite_sync_count 0
  854. set sqlite_fullsync_count 0
  855. do_test avtrans-9.$i.1-$cnt {
  856. execsql {
  857. BEGIN;
  858. DELETE FROM t3 WHERE random()%10!=0;
  859. INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
  860. INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
  861. ROLLBACK;
  862. }
  863. signature
  864. } $sig
  865. do_test avtrans-9.$i.2-$cnt {
  866. execsql {
  867. BEGIN;
  868. DELETE FROM t3 WHERE random()%10!=0;
  869. INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
  870. DELETE FROM t3 WHERE random()%10!=0;
  871. INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
  872. ROLLBACK;
  873. }
  874. signature
  875. } $sig
  876. if {$i<$limit} {
  877. do_test avtrans-9.$i.3-$cnt {
  878. execsql {
  879. INSERT INTO t3 SELECT randstr(10,400) FROM t3 WHERE random()%10==0;
  880. }
  881. } {}
  882. if {$tcl_platform(platform)=="unix"} {
  883. do_test avtrans-9.$i.4-$cnt {
  884. expr {$sqlite_sync_count>0}
  885. } 1
  886. ifcapable pager_pragmas {
  887. do_test avtrans-9.$i.5-$cnt {
  888. expr {$sqlite_fullsync_count>0}
  889. } [expr {$i%2==0}]
  890. } else {
  891. do_test avtrans-9.$i.5-$cnt {
  892. expr {$sqlite_fullsync_count==0}
  893. } {1}
  894. }
  895. }
  896. wal_check_journal_mode avtrans-9.$i-6.$cnt
  897. }
  898. set ::pager_old_format 0
  899. }
  900. integrity_check avtrans-10.1
  901. wal_check_journal_mode avtrans-10.2
  902. finish_test