trans.test 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. The
  12. # focus of this script is database locks.
  13. #
  14. # $Id: trans.test,v 1.11 2002/03/11 02:06:14 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Create several tables to work with.
  18. #
  19. do_test trans-1.0 {
  20. execsql {
  21. CREATE TABLE one(a int PRIMARY KEY, b text);
  22. INSERT INTO one VALUES(1,'one');
  23. INSERT INTO one VALUES(2,'two');
  24. INSERT INTO one VALUES(3,'three');
  25. SELECT b FROM one ORDER BY a;
  26. }
  27. } {one two three}
  28. do_test trans-1.1 {
  29. execsql {
  30. CREATE TABLE two(a int PRIMARY KEY, b text);
  31. INSERT INTO two VALUES(1,'I');
  32. INSERT INTO two VALUES(5,'V');
  33. INSERT INTO two VALUES(10,'X');
  34. SELECT b FROM two ORDER BY a;
  35. }
  36. } {I V X}
  37. do_test trans-1.9 {
  38. sqlite altdb test.db
  39. execsql {SELECT b FROM one ORDER BY a} altdb
  40. } {one two three}
  41. do_test trans-1.10 {
  42. execsql {SELECT b FROM two ORDER BY a} altdb
  43. } {I V X}
  44. # Basic transactions
  45. #
  46. do_test trans-2.1 {
  47. set v [catch {execsql {BEGIN}} msg]
  48. lappend v $msg
  49. } {0 {}}
  50. do_test trans-2.2 {
  51. set v [catch {execsql {END}} msg]
  52. lappend v $msg
  53. } {0 {}}
  54. do_test trans-2.3 {
  55. set v [catch {execsql {BEGIN TRANSACTION}} msg]
  56. lappend v $msg
  57. } {0 {}}
  58. do_test trans-2.4 {
  59. set v [catch {execsql {COMMIT TRANSACTION}} msg]
  60. lappend v $msg
  61. } {0 {}}
  62. do_test trans-2.5 {
  63. set v [catch {execsql {BEGIN TRANSACTION 'foo'}} msg]
  64. lappend v $msg
  65. } {0 {}}
  66. do_test trans-2.6 {
  67. set v [catch {execsql {ROLLBACK TRANSACTION 'foo'}} msg]
  68. lappend v $msg
  69. } {0 {}}
  70. do_test trans-2.10 {
  71. execsql {
  72. BEGIN;
  73. SELECT a FROM one ORDER BY a;
  74. SELECT a FROM two ORDER BY a;
  75. END;
  76. }
  77. } {1 2 3 1 5 10}
  78. # Check the locking behavior
  79. #
  80. do_test trans-3.1 {
  81. execsql {
  82. BEGIN;
  83. SELECT a FROM one ORDER BY a;
  84. }
  85. } {1 2 3}
  86. do_test trans-3.2 {
  87. set v [catch {execsql {
  88. SELECT a FROM two ORDER BY a;
  89. } altdb} msg]
  90. lappend v $msg
  91. } {1 {database is locked}}
  92. do_test trans-3.3 {
  93. set v [catch {execsql {
  94. SELECT a FROM one ORDER BY a;
  95. } altdb} msg]
  96. lappend v $msg
  97. } {1 {database is locked}}
  98. do_test trans-3.4 {
  99. set v [catch {execsql {
  100. INSERT INTO one VALUES(4,'four');
  101. }} msg]
  102. lappend v $msg
  103. } {0 {}}
  104. do_test trans-3.5 {
  105. set v [catch {execsql {
  106. SELECT a FROM two ORDER BY a;
  107. } altdb} msg]
  108. lappend v $msg
  109. } {1 {database is locked}}
  110. do_test trans-3.6 {
  111. set v [catch {execsql {
  112. SELECT a FROM one ORDER BY a;
  113. } altdb} msg]
  114. lappend v $msg
  115. } {1 {database is locked}}
  116. do_test trans-3.7 {
  117. set v [catch {execsql {
  118. INSERT INTO two VALUES(4,'IV');
  119. }} msg]
  120. lappend v $msg
  121. } {0 {}}
  122. do_test trans-3.8 {
  123. set v [catch {execsql {
  124. SELECT a FROM two ORDER BY a;
  125. } altdb} msg]
  126. lappend v $msg
  127. } {1 {database is locked}}
  128. do_test trans-3.9 {
  129. set v [catch {execsql {
  130. SELECT a FROM one ORDER BY a;
  131. } altdb} msg]
  132. lappend v $msg
  133. } {1 {database is locked}}
  134. do_test trans-3.10 {
  135. execsql {END TRANSACTION}
  136. } {}
  137. do_test trans-3.11 {
  138. set v [catch {execsql {
  139. SELECT a FROM two ORDER BY a;
  140. } altdb} msg]
  141. lappend v $msg
  142. } {0 {1 4 5 10}}
  143. do_test trans-3.12 {
  144. set v [catch {execsql {
  145. SELECT a FROM one ORDER BY a;
  146. } altdb} msg]
  147. lappend v $msg
  148. } {0 {1 2 3 4}}
  149. do_test trans-3.13 {
  150. set v [catch {execsql {
  151. SELECT a FROM two ORDER BY a;
  152. } db} msg]
  153. lappend v $msg
  154. } {0 {1 4 5 10}}
  155. do_test trans-3.14 {
  156. set v [catch {execsql {
  157. SELECT a FROM one ORDER BY a;
  158. } db} msg]
  159. lappend v $msg
  160. } {0 {1 2 3 4}}
  161. do_test trans-4.1 {
  162. set v [catch {execsql {
  163. COMMIT;
  164. } db} msg]
  165. lappend v $msg
  166. } {0 {}}
  167. do_test trans-4.2 {
  168. set v [catch {execsql {
  169. ROLLBACK;
  170. } db} msg]
  171. lappend v $msg
  172. } {0 {}}
  173. do_test trans-4.3 {
  174. set v [catch {execsql {
  175. BEGIN TRANSACTION;
  176. SELECT a FROM two ORDER BY a;
  177. } db} msg]
  178. lappend v $msg
  179. } {0 {1 4 5 10}}
  180. do_test trans-4.4 {
  181. set v [catch {execsql {
  182. SELECT a FROM two ORDER BY a;
  183. } altdb} msg]
  184. lappend v $msg
  185. } {1 {database is locked}}
  186. do_test trans-4.5 {
  187. set v [catch {execsql {
  188. SELECT a FROM one ORDER BY a;
  189. } altdb} msg]
  190. lappend v $msg
  191. } {1 {database is locked}}
  192. do_test trans-4.6 {
  193. set v [catch {execsql {
  194. BEGIN TRANSACTION;
  195. SELECT a FROM one ORDER BY a;
  196. } db} msg]
  197. lappend v $msg
  198. } {0 {1 2 3 4}}
  199. do_test trans-4.7 {
  200. set v [catch {execsql {
  201. SELECT a FROM two ORDER BY a;
  202. } altdb} msg]
  203. lappend v $msg
  204. } {1 {database is locked}}
  205. do_test trans-4.8 {
  206. set v [catch {execsql {
  207. SELECT a FROM one ORDER BY a;
  208. } altdb} msg]
  209. lappend v $msg
  210. } {1 {database is locked}}
  211. do_test trans-4.9 {
  212. set v [catch {execsql {
  213. END TRANSACTION;
  214. SELECT a FROM two ORDER BY a;
  215. } db} msg]
  216. lappend v $msg
  217. } {0 {1 4 5 10}}
  218. do_test trans-4.10 {
  219. set v [catch {execsql {
  220. SELECT a FROM two ORDER BY a;
  221. } altdb} msg]
  222. lappend v $msg
  223. } {0 {1 4 5 10}}
  224. do_test trans-4.11 {
  225. set v [catch {execsql {
  226. SELECT a FROM one ORDER BY a;
  227. } altdb} msg]
  228. lappend v $msg
  229. } {0 {1 2 3 4}}
  230. do_test trans-4.99 {
  231. altdb close
  232. execsql {
  233. DROP TABLE one;
  234. DROP TABLE two;
  235. }
  236. } {}
  237. # Check out the commit/rollback behavior of the database
  238. #
  239. do_test trans-5.1 {
  240. execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
  241. } {}
  242. do_test trans-5.2 {
  243. execsql {BEGIN TRANSACTION}
  244. execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
  245. } {}
  246. do_test trans-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 trans-5.4 {
  251. execsql {SELECT a,b FROM one ORDER BY b}
  252. } {}
  253. do_test trans-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 trans-5.6 {
  258. execsql {ROLLBACK}
  259. execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
  260. } {}
  261. do_test trans-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 trans-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 trans-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 trans-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 trans-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 trans-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 trans-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 trans-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 trans-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 trans-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 trans-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 trans-5.18 {
  361. execsql {
  362. SELECT * FROM t2;
  363. }
  364. } {1 2 3}
  365. do_test trans-5.19 {
  366. execsql {
  367. SELECT x FROM t2 WHERE y=2;
  368. }
  369. } {1}
  370. do_test trans-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 trans-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 trans-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 trans-5.23 {
  395. execsql {
  396. SELECT * FROM t2;
  397. }
  398. } {1 2 3}
  399. # Try to DROP and CREATE tables and indices with the same name
  400. # within a transaction. Make sure ROLLBACK works.
  401. #
  402. do_test trans-6.1 {
  403. execsql2 {
  404. INSERT INTO t1 VALUES(1,2,3);
  405. BEGIN TRANSACTION;
  406. DROP TABLE t1;
  407. CREATE TABLE t1(p,q,r);
  408. ROLLBACK;
  409. SELECT * FROM t1;
  410. }
  411. } {a 1 b 2 c 3}
  412. do_test trans-6.2 {
  413. execsql2 {
  414. INSERT INTO t1 VALUES(1,2,3);
  415. BEGIN TRANSACTION;
  416. DROP TABLE t1;
  417. CREATE TABLE t1(p,q,r);
  418. COMMIT;
  419. SELECT * FROM t1;
  420. }
  421. } {}
  422. do_test trans-6.3 {
  423. execsql2 {
  424. INSERT INTO t1 VALUES(1,2,3);
  425. SELECT * FROM t1;
  426. }
  427. } {p 1 q 2 r 3}
  428. do_test trans-6.4 {
  429. execsql2 {
  430. BEGIN TRANSACTION;
  431. DROP TABLE t1;
  432. CREATE TABLE t1(a,b,c);
  433. INSERT INTO t1 VALUES(4,5,6);
  434. SELECT * FROM t1;
  435. DROP TABLE t1;
  436. }
  437. } {a 4 b 5 c 6}
  438. do_test trans-6.5 {
  439. execsql2 {
  440. ROLLBACK;
  441. SELECT * FROM t1;
  442. }
  443. } {p 1 q 2 r 3}
  444. do_test trans-6.6 {
  445. execsql2 {
  446. BEGIN TRANSACTION;
  447. DROP TABLE t1;
  448. CREATE TABLE t1(a,b,c);
  449. INSERT INTO t1 VALUES(4,5,6);
  450. SELECT * FROM t1;
  451. DROP TABLE t1;
  452. }
  453. } {a 4 b 5 c 6}
  454. do_test trans-6.7 {
  455. catchsql {
  456. COMMIT;
  457. SELECT * FROM t1;
  458. }
  459. } {1 {no such table: t1}}
  460. # Repeat on a table with an automatically generated index.
  461. #
  462. do_test trans-6.10 {
  463. execsql2 {
  464. CREATE TABLE t1(a unique,b,c);
  465. INSERT INTO t1 VALUES(1,2,3);
  466. BEGIN TRANSACTION;
  467. DROP TABLE t1;
  468. CREATE TABLE t1(p unique,q,r);
  469. ROLLBACK;
  470. SELECT * FROM t1;
  471. }
  472. } {a 1 b 2 c 3}
  473. do_test trans-6.11 {
  474. execsql2 {
  475. BEGIN TRANSACTION;
  476. DROP TABLE t1;
  477. CREATE TABLE t1(p unique,q,r);
  478. COMMIT;
  479. SELECT * FROM t1;
  480. }
  481. } {}
  482. do_test trans-6.12 {
  483. execsql2 {
  484. INSERT INTO t1 VALUES(1,2,3);
  485. SELECT * FROM t1;
  486. }
  487. } {p 1 q 2 r 3}
  488. do_test trans-6.13 {
  489. execsql2 {
  490. BEGIN TRANSACTION;
  491. DROP TABLE t1;
  492. CREATE TABLE t1(a unique,b,c);
  493. INSERT INTO t1 VALUES(4,5,6);
  494. SELECT * FROM t1;
  495. DROP TABLE t1;
  496. }
  497. } {a 4 b 5 c 6}
  498. do_test trans-6.14 {
  499. execsql2 {
  500. ROLLBACK;
  501. SELECT * FROM t1;
  502. }
  503. } {p 1 q 2 r 3}
  504. do_test trans-6.15 {
  505. execsql2 {
  506. BEGIN TRANSACTION;
  507. DROP TABLE t1;
  508. CREATE TABLE t1(a unique,b,c);
  509. INSERT INTO t1 VALUES(4,5,6);
  510. SELECT * FROM t1;
  511. DROP TABLE t1;
  512. }
  513. } {a 4 b 5 c 6}
  514. do_test trans-6.16 {
  515. catchsql {
  516. COMMIT;
  517. SELECT * FROM t1;
  518. }
  519. } {1 {no such table: t1}}
  520. do_test trans-6.20 {
  521. execsql {
  522. CREATE TABLE t1(a integer primary key,b,c);
  523. INSERT INTO t1 VALUES(1,-2,-3);
  524. INSERT INTO t1 VALUES(4,-5,-6);
  525. SELECT * FROM t1;
  526. }
  527. } {1 -2 -3 4 -5 -6}
  528. do_test trans-6.21 {
  529. execsql {
  530. CREATE INDEX i1 ON t1(b);
  531. SELECT * FROM t1 WHERE b<1;
  532. }
  533. } {4 -5 -6 1 -2 -3}
  534. do_test trans-6.22 {
  535. execsql {
  536. BEGIN TRANSACTION;
  537. DROP INDEX i1;
  538. SELECT * FROM t1 WHERE b<1;
  539. ROLLBACK;
  540. }
  541. } {1 -2 -3 4 -5 -6}
  542. do_test trans-6.23 {
  543. execsql {
  544. SELECT * FROM t1 WHERE b<1;
  545. }
  546. } {4 -5 -6 1 -2 -3}
  547. do_test trans-6.24 {
  548. execsql {
  549. BEGIN TRANSACTION;
  550. DROP TABLE t1;
  551. ROLLBACK;
  552. SELECT * FROM t1 WHERE b<1;
  553. }
  554. } {4 -5 -6 1 -2 -3}
  555. do_test trans-6.25 {
  556. execsql {
  557. BEGIN TRANSACTION;
  558. DROP INDEX i1;
  559. CREATE INDEX i1 ON t1(c);
  560. SELECT * FROM t1 WHERE b<1;
  561. }
  562. } {1 -2 -3 4 -5 -6}
  563. do_test trans-6.26 {
  564. execsql {
  565. SELECT * FROM t1 WHERE c<1;
  566. }
  567. } {4 -5 -6 1 -2 -3}
  568. do_test trans-6.27 {
  569. execsql {
  570. ROLLBACK;
  571. SELECT * FROM t1 WHERE b<1;
  572. }
  573. } {4 -5 -6 1 -2 -3}
  574. do_test trans-6.28 {
  575. execsql {
  576. SELECT * FROM t1 WHERE c<1;
  577. }
  578. } {1 -2 -3 4 -5 -6}
  579. # The following repeats steps 6.20 through 6.28, but puts a "unique"
  580. # constraint the first field of the table in order to generate an
  581. # automatic index.
  582. #
  583. do_test trans-6.30 {
  584. execsql {
  585. BEGIN TRANSACTION;
  586. DROP TABLE t1;
  587. CREATE TABLE t1(a int unique,b,c);
  588. COMMIT;
  589. INSERT INTO t1 VALUES(1,-2,-3);
  590. INSERT INTO t1 VALUES(4,-5,-6);
  591. SELECT * FROM t1 ORDER BY a;
  592. }
  593. } {1 -2 -3 4 -5 -6}
  594. do_test trans-6.31 {
  595. execsql {
  596. CREATE INDEX i1 ON t1(b);
  597. SELECT * FROM t1 WHERE b<1;
  598. }
  599. } {4 -5 -6 1 -2 -3}
  600. do_test trans-6.32 {
  601. execsql {
  602. BEGIN TRANSACTION;
  603. DROP INDEX i1;
  604. SELECT * FROM t1 WHERE b<1;
  605. ROLLBACK;
  606. }
  607. } {1 -2 -3 4 -5 -6}
  608. do_test trans-6.33 {
  609. execsql {
  610. SELECT * FROM t1 WHERE b<1;
  611. }
  612. } {4 -5 -6 1 -2 -3}
  613. do_test trans-6.34 {
  614. execsql {
  615. BEGIN TRANSACTION;
  616. DROP TABLE t1;
  617. ROLLBACK;
  618. SELECT * FROM t1 WHERE b<1;
  619. }
  620. } {4 -5 -6 1 -2 -3}
  621. do_test trans-6.35 {
  622. execsql {
  623. BEGIN TRANSACTION;
  624. DROP INDEX i1;
  625. CREATE INDEX i1 ON t1(c);
  626. SELECT * FROM t1 WHERE b<1;
  627. }
  628. } {1 -2 -3 4 -5 -6}
  629. do_test trans-6.36 {
  630. execsql {
  631. SELECT * FROM t1 WHERE c<1;
  632. }
  633. } {4 -5 -6 1 -2 -3}
  634. do_test trans-6.37 {
  635. execsql {
  636. DROP INDEX i1;
  637. SELECT * FROM t1 WHERE c<1;
  638. }
  639. } {1 -2 -3 4 -5 -6}
  640. do_test trans-6.38 {
  641. execsql {
  642. ROLLBACK;
  643. SELECT * FROM t1 WHERE b<1;
  644. }
  645. } {4 -5 -6 1 -2 -3}
  646. do_test trans-6.39 {
  647. execsql {
  648. SELECT * FROM t1 WHERE c<1;
  649. }
  650. } {1 -2 -3 4 -5 -6}
  651. # Test to make sure rollback restores the database back to its original
  652. # state.
  653. #
  654. do_test trans-7.1 {
  655. execsql {BEGIN}
  656. for {set i 0} {$i<1000} {incr i} {
  657. set r1 [expr {rand()}]
  658. set r2 [expr {rand()}]
  659. set r3 [expr {rand()}]
  660. execsql "INSERT INTO t2 VALUES($r1,$r2,$r3)"
  661. }
  662. execsql {COMMIT}
  663. set ::checksum [execsql {SELECT md5sum(x,y,z) FROM t2}]
  664. set ::checksum2 [
  665. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  666. ]
  667. execsql {SELECT count(*) FROM t2}
  668. } {1001}
  669. do_test trans-7.2 {
  670. execsql {SELECT md5sum(x,y,z) FROM t2}
  671. } $checksum
  672. do_test trans-7.2.1 {
  673. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  674. } $checksum2
  675. do_test trans-7.3 {
  676. execsql {
  677. BEGIN;
  678. DELETE FROM t2;
  679. ROLLBACK;
  680. SELECT md5sum(x,y,z) FROM t2;
  681. }
  682. } $checksum
  683. do_test trans-7.4 {
  684. execsql {
  685. BEGIN;
  686. INSERT INTO t2 SELECT * FROM t2;
  687. ROLLBACK;
  688. SELECT md5sum(x,y,z) FROM t2;
  689. }
  690. } $checksum
  691. do_test trans-7.5 {
  692. execsql {
  693. BEGIN;
  694. DELETE FROM t2;
  695. ROLLBACK;
  696. SELECT md5sum(x,y,z) FROM t2;
  697. }
  698. } $checksum
  699. do_test trans-7.6 {
  700. execsql {
  701. BEGIN;
  702. INSERT INTO t2 SELECT * FROM t2;
  703. ROLLBACK;
  704. SELECT md5sum(x,y,z) FROM t2;
  705. }
  706. } $checksum
  707. do_test trans-7.7 {
  708. execsql {
  709. BEGIN;
  710. CREATE TABLE t3 AS SELECT * FROM t2;
  711. INSERT INTO t2 SELECT * FROM t3;
  712. ROLLBACK;
  713. SELECT md5sum(x,y,z) FROM t2;
  714. }
  715. } $checksum
  716. do_test trans-7.8 {
  717. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  718. } $checksum2
  719. do_test trans-7.9 {
  720. execsql {
  721. BEGIN;
  722. CREATE TEMP TABLE t3 AS SELECT * FROM t2;
  723. INSERT INTO t2 SELECT * FROM t3;
  724. ROLLBACK;
  725. SELECT md5sum(x,y,z) FROM t2;
  726. }
  727. } $checksum
  728. do_test trans-7.10 {
  729. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  730. } $checksum2
  731. do_test trans-7.11 {
  732. execsql {
  733. BEGIN;
  734. CREATE TEMP TABLE t3 AS SELECT * FROM t2;
  735. INSERT INTO t2 SELECT * FROM t3;
  736. DROP INDEX i2x;
  737. DROP INDEX i2y;
  738. CREATE INDEX i3a ON t3(x);
  739. ROLLBACK;
  740. SELECT md5sum(x,y,z) FROM t2;
  741. }
  742. } $checksum
  743. do_test trans-7.12 {
  744. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  745. } $checksum2
  746. do_test trans-7.13 {
  747. execsql {
  748. BEGIN;
  749. DROP TABLE t2;
  750. ROLLBACK;
  751. SELECT md5sum(x,y,z) FROM t2;
  752. }
  753. } $checksum
  754. do_test trans-7.14 {
  755. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  756. } $checksum2
  757. # Arrange for another process to begin modifying the database but abort
  758. # and die in the middle of the modification. Then have this process read
  759. # the database. This process should detect the journal file and roll it
  760. # back. Verify that this happens correctly.
  761. #
  762. set fd [open test.tcl w]
  763. puts $fd {
  764. sqlite db test.db
  765. db eval {
  766. BEGIN;
  767. CREATE TABLE t3 AS SELECT * FROM t2;
  768. DELETE FROM t2;
  769. }
  770. sqlite_abort
  771. }
  772. close $fd
  773. do_test trans-8.1 {
  774. catch {exec [info nameofexec] test.tcl}
  775. execsql {SELECT md5sum(x,y,z) FROM t2}
  776. } $checksum
  777. do_test trans-8.2 {
  778. execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
  779. } $checksum2
  780. finish_test