session2.test 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. # 2011 Mar 16
  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. #
  12. # The focus of this file is testing the session module.
  13. #
  14. if {![info exists testdir]} {
  15. set testdir [file join [file dirname [info script]] .. .. test]
  16. }
  17. source [file join [file dirname [info script]] session_common.tcl]
  18. source $testdir/tester.tcl
  19. ifcapable !session {finish_test; return}
  20. set testprefix session2
  21. proc test_reset {} {
  22. catch { db close }
  23. catch { db2 close }
  24. forcedelete test.db test.db2
  25. sqlite3 db test.db
  26. sqlite3 db2 test.db2
  27. }
  28. ##########################################################################
  29. # End of proc definitions. Start of tests.
  30. ##########################################################################
  31. test_reset
  32. do_execsql_test 1.0 {
  33. CREATE TABLE t1(a INT PRIMARY KEY, b);
  34. INSERT INTO t1 VALUES('i', 'one');
  35. }
  36. do_iterator_test 1.1 t1 {
  37. DELETE FROM t1 WHERE a = 'i';
  38. INSERT INTO t1 VALUES('ii', 'two');
  39. } {
  40. {DELETE t1 0 X. {t i t one} {}}
  41. {INSERT t1 0 X. {} {t ii t two}}
  42. }
  43. do_iterator_test 1.2 t1 {
  44. INSERT INTO t1 VALUES(1.5, 99.9)
  45. } {
  46. {INSERT t1 0 X. {} {f 1.5 f 99.9}}
  47. }
  48. do_iterator_test 1.3 t1 {
  49. UPDATE t1 SET b = 100.1 WHERE a = 1.5;
  50. UPDATE t1 SET b = 99.9 WHERE a = 1.5;
  51. } { }
  52. do_iterator_test 1.4 t1 {
  53. UPDATE t1 SET b = 100.1 WHERE a = 1.5;
  54. } {
  55. {UPDATE t1 0 X. {f 1.5 f 99.9} {{} {} f 100.1}}
  56. }
  57. # Execute each of the following blocks of SQL on database [db1]. Collect
  58. # changes using a session object. Apply the resulting changeset to
  59. # database [db2]. Then check that the contents of the two databases are
  60. # identical.
  61. #
  62. set set_of_tests {
  63. 1 { INSERT INTO %T1% VALUES(1, 2) }
  64. 2 {
  65. INSERT INTO %T2% VALUES(1, NULL);
  66. INSERT INTO %T2% VALUES(2, NULL);
  67. INSERT INTO %T2% VALUES(3, NULL);
  68. DELETE FROM %T2% WHERE a = 2;
  69. INSERT INTO %T2% VALUES(4, NULL);
  70. UPDATE %T2% SET b=0 WHERE b=1;
  71. }
  72. 3 { INSERT INTO %T3% SELECT *, NULL FROM %T2% }
  73. 4 {
  74. INSERT INTO %T3% SELECT a||a, b||b, NULL FROM %T3%;
  75. DELETE FROM %T3% WHERE rowid%2;
  76. }
  77. 5 { UPDATE %T3% SET c = a||b }
  78. 6 { UPDATE %T1% SET a = 32 }
  79. 7 {
  80. INSERT INTO %T1% SELECT randomblob(32), randomblob(32) FROM %T1%;
  81. INSERT INTO %T1% SELECT randomblob(32), randomblob(32) FROM %T1%;
  82. INSERT INTO %T1% SELECT randomblob(32), randomblob(32) FROM %T1%;
  83. INSERT INTO %T1% SELECT randomblob(32), randomblob(32) FROM %T1%;
  84. INSERT INTO %T1% SELECT randomblob(32), randomblob(32) FROM %T1%;
  85. INSERT INTO %T1% SELECT randomblob(32), randomblob(32) FROM %T1%;
  86. INSERT INTO %T1% SELECT randomblob(32), randomblob(32) FROM %T1%;
  87. INSERT INTO %T1% SELECT randomblob(32), randomblob(32) FROM %T1%;
  88. INSERT INTO %T1% SELECT randomblob(32), randomblob(32) FROM %T1%;
  89. INSERT INTO %T1% SELECT randomblob(32), randomblob(32) FROM %T1%;
  90. INSERT INTO %T1% SELECT randomblob(32), randomblob(32) FROM %T1%;
  91. DELETE FROM %T1% WHERE (rowid%3)==0;
  92. }
  93. 8 {
  94. BEGIN;
  95. INSERT INTO %T1% SELECT randomblob(32), randomblob(32) FROM %T1%;
  96. ROLLBACK;
  97. }
  98. 9 {
  99. BEGIN;
  100. UPDATE %T1% SET b = 'xxx';
  101. ROLLBACK;
  102. }
  103. 10 {
  104. BEGIN;
  105. DELETE FROM %T1% WHERE 1;
  106. ROLLBACK;
  107. }
  108. 11 {
  109. INSERT INTO %T1% VALUES(randomblob(21000), randomblob(0));
  110. INSERT INTO %T1% VALUES(1.5, 1.5);
  111. INSERT INTO %T1% VALUES(4.56, -99.999999999999999999999);
  112. }
  113. 12 {
  114. INSERT INTO %T2% VALUES(NULL, NULL);
  115. }
  116. 13 {
  117. DELETE FROM %T1% WHERE 1;
  118. -- Insert many rows with real primary keys. Enough to force the session
  119. -- objects hash table to resize.
  120. INSERT INTO %T1% VALUES(0.1, 0.1);
  121. INSERT INTO %T1% SELECT a+0.1, b+0.1 FROM %T1%;
  122. INSERT INTO %T1% SELECT a+0.2, b+0.2 FROM %T1%;
  123. INSERT INTO %T1% SELECT a+0.4, b+0.4 FROM %T1%;
  124. INSERT INTO %T1% SELECT a+0.8, b+0.8 FROM %T1%;
  125. INSERT INTO %T1% SELECT a+1.6, b+1.6 FROM %T1%;
  126. INSERT INTO %T1% SELECT a+3.2, b+3.2 FROM %T1%;
  127. INSERT INTO %T1% SELECT a+6.4, b+6.4 FROM %T1%;
  128. INSERT INTO %T1% SELECT a+12.8, b+12.8 FROM %T1%;
  129. INSERT INTO %T1% SELECT a+25.6, b+25.6 FROM %T1%;
  130. INSERT INTO %T1% SELECT a+51.2, b+51.2 FROM %T1%;
  131. INSERT INTO %T1% SELECT a+102.4, b+102.4 FROM %T1%;
  132. INSERT INTO %T1% SELECT a+204.8, b+204.8 FROM %T1%;
  133. }
  134. 14 {
  135. DELETE FROM %T1% WHERE 1;
  136. }
  137. 15 {
  138. INSERT INTO %T1% VALUES(1, 1);
  139. INSERT INTO %T1% SELECT a+2, b+2 FROM %T1%;
  140. INSERT INTO %T1% SELECT a+4, b+4 FROM %T1%;
  141. INSERT INTO %T1% SELECT a+8, b+8 FROM %T1%;
  142. INSERT INTO %T1% SELECT a+256, b+256 FROM %T1%;
  143. }
  144. 16 {
  145. INSERT INTO %T4% VALUES('abc', 'def');
  146. INSERT INTO %T4% VALUES('def', 'abc');
  147. }
  148. 17 { UPDATE %T4% SET b = 1 }
  149. 18 { DELETE FROM %T4% WHERE 1 }
  150. 19 {
  151. INSERT INTO t1 VALUES('', '');
  152. INSERT INTO t1 VALUES(X'', X'');
  153. }
  154. 20 {
  155. DELETE FROM t1;
  156. INSERT INTO t1 VALUES('', NULL);
  157. }
  158. }
  159. test_reset
  160. do_common_sql {
  161. CREATE TABLE t1(a int PRIMARY KEY, b);
  162. CREATE TABLE t2(a, b INTEGER PRIMARY KEY);
  163. CREATE TABLE t3(a, b, c, PRIMARY KEY(a, b));
  164. CREATE TABLE t4(a, b, PRIMARY KEY(b, a));
  165. }
  166. foreach {tn sql} [string map {%T1% t1 %T2% t2 %T3% t3 %T4% t4} $set_of_tests] {
  167. do_then_apply_sql -ignorenoop $sql
  168. do_test 2.$tn { compare_db db db2 } {}
  169. }
  170. # The following block of tests is similar to the last, except that the
  171. # session object is recording changes made to an attached database. The
  172. # main database contains a table of the same name as the table being
  173. # modified within the attached db.
  174. #
  175. test_reset
  176. forcedelete test.db3
  177. sqlite3 db3 test.db3
  178. do_test 3.0 {
  179. execsql {
  180. ATTACH 'test.db3' AS 'aux';
  181. CREATE TABLE t1(a int, b PRIMARY KEY);
  182. CREATE TABLE t2(x, y, z);
  183. CREATE TABLE t3(a);
  184. CREATE TABLE aux.t1(a int PRIMARY KEY, b);
  185. CREATE TABLE aux.t2(a, b INTEGER PRIMARY KEY);
  186. CREATE TABLE aux.t3(a, b, c, PRIMARY KEY(a, b));
  187. CREATE TABLE aux.t4(a, b, PRIMARY KEY(b, a));
  188. }
  189. execsql {
  190. CREATE TABLE t1(a int PRIMARY KEY, b);
  191. CREATE TABLE t2(a, b INTEGER PRIMARY KEY);
  192. CREATE TABLE t3(a, b, c, PRIMARY KEY(a, b));
  193. CREATE TABLE t4(a, b, PRIMARY KEY(b, a));
  194. } db2
  195. } {}
  196. proc xTrace {args} { puts $args }
  197. foreach {tn sql} [
  198. string map {%T1% aux.t1 %T2% aux.t2 %T3% aux.t3 %T4% aux.t4} $set_of_tests
  199. ] {
  200. do_then_apply_sql $sql aux
  201. do_test 3.$tn { compare_db db2 db3 } {}
  202. }
  203. catch {db3 close}
  204. #-------------------------------------------------------------------------
  205. # The following tests verify that NULL values in primary key columns are
  206. # handled correctly by the session module.
  207. #
  208. test_reset
  209. do_execsql_test 4.0 {
  210. CREATE TABLE t1(a PRIMARY KEY);
  211. CREATE TABLE t2(a, b, c, PRIMARY KEY(c, b));
  212. CREATE TABLE t3(a, b INTEGER PRIMARY KEY);
  213. }
  214. foreach {tn sql changeset} {
  215. 1 {
  216. INSERT INTO t1 VALUES(123);
  217. INSERT INTO t1 VALUES(NULL);
  218. INSERT INTO t1 VALUES(456);
  219. } {
  220. {INSERT t1 0 X {} {i 456}}
  221. {INSERT t1 0 X {} {i 123}}
  222. }
  223. 2 {
  224. UPDATE t1 SET a = NULL;
  225. } {
  226. {DELETE t1 0 X {i 456} {}}
  227. {DELETE t1 0 X {i 123} {}}
  228. }
  229. 3 { DELETE FROM t1 } { }
  230. 4 {
  231. INSERT INTO t3 VALUES(NULL, NULL)
  232. } {
  233. {INSERT t3 0 .X {} {n {} i 1}}
  234. }
  235. 5 { INSERT INTO t2 VALUES(1, 2, NULL) } { }
  236. 6 { INSERT INTO t2 VALUES(1, NULL, 3) } { }
  237. 7 { INSERT INTO t2 VALUES(1, NULL, NULL) } { }
  238. 8 { INSERT INTO t2 VALUES(1, 2, 3) } { {INSERT t2 0 .XX {} {i 1 i 2 i 3}} }
  239. 9 { DELETE FROM t2 WHERE 1 } { {DELETE t2 0 .XX {i 1 i 2 i 3} {}} }
  240. } {
  241. do_iterator_test 4.$tn {t1 t2 t3} $sql $changeset
  242. }
  243. #-------------------------------------------------------------------------
  244. # Test that if NULL is passed to sqlite3session_attach(), all database
  245. # tables are attached to the session object.
  246. #
  247. test_reset
  248. do_execsql_test 5.0 {
  249. CREATE TABLE t1(a PRIMARY KEY);
  250. CREATE TABLE t2(x, y PRIMARY KEY);
  251. }
  252. foreach {tn sql changeset} {
  253. 1 { INSERT INTO t1 VALUES(35) } { {INSERT t1 0 X {} {i 35}} }
  254. 2 { INSERT INTO t2 VALUES(36, 37) } { {INSERT t2 0 .X {} {i 36 i 37}} }
  255. 3 {
  256. DELETE FROM t1 WHERE 1;
  257. UPDATE t2 SET x = 34;
  258. } {
  259. {DELETE t1 0 X {i 35} {}}
  260. {UPDATE t2 0 .X {i 36 i 37} {i 34 {} {}}}
  261. }
  262. } {
  263. do_iterator_test 5.$tn * $sql $changeset
  264. }
  265. #-------------------------------------------------------------------------
  266. # The next block of tests verify that the "indirect" flag is set
  267. # correctly within changesets. The indirect flag is set for a change
  268. # if either of the following are true:
  269. #
  270. # * The sqlite3session_indirect() API has been used to set the session
  271. # indirect flag to true, or
  272. # * The change was made by a trigger.
  273. #
  274. # If the same row is updated more than once during a session, then the
  275. # change is considered indirect only if all changes meet the criteria
  276. # above.
  277. #
  278. test_reset
  279. db function indirect [list S indirect]
  280. do_execsql_test 6.0 {
  281. CREATE TABLE t1(a PRIMARY KEY, b, c);
  282. CREATE TABLE t2(x PRIMARY KEY, y);
  283. CREATE TRIGGER AFTER INSERT ON t2 WHEN new.x%2 BEGIN
  284. INSERT INTO t2 VALUES(new.x+1, NULL);
  285. END;
  286. }
  287. do_iterator_test 6.1.1 * {
  288. INSERT INTO t1 VALUES(1, 'one', 'i');
  289. SELECT indirect(1);
  290. INSERT INTO t1 VALUES(2, 'two', 'ii');
  291. SELECT indirect(0);
  292. INSERT INTO t1 VALUES(3, 'three', 'iii');
  293. } {
  294. {INSERT t1 0 X.. {} {i 1 t one t i}}
  295. {INSERT t1 1 X.. {} {i 2 t two t ii}}
  296. {INSERT t1 0 X.. {} {i 3 t three t iii}}
  297. }
  298. do_iterator_test 6.1.2 * {
  299. SELECT indirect(1);
  300. UPDATE t1 SET c = 'I' WHERE a = 1;
  301. SELECT indirect(0);
  302. } {
  303. {UPDATE t1 1 X.. {i 1 {} {} t i} {{} {} {} {} t I}}
  304. }
  305. do_iterator_test 6.1.3 * {
  306. SELECT indirect(1);
  307. UPDATE t1 SET c = '.' WHERE a = 1;
  308. SELECT indirect(0);
  309. UPDATE t1 SET c = 'o' WHERE a = 1;
  310. } {
  311. {UPDATE t1 0 X.. {i 1 {} {} t I} {{} {} {} {} t o}}
  312. }
  313. do_iterator_test 6.1.4 * {
  314. SELECT indirect(0);
  315. UPDATE t1 SET c = 'x' WHERE a = 1;
  316. SELECT indirect(1);
  317. UPDATE t1 SET c = 'i' WHERE a = 1;
  318. } {
  319. {UPDATE t1 0 X.. {i 1 {} {} t o} {{} {} {} {} t i}}
  320. }
  321. do_iterator_test 6.1.4 * {
  322. SELECT indirect(1);
  323. UPDATE t1 SET c = 'y' WHERE a = 1;
  324. SELECT indirect(1);
  325. UPDATE t1 SET c = 'I' WHERE a = 1;
  326. } {
  327. {UPDATE t1 1 X.. {i 1 {} {} t i} {{} {} {} {} t I}}
  328. }
  329. do_iterator_test 6.1.5 * {
  330. INSERT INTO t2 VALUES(1, 'x');
  331. } {
  332. {INSERT t2 0 X. {} {i 1 t x}}
  333. {INSERT t2 1 X. {} {i 2 n {}}}
  334. }
  335. do_iterator_test 6.1.6 * {
  336. SELECT indirect(1);
  337. INSERT INTO t2 VALUES(3, 'x');
  338. SELECT indirect(0);
  339. UPDATE t2 SET y = 'y' WHERE x>2;
  340. } {
  341. {INSERT t2 0 X. {} {i 3 t y}}
  342. {INSERT t2 0 X. {} {i 4 t y}}
  343. }
  344. do_iterator_test 6.1.7 * {
  345. SELECT indirect(1);
  346. DELETE FROM t2 WHERE x = 4;
  347. SELECT indirect(0);
  348. INSERT INTO t2 VALUES(4, 'new');
  349. } {
  350. {UPDATE t2 0 X. {i 4 t y} {{} {} t new}}
  351. }
  352. do_iterator_test 6.1.8 * {
  353. CREATE TABLE t3(a, b PRIMARY KEY);
  354. CREATE TABLE t4(a, b PRIMARY KEY);
  355. CREATE TRIGGER t4t AFTER UPDATE ON t4 BEGIN
  356. UPDATE t3 SET a = new.a WHERE b = new.b;
  357. END;
  358. SELECT indirect(1);
  359. INSERT INTO t3 VALUES('one', 1);
  360. INSERT INTO t4 VALUES('one', 1);
  361. SELECT indirect(0);
  362. UPDATE t4 SET a = 'two' WHERE b = 1;
  363. } {
  364. {INSERT t3 1 .X {} {t two i 1}}
  365. {INSERT t4 0 .X {} {t two i 1}}
  366. }
  367. sqlite3session S db main
  368. do_execsql_test 6.2.1 {
  369. SELECT indirect(0);
  370. SELECT indirect(-1);
  371. SELECT indirect(45);
  372. SELECT indirect(-100);
  373. } {0 0 1 1}
  374. S delete
  375. #-------------------------------------------------------------------------
  376. # Test that if a conflict-handler that has been passed either NOTFOUND or
  377. # CONSTRAINT returns REPLACE - the sqlite3changeset_apply() call returns
  378. # MISUSE and rolls back any changes made so far.
  379. #
  380. # 7.1.*: NOTFOUND conflict-callback.
  381. # 7.2.*: CONSTRAINT conflict-callback.
  382. #
  383. proc xConflict {args} {return REPLACE}
  384. test_reset
  385. do_execsql_test 7.1.1 {
  386. CREATE TABLE t1(a PRIMARY KEY, b);
  387. INSERT INTO t1 VALUES(1, 'one');
  388. INSERT INTO t1 VALUES(2, 'two');
  389. }
  390. do_test 7.1.2 {
  391. execsql {
  392. CREATE TABLE t1(a PRIMARY KEY, b NOT NULL);
  393. INSERT INTO t1 VALUES(1, 'one');
  394. } db2
  395. } {}
  396. do_test 7.1.3 {
  397. set changeset [changeset_from_sql {
  398. UPDATE t1 SET b = 'five' WHERE a = 1;
  399. UPDATE t1 SET b = 'six' WHERE a = 2;
  400. }]
  401. set x [list]
  402. sqlite3session_foreach c $changeset { lappend x $c }
  403. set x
  404. } [list \
  405. {UPDATE t1 0 X. {i 1 t one} {{} {} t five}} \
  406. {UPDATE t1 0 X. {i 2 t two} {{} {} t six}} \
  407. ]
  408. do_test 7.1.4 {
  409. list [catch {sqlite3changeset_apply db2 $changeset xConflict} msg] $msg
  410. } {1 SQLITE_MISUSE}
  411. do_test 7.1.5 { execsql { SELECT * FROM t1 } db2 } {1 one}
  412. do_test 7.2.1 {
  413. set changeset [changeset_from_sql { UPDATE t1 SET b = NULL WHERE a = 1 }]
  414. set x [list]
  415. sqlite3session_foreach c $changeset { lappend x $c }
  416. set x
  417. } [list \
  418. {UPDATE t1 0 X. {i 1 t five} {{} {} n {}}} \
  419. ]
  420. do_test 7.2.2 {
  421. list [catch {sqlite3changeset_apply db2 $changeset xConflict} msg] $msg
  422. } {1 SQLITE_MISUSE}
  423. do_test 7.2.3 { execsql { SELECT * FROM t1 } db2 } {1 one}
  424. #-------------------------------------------------------------------------
  425. # Test that if a conflict-handler returns ABORT, application of the
  426. # changeset is rolled back and the sqlite3changeset_apply() method returns
  427. # SQLITE_ABORT.
  428. #
  429. # Also test that the same thing happens if a conflict handler returns an
  430. # unrecognized integer value. Except, in this case SQLITE_MISUSE is returned
  431. # instead of SQLITE_ABORT.
  432. #
  433. foreach {tn conflict_return apply_return} {
  434. 1 ABORT SQLITE_ABORT
  435. 2 567 SQLITE_MISUSE
  436. } {
  437. test_reset
  438. proc xConflict {args} [list return $conflict_return]
  439. do_test 8.$tn.0 {
  440. do_common_sql {
  441. CREATE TABLE t1(x, y, PRIMARY KEY(x, y));
  442. INSERT INTO t1 VALUES('x', 'y');
  443. }
  444. execsql { INSERT INTO t1 VALUES('w', 'w') }
  445. set changeset [changeset_from_sql { DELETE FROM t1 WHERE 1 }]
  446. set x [list]
  447. sqlite3session_foreach c $changeset { lappend x $c }
  448. set x
  449. } [list \
  450. {DELETE t1 0 XX {t w t w} {}} \
  451. {DELETE t1 0 XX {t x t y} {}} \
  452. ]
  453. do_test 8.$tn.1 {
  454. list [catch {sqlite3changeset_apply db2 $changeset xConflict} msg] $msg
  455. } [list 1 $apply_return]
  456. do_test 8.$tn.2 {
  457. execsql {SELECT * FROM t1} db2
  458. } {x y}
  459. }
  460. #-------------------------------------------------------------------------
  461. # Try to cause an infinite loop as follows:
  462. #
  463. # 1. Have a changeset insert a row that causes a CONFLICT callback,
  464. # 2. Have the conflict handler return REPLACE,
  465. # 3. After the session module deletes the conflicting row, have a trigger
  466. # re-insert it.
  467. # 4. Goto step 1...
  468. #
  469. # This doesn't work, as the second invocation of the conflict handler is a
  470. # CONSTRAINT, not a CONFLICT. There is at most one CONFLICT callback for
  471. # each change in the changeset.
  472. #
  473. test_reset
  474. proc xConflict {type args} {
  475. if {$type == "CONFLICT"} { return REPLACE }
  476. return OMIT
  477. }
  478. do_test 9.1 {
  479. execsql {
  480. CREATE TABLE t1(a PRIMARY KEY, b);
  481. }
  482. execsql {
  483. CREATE TABLE t1(a PRIMARY KEY, b);
  484. INSERT INTO t1 VALUES('x', 2);
  485. CREATE TRIGGER tr1 AFTER DELETE ON t1 BEGIN
  486. INSERT INTO t1 VALUES(old.a, old.b);
  487. END;
  488. } db2
  489. } {}
  490. do_test 9.2 {
  491. set changeset [changeset_from_sql { INSERT INTO t1 VALUES('x', 1) }]
  492. sqlite3changeset_apply db2 $changeset xConflict
  493. } {}
  494. do_test 9.3 {
  495. execsql { SELECT * FROM t1 } db2
  496. } {x 2}
  497. #-------------------------------------------------------------------------
  498. #
  499. test_reset
  500. db function enable [list S enable]
  501. do_common_sql {
  502. CREATE TABLE t1(a PRIMARY KEY, b);
  503. INSERT INTO t1 VALUES('x', 'X');
  504. }
  505. do_iterator_test 10.1 t1 {
  506. INSERT INTO t1 VALUES('y', 'Y');
  507. SELECT enable(0);
  508. INSERT INTO t1 VALUES('z', 'Z');
  509. SELECT enable(1);
  510. } {
  511. {INSERT t1 0 X. {} {t y t Y}}
  512. }
  513. sqlite3session S db main
  514. do_execsql_test 10.2 {
  515. SELECT enable(0);
  516. SELECT enable(-1);
  517. SELECT enable(1);
  518. SELECT enable(-1);
  519. } {0 0 1 1}
  520. S delete
  521. #-------------------------------------------------------------------------
  522. test_reset
  523. do_common_sql {
  524. CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c, d, e, f);
  525. WITH s(i) AS (
  526. SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<32
  527. )
  528. INSERT INTO t1 SELECT NULL, 0, 0, 0, 0, 0 FROM s
  529. }
  530. do_then_apply_sql -ignorenoop {
  531. UPDATE t1 SET f=f+1 WHERE a=1;
  532. UPDATE t1 SET e=e+1 WHERE a=2;
  533. UPDATE t1 SET e=e+1, f=f+1 WHERE a=3;
  534. UPDATE t1 SET d=d+1 WHERE a=4;
  535. UPDATE t1 SET d=d+1, f=f+1 WHERE a=5;
  536. UPDATE t1 SET d=d+1, e=e+1 WHERE a=6;
  537. UPDATE t1 SET d=d+1, e=e+1, f=f+1 WHERE a=7;
  538. UPDATE t1 SET c=c+1 WHERE a=8;
  539. UPDATE t1 SET c=c+1, f=f+1 WHERE a=9;
  540. UPDATE t1 SET c=c+1, e=e+1 WHERE a=10;
  541. UPDATE t1 SET c=c+1, e=e+1, f=f+1 WHERE a=11;
  542. UPDATE t1 SET c=c+1, d=d+1 WHERE a=12;
  543. UPDATE t1 SET c=c+1, d=d+1, f=f+1 WHERE a=13;
  544. UPDATE t1 SET c=c+1, d=d+1, e=e+1 WHERE a=14;
  545. UPDATE t1 SET c=c+1, d=d+1, e=e+1, f=f+1 WHERE a=15;
  546. UPDATE t1 SET d=d+1 WHERE a=16;
  547. UPDATE t1 SET d=d+1, f=f+1 WHERE a=17;
  548. UPDATE t1 SET d=d+1, e=e+1 WHERE a=18;
  549. UPDATE t1 SET d=d+1, e=e+1, f=f+1 WHERE a=19;
  550. UPDATE t1 SET d=d+1, d=d+1 WHERE a=20;
  551. UPDATE t1 SET d=d+1, d=d+1, f=f+1 WHERE a=21;
  552. UPDATE t1 SET d=d+1, d=d+1, e=e+1 WHERE a=22;
  553. UPDATE t1 SET d=d+1, d=d+1, e=e+1, f=f+1 WHERE a=23;
  554. UPDATE t1 SET d=d+1, c=c+1 WHERE a=24;
  555. UPDATE t1 SET d=d+1, c=c+1, f=f+1 WHERE a=25;
  556. UPDATE t1 SET d=d+1, c=c+1, e=e+1 WHERE a=26;
  557. UPDATE t1 SET d=d+1, c=c+1, e=e+1, f=f+1 WHERE a=27;
  558. UPDATE t1 SET d=d+1, c=c+1, d=d+1 WHERE a=28;
  559. UPDATE t1 SET d=d+1, c=c+1, d=d+1, f=f+1 WHERE a=29;
  560. UPDATE t1 SET d=d+1, c=c+1, d=d+1, e=e+1 WHERE a=30;
  561. UPDATE t1 SET d=d+1, c=c+1, d=d+1, e=e+1, f=f+1 WHERE a=31;
  562. }
  563. do_test 11.0 {
  564. compare_db db db2
  565. } {}
  566. finish_test