backup.test 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. # 2009 January 30
  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 file is testing the sqlite3_backup_XXX API.
  13. #
  14. # $Id: backup.test,v 1.11 2009/06/05 17:09:12 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. do_not_use_codec
  18. #---------------------------------------------------------------------
  19. # Test organization:
  20. #
  21. # backup-1.*: Warm-body tests.
  22. #
  23. # backup-2.*: Test backup under various conditions. To and from in-memory
  24. # databases. To and from empty/populated databases. etc.
  25. #
  26. # backup-3.*: Verify that the locking-page (pending byte page) is handled.
  27. #
  28. # backup-4.*: Test various error conditions.
  29. #
  30. # backup-5.*: Test the source database being modified during a backup.
  31. #
  32. # backup-6.*: Test the backup_remaining() and backup_pagecount() APIs.
  33. #
  34. # backup-7.*: Test SQLITE_BUSY and SQLITE_LOCKED errors.
  35. #
  36. # backup-8.*: Test multiple simultaneous backup operations.
  37. #
  38. # backup-9.*: Test that passing a negative argument to backup_step() is
  39. # interpreted as "copy the whole file".
  40. #
  41. # backup-10.*: Test writing the source database mid backup.
  42. #
  43. proc data_checksum {db file} { $db one "SELECT md5sum(a, b) FROM ${file}.t1" }
  44. proc test_contents {name db1 file1 db2 file2} {
  45. $db2 eval {select * from sqlite_master}
  46. $db1 eval {select * from sqlite_master}
  47. set checksum [data_checksum $db2 $file2]
  48. uplevel [list do_test $name [list data_checksum $db1 $file1] $checksum]
  49. }
  50. do_test backup-1.1 {
  51. execsql {
  52. BEGIN;
  53. CREATE TABLE t1(a, b);
  54. CREATE INDEX i1 ON t1(a, b);
  55. INSERT INTO t1 VALUES(1, randstr(1000,1000));
  56. INSERT INTO t1 VALUES(2, randstr(1000,1000));
  57. INSERT INTO t1 VALUES(3, randstr(1000,1000));
  58. INSERT INTO t1 VALUES(4, randstr(1000,1000));
  59. INSERT INTO t1 VALUES(5, randstr(1000,1000));
  60. COMMIT;
  61. }
  62. } {}
  63. # Sanity check to verify that the [test_contents] proc works.
  64. #
  65. test_contents backup-1.2 db main db main
  66. # Check that it is possible to create and finish backup operations.
  67. #
  68. do_test backup-1.3.1 {
  69. delete_file test2.db
  70. sqlite3 db2 test2.db
  71. sqlite3_backup B db2 main db main
  72. } {B}
  73. do_test backup-1.3.2 {
  74. B finish
  75. } {SQLITE_OK}
  76. do_test backup-1.3.3 {
  77. info commands B
  78. } {}
  79. # Simplest backup operation. Backup test.db to test2.db. test2.db is
  80. # initially empty. test.db uses the default page size.
  81. #
  82. do_test backup-1.4.1 {
  83. sqlite3_backup B db2 main db main
  84. } {B}
  85. do_test backup-1.4.2 {
  86. B step 200
  87. } {SQLITE_DONE}
  88. do_test backup-1.4.3 {
  89. B finish
  90. } {SQLITE_OK}
  91. do_test backup-1.4.4 {
  92. info commands B
  93. } {}
  94. test_contents backup-1.4.5 db2 main db main
  95. db close
  96. db2 close
  97. #
  98. # End of backup-1.* tests.
  99. #---------------------------------------------------------------------
  100. #---------------------------------------------------------------------
  101. # The following tests, backup-2.*, are based on the following procedure:
  102. #
  103. # 1) Populate the source database.
  104. # 2) Populate the destination database.
  105. # 3) Run the backup to completion. (backup-2.*.1)
  106. # 4) Integrity check the destination db. (backup-2.*.2)
  107. # 5) Check that the contents of the destination db is the same as that
  108. # of the source db. (backup-2.*.3)
  109. #
  110. # The test is run with all possible combinations of the following
  111. # input parameters, except that if the destination is an in-memory
  112. # database, the only page size tested is 1024 bytes (the same as the
  113. # source page-size).
  114. #
  115. # * Source database is an in-memory database, OR
  116. # * Source database is a file-backed database.
  117. #
  118. # * Target database is an in-memory database, OR
  119. # * Target database is a file-backed database.
  120. #
  121. # * Destination database is a main file, OR
  122. # * Destination database is an attached file, OR
  123. # * Destination database is a temp database.
  124. #
  125. # * Target database is empty (zero bytes), OR
  126. # * Target database is larger than the source, OR
  127. # * Target database is smaller than the source.
  128. #
  129. # * Target database page-size is the same as the source, OR
  130. # * Target database page-size is larger than the source, OR
  131. # * Target database page-size is smaller than the source.
  132. #
  133. # * Each call to step copies a single page, OR
  134. # * A single call to step copies the entire source database.
  135. #
  136. set iTest 1
  137. foreach zSrcFile {test.db :memory:} {
  138. foreach zDestFile {test2.db :memory:} {
  139. foreach zOpenScript [list {
  140. sqlite3 db $zSrcFile
  141. sqlite3 db2 $zSrcFile
  142. db2 eval "ATTACH '$zDestFile' AS bak"
  143. set db_dest db2
  144. set file_dest bak
  145. } {
  146. sqlite3 db $zSrcFile
  147. sqlite3 db2 $zDestFile
  148. set db_dest db2
  149. set file_dest main
  150. } {
  151. sqlite3 db $zSrcFile
  152. sqlite3 db2 $zDestFile
  153. set db_dest db2
  154. set file_dest temp
  155. }] {
  156. foreach rows_dest {0 3 10} {
  157. foreach pgsz_dest {512 1024 2048} {
  158. foreach nPagePerStep {1 200} {
  159. # Open the databases.
  160. catch { delete_file test.db }
  161. catch { delete_file test2.db }
  162. eval $zOpenScript
  163. # Set to true if copying to an in-memory destination. Copying to an
  164. # in-memory destination is only possible if the initial destination
  165. # page size is the same as the source page size (in this case 1024 bytes).
  166. #
  167. set isMemDest [expr {
  168. $zDestFile eq ":memory:" || $file_dest eq "temp" && $TEMP_STORE>=2
  169. }]
  170. if { $isMemDest==0 || $pgsz_dest == 1024 } {
  171. if 0 {
  172. puts -nonewline "Test $iTest: src=$zSrcFile dest=$zDestFile"
  173. puts -nonewline " (as $db_dest.$file_dest)"
  174. puts -nonewline " rows_dest=$rows_dest pgsz_dest=$pgsz_dest"
  175. puts ""
  176. }
  177. # Set up the content of the source database.
  178. execsql {
  179. PRAGMA page_size = 1024;
  180. BEGIN;
  181. CREATE TABLE t1(a, b);
  182. CREATE INDEX i1 ON t1(a, b);
  183. INSERT INTO t1 VALUES(1, randstr(1000,1000));
  184. INSERT INTO t1 VALUES(2, randstr(1000,1000));
  185. INSERT INTO t1 VALUES(3, randstr(1000,1000));
  186. INSERT INTO t1 VALUES(4, randstr(1000,1000));
  187. INSERT INTO t1 VALUES(5, randstr(1000,1000));
  188. COMMIT;
  189. }
  190. # Set up the content of the target database.
  191. execsql "PRAGMA ${file_dest}.page_size = ${pgsz_dest}" $db_dest
  192. if {$rows_dest != 0} {
  193. execsql "
  194. BEGIN;
  195. CREATE TABLE ${file_dest}.t1(a, b);
  196. CREATE INDEX ${file_dest}.i1 ON t1(a, b);
  197. " $db_dest
  198. for {set ii 0} {$ii < $rows_dest} {incr ii} {
  199. execsql "
  200. INSERT INTO ${file_dest}.t1 VALUES(1, randstr(1000,1000))
  201. " $db_dest
  202. }
  203. }
  204. # Backup the source database.
  205. do_test backup-2.$iTest.1 {
  206. sqlite3_backup B $db_dest $file_dest db main
  207. while {[B step $nPagePerStep]=="SQLITE_OK"} {}
  208. B finish
  209. } {SQLITE_OK}
  210. # Run integrity check on the backup.
  211. do_test backup-2.$iTest.2 {
  212. execsql "PRAGMA ${file_dest}.integrity_check" $db_dest
  213. } {ok}
  214. test_contents backup-2.$iTest.3 db main $db_dest $file_dest
  215. }
  216. db close
  217. catch {db2 close}
  218. incr iTest
  219. } } } } } }
  220. #
  221. # End of backup-2.* tests.
  222. #---------------------------------------------------------------------
  223. #---------------------------------------------------------------------
  224. # These tests, backup-3.*, ensure that nothing goes wrong if either
  225. # the source or destination database are large enough to include the
  226. # the locking-page (the page that contains the range of bytes that
  227. # the locks are applied to). These tests assume that the pending
  228. # byte is at offset 0x00010000 (64KB offset), as set by tester.tcl,
  229. # not at the 1GB offset as it usually is.
  230. #
  231. # The test procedure is as follows (same procedure as used for
  232. # the backup-2.* tests):
  233. #
  234. # 1) Populate the source database.
  235. # 2) Populate the destination database.
  236. # 3) Run the backup to completion. (backup-3.*.1)
  237. # 4) Integrity check the destination db. (backup-3.*.2)
  238. # 5) Check that the contents of the destination db is the same as that
  239. # of the source db. (backup-3.*.3)
  240. #
  241. # The test procedure is run with the following parameters varied:
  242. #
  243. # * Source database includes pending-byte page.
  244. # * Source database does not include pending-byte page.
  245. #
  246. # * Target database includes pending-byte page.
  247. # * Target database does not include pending-byte page.
  248. #
  249. # * Target database page-size is the same as the source, OR
  250. # * Target database page-size is larger than the source, OR
  251. # * Target database page-size is smaller than the source.
  252. #
  253. set iTest 1
  254. foreach nSrcPg {10 64 65 66 100} {
  255. foreach nDestRow {10 100} {
  256. foreach nDestPgsz {512 1024 2048 4096} {
  257. catch { delete_file test.db }
  258. catch { delete_file test2.db }
  259. sqlite3 db test.db
  260. sqlite3 db2 test2.db
  261. # Set up the content of the two databases.
  262. #
  263. execsql { PRAGMA page_size = 1024 }
  264. execsql "PRAGMA page_size = $nDestPgsz" db2
  265. foreach db {db db2} {
  266. execsql {
  267. BEGIN;
  268. CREATE TABLE t1(a, b);
  269. CREATE INDEX i1 ON t1(a, b);
  270. COMMIT;
  271. } $db
  272. }
  273. while {[file size test.db]/1024 < $nSrcPg} {
  274. execsql { INSERT INTO t1 VALUES($ii, randstr(200,200)) }
  275. }
  276. for {set ii 0} {$ii < $nDestRow} {incr ii} {
  277. execsql { INSERT INTO t1 VALUES($ii, randstr(1000,1000)) } db2
  278. }
  279. # Backup the source database.
  280. do_test backup-3.$iTest.1 {
  281. sqlite3_backup B db main db2 main
  282. while {[B step 10]=="SQLITE_OK"} {}
  283. B finish
  284. } {SQLITE_OK}
  285. # Run integrity check on the backup.
  286. do_test backup-3.$iTest.2 {
  287. execsql "PRAGMA integrity_check" db2
  288. } {ok}
  289. test_contents backup-3.$iTest.3 db main db2 main
  290. db close
  291. db2 close
  292. incr iTest
  293. }
  294. }
  295. }
  296. #--------------------------------------------------------------------
  297. do_test backup-3.$iTest.1 {
  298. catch { forcedelete test.db }
  299. catch { forcedelete test2.db }
  300. sqlite3 db test.db
  301. set iTab 1
  302. db eval { PRAGMA page_size = 512 }
  303. while {[file size test.db] <= $::sqlite_pending_byte} {
  304. db eval "CREATE TABLE t${iTab}(a, b, c)"
  305. incr iTab
  306. }
  307. sqlite3 db2 test2.db
  308. db2 eval { PRAGMA page_size = 4096 }
  309. while {[file size test2.db] < $::sqlite_pending_byte} {
  310. db2 eval "CREATE TABLE t${iTab}(a, b, c)"
  311. incr iTab
  312. }
  313. sqlite3_backup B db2 main db main
  314. B step -1
  315. } {SQLITE_DONE}
  316. do_test backup-3.$iTest.2 {
  317. B finish
  318. } {SQLITE_OK}
  319. #
  320. # End of backup-3.* tests.
  321. #---------------------------------------------------------------------
  322. #---------------------------------------------------------------------
  323. # The following tests, backup-4.*, test various error conditions:
  324. #
  325. # backup-4.1.*: Test invalid database names.
  326. #
  327. # backup-4.2.*: Test that the source database cannot be detached while
  328. # a backup is in progress.
  329. #
  330. # backup-4.3.*: Test that the source database handle cannot be closed
  331. # while a backup is in progress.
  332. #
  333. # backup-4.4.*: Test an attempt to specify the same handle for the
  334. # source and destination databases.
  335. #
  336. # backup-4.5.*: Test that an in-memory destination with a different
  337. # page-size to the source database is an error.
  338. #
  339. sqlite3 db test.db
  340. sqlite3 db2 test2.db
  341. do_test backup-4.1.1 {
  342. catch { sqlite3_backup B db aux db2 main }
  343. } {1}
  344. do_test backup-4.1.2 {
  345. sqlite3_errmsg db
  346. } {unknown database aux}
  347. do_test backup-4.1.3 {
  348. catch { sqlite3_backup B db main db2 aux }
  349. } {1}
  350. do_test backup-4.1.4 {
  351. sqlite3_errmsg db
  352. } {unknown database aux}
  353. do_test backup-4.2.1 {
  354. catch { forcedelete test3.db }
  355. catch { forcedelete test4.db }
  356. execsql {
  357. ATTACH 'test3.db' AS aux1;
  358. CREATE TABLE aux1.t1(a, b);
  359. }
  360. execsql {
  361. ATTACH 'test4.db' AS aux2;
  362. CREATE TABLE aux2.t2(a, b);
  363. } db2
  364. sqlite3_backup B db aux1 db2 aux2
  365. } {B}
  366. do_test backup-4.2.2 {
  367. catchsql { DETACH aux2 } db2
  368. } {1 {database aux2 is locked}}
  369. do_test backup-4.2.3 {
  370. B step 50
  371. } {SQLITE_DONE}
  372. do_test backup-4.2.4 {
  373. B finish
  374. } {SQLITE_OK}
  375. do_test backup-4.3.1 {
  376. sqlite3_backup B db aux1 db2 aux2
  377. } {B}
  378. do_test backup-4.3.2 {
  379. db2 cache flush
  380. sqlite3_close db2
  381. } {SQLITE_BUSY}
  382. do_test backup-4.3.3 {
  383. sqlite3_errmsg db2
  384. } {unable to close due to unfinalized statements or unfinished backups}
  385. do_test backup-4.3.4 {
  386. B step 50
  387. } {SQLITE_DONE}
  388. do_test backup-4.3.5 {
  389. B finish
  390. } {SQLITE_OK}
  391. do_test backup-4.4.1 {
  392. set rc [catch {sqlite3_backup B db main db aux1}]
  393. list $rc [sqlite3_errcode db] [sqlite3_errmsg db]
  394. } {1 SQLITE_ERROR {source and destination must be distinct}}
  395. db close
  396. db2 close
  397. do_test backup-4.5.1 {
  398. catch { forcedelete test.db }
  399. sqlite3 db test.db
  400. sqlite3 db2 :memory:
  401. execsql {
  402. CREATE TABLE t1(a, b);
  403. INSERT INTO t1 VALUES(1, 2);
  404. }
  405. execsql {
  406. PRAGMA page_size = 4096;
  407. CREATE TABLE t2(a, b);
  408. INSERT INTO t2 VALUES(3, 4);
  409. } db2
  410. sqlite3_backup B db2 main db main
  411. } {B}
  412. do_test backup-4.5.2 {
  413. B step 5000
  414. } {SQLITE_READONLY}
  415. do_test backup-4.5.3 {
  416. B finish
  417. } {SQLITE_READONLY}
  418. db close
  419. db2 close
  420. #
  421. # End of backup-4.* tests.
  422. #---------------------------------------------------------------------
  423. #---------------------------------------------------------------------
  424. # The following tests, backup-5.*, test that the backup works properly
  425. # when the source database is modified during the backup. Test cases
  426. # are organized as follows:
  427. #
  428. # backup-5.x.1.*: Nothing special. Modify the database mid-backup.
  429. #
  430. # backup-5.x.2.*: Modify the database mid-backup so that one or more
  431. # pages are written out due to cache stress. Then
  432. # rollback the transaction.
  433. #
  434. # backup-5.x.3.*: Database is vacuumed.
  435. #
  436. # backup-5.x.4.*: Database is vacuumed and the page-size modified.
  437. #
  438. # backup-5.x.5.*: Database is shrunk via incr-vacuum.
  439. #
  440. # Each test is run three times, in the following configurations:
  441. #
  442. # 1) Backing up file-to-file. The writer writes via an external pager.
  443. # 2) Backing up file-to-file. The writer writes via the same pager as
  444. # is used by the backup operation.
  445. # 3) Backing up memory-to-file.
  446. #
  447. set iTest 0
  448. forcedelete bak.db-wal
  449. foreach {writer file} {db test.db db3 test.db db :memory:} {
  450. incr iTest
  451. catch { delete_file bak.db }
  452. sqlite3 db2 bak.db
  453. catch { delete_file $file }
  454. sqlite3 db $file
  455. sqlite3 db3 $file
  456. do_test backup-5.$iTest.1.1 {
  457. execsql {
  458. BEGIN;
  459. CREATE TABLE t1(a, b);
  460. CREATE INDEX i1 ON t1(a, b);
  461. INSERT INTO t1 VALUES(1, randstr(1000,1000));
  462. INSERT INTO t1 VALUES(2, randstr(1000,1000));
  463. INSERT INTO t1 VALUES(3, randstr(1000,1000));
  464. INSERT INTO t1 VALUES(4, randstr(1000,1000));
  465. INSERT INTO t1 VALUES(5, randstr(1000,1000));
  466. COMMIT;
  467. }
  468. expr {[execsql {PRAGMA page_count}] > 10}
  469. } {1}
  470. do_test backup-5.$iTest.1.2 {
  471. sqlite3_backup B db2 main db main
  472. B step 5
  473. } {SQLITE_OK}
  474. do_test backup-5.$iTest.1.3 {
  475. execsql { UPDATE t1 SET a = a + 1 } $writer
  476. B step 50
  477. } {SQLITE_DONE}
  478. do_test backup-5.$iTest.1.4 {
  479. B finish
  480. } {SQLITE_OK}
  481. integrity_check backup-5.$iTest.1.5 db2
  482. test_contents backup-5.$iTest.1.6 db main db2 main
  483. do_test backup-5.$iTest.2.1 {
  484. execsql {
  485. PRAGMA cache_size = 10;
  486. BEGIN;
  487. INSERT INTO t1 SELECT '', randstr(1000,1000) FROM t1;
  488. INSERT INTO t1 SELECT '', randstr(1000,1000) FROM t1;
  489. INSERT INTO t1 SELECT '', randstr(1000,1000) FROM t1;
  490. INSERT INTO t1 SELECT '', randstr(1000,1000) FROM t1;
  491. COMMIT;
  492. }
  493. } {}
  494. do_test backup-5.$iTest.2.2 {
  495. sqlite3_backup B db2 main db main
  496. B step 50
  497. } {SQLITE_OK}
  498. do_test backup-5.$iTest.2.3 {
  499. execsql {
  500. BEGIN;
  501. UPDATE t1 SET a = a + 1;
  502. ROLLBACK;
  503. } $writer
  504. B step 5000
  505. } {SQLITE_DONE}
  506. do_test backup-5.$iTest.2.4 {
  507. B finish
  508. } {SQLITE_OK}
  509. integrity_check backup-5.$iTest.2.5 db2
  510. test_contents backup-5.$iTest.2.6 db main db2 main
  511. do_test backup-5.$iTest.3.1 {
  512. execsql { UPDATE t1 SET b = randstr(1000,1000) }
  513. } {}
  514. do_test backup-5.$iTest.3.2 {
  515. sqlite3_backup B db2 main db main
  516. B step 50
  517. } {SQLITE_OK}
  518. do_test backup-5.$iTest.3.3 {
  519. execsql { VACUUM } $writer
  520. B step 5000
  521. } {SQLITE_DONE}
  522. do_test backup-5.$iTest.3.4 {
  523. B finish
  524. } {SQLITE_OK}
  525. integrity_check backup-5.$iTest.3.5 db2
  526. test_contents backup-5.$iTest.3.6 db main db2 main
  527. do_test backup-5.$iTest.4.1 {
  528. execsql { UPDATE t1 SET b = randstr(1000,1000) }
  529. } {}
  530. do_test backup-5.$iTest.4.2 {
  531. sqlite3_backup B db2 main db main
  532. B step 50
  533. } {SQLITE_OK}
  534. do_test backup-5.$iTest.4.3 {
  535. execsql {
  536. PRAGMA page_size = 2048;
  537. VACUUM;
  538. } $writer
  539. B step 5000
  540. } {SQLITE_DONE}
  541. do_test backup-5.$iTest.4.4 {
  542. B finish
  543. } {SQLITE_OK}
  544. integrity_check backup-5.$iTest.4.5 db2
  545. test_contents backup-5.$iTest.4.6 db main db2 main
  546. catch {db close}
  547. catch {db2 close}
  548. catch {db3 close}
  549. catch { delete_file bak.db }
  550. sqlite3 db2 bak.db
  551. catch { delete_file $file }
  552. sqlite3 db $file
  553. sqlite3 db3 $file
  554. do_test backup-5.$iTest.5.1 {
  555. execsql {
  556. PRAGMA auto_vacuum = incremental;
  557. BEGIN;
  558. CREATE TABLE t1(a, b);
  559. CREATE INDEX i1 ON t1(a, b);
  560. INSERT INTO t1 VALUES(1, randstr(1000,1000));
  561. INSERT INTO t1 VALUES(2, randstr(1000,1000));
  562. INSERT INTO t1 VALUES(3, randstr(1000,1000));
  563. INSERT INTO t1 VALUES(4, randstr(1000,1000));
  564. INSERT INTO t1 VALUES(5, randstr(1000,1000));
  565. COMMIT;
  566. }
  567. } {}
  568. do_test backup-5.$iTest.5.2 {
  569. sqlite3_backup B db2 main db main
  570. B step 8
  571. } {SQLITE_OK}
  572. do_test backup-5.$iTest.5.3 {
  573. execsql {
  574. DELETE FROM t1;
  575. PRAGMA incremental_vacuum;
  576. } $writer
  577. B step 50
  578. } {SQLITE_DONE}
  579. do_test backup-5.$iTest.5.4 {
  580. B finish
  581. } {SQLITE_OK}
  582. integrity_check backup-5.$iTest.5.5 db2
  583. test_contents backup-5.$iTest.5.6 db main db2 main
  584. catch {db close}
  585. catch {db2 close}
  586. catch {db3 close}
  587. }
  588. #
  589. # End of backup-5.* tests.
  590. #---------------------------------------------------------------------
  591. #---------------------------------------------------------------------
  592. # Test the sqlite3_backup_remaining() and backup_pagecount() APIs.
  593. #
  594. do_test backup-6.1 {
  595. catch { forcedelete test.db }
  596. catch { forcedelete test2.db }
  597. sqlite3 db test.db
  598. sqlite3 db2 test2.db
  599. execsql {
  600. BEGIN;
  601. CREATE TABLE t1(a, b);
  602. CREATE INDEX i1 ON t1(a, b);
  603. INSERT INTO t1 VALUES(1, randstr(1000,1000));
  604. INSERT INTO t1 VALUES(2, randstr(1000,1000));
  605. INSERT INTO t1 VALUES(3, randstr(1000,1000));
  606. INSERT INTO t1 VALUES(4, randstr(1000,1000));
  607. INSERT INTO t1 VALUES(5, randstr(1000,1000));
  608. COMMIT;
  609. }
  610. } {}
  611. do_test backup-6.2 {
  612. set nTotal [expr {[file size test.db]/1024}]
  613. sqlite3_backup B db2 main db main
  614. B step 1
  615. } {SQLITE_OK}
  616. do_test backup-6.3 {
  617. B pagecount
  618. } $nTotal
  619. do_test backup-6.4 {
  620. B remaining
  621. } [expr $nTotal-1]
  622. do_test backup-6.5 {
  623. B step 5
  624. list [B remaining] [B pagecount]
  625. } [list [expr $nTotal-6] $nTotal]
  626. do_test backup-6.6 {
  627. execsql { CREATE TABLE t2(a PRIMARY KEY, b) }
  628. B step 1
  629. list [B remaining] [B pagecount]
  630. } [list [expr $nTotal-5] [expr $nTotal+2]]
  631. do_test backup-6.X {
  632. B finish
  633. } {SQLITE_OK}
  634. catch {db close}
  635. catch {db2 close}
  636. #---------------------------------------------------------------------
  637. # Test cases backup-7.* test that SQLITE_BUSY and SQLITE_LOCKED errors
  638. # are returned correctly:
  639. #
  640. # backup-7.1.*: Source database is externally locked (return SQLITE_BUSY).
  641. #
  642. # backup-7.2.*: Attempt to step the backup process while a
  643. # write-transaction is underway on the source pager (return
  644. # SQLITE_LOCKED).
  645. #
  646. # backup-7.3.*: Destination database is externally locked (return SQLITE_BUSY).
  647. #
  648. do_test backup-7.0 {
  649. catch { forcedelete test.db }
  650. catch { forcedelete test2.db }
  651. sqlite3 db2 test2.db
  652. sqlite3 db test.db
  653. execsql {
  654. CREATE TABLE t1(a, b);
  655. CREATE INDEX i1 ON t1(a, b);
  656. INSERT INTO t1 VALUES(1, randstr(1000,1000));
  657. INSERT INTO t1 SELECT a+ 1, randstr(1000,1000) FROM t1;
  658. INSERT INTO t1 SELECT a+ 2, randstr(1000,1000) FROM t1;
  659. INSERT INTO t1 SELECT a+ 4, randstr(1000,1000) FROM t1;
  660. INSERT INTO t1 SELECT a+ 8, randstr(1000,1000) FROM t1;
  661. INSERT INTO t1 SELECT a+16, randstr(1000,1000) FROM t1;
  662. INSERT INTO t1 SELECT a+32, randstr(1000,1000) FROM t1;
  663. INSERT INTO t1 SELECT a+64, randstr(1000,1000) FROM t1;
  664. }
  665. } {}
  666. do_test backup-7.1.1 {
  667. sqlite3_backup B db2 main db main
  668. B step 5
  669. } {SQLITE_OK}
  670. do_test backup-7.1.2 {
  671. sqlite3 db3 test.db
  672. execsql { BEGIN EXCLUSIVE } db3
  673. B step 5
  674. } {SQLITE_BUSY}
  675. do_test backup-7.1.3 {
  676. execsql { ROLLBACK } db3
  677. B step 5
  678. } {SQLITE_OK}
  679. do_test backup-7.2.1 {
  680. execsql {
  681. BEGIN;
  682. INSERT INTO t1 VALUES(1, 4);
  683. }
  684. } {}
  685. do_test backup-7.2.2 {
  686. B step 5000
  687. } {SQLITE_BUSY}
  688. do_test backup-7.2.3 {
  689. execsql { ROLLBACK }
  690. B step 5000
  691. } {SQLITE_DONE}
  692. do_test backup-7.2.4 {
  693. B finish
  694. } {SQLITE_OK}
  695. test_contents backup-7.2.5 db main db2 main
  696. integrity_check backup-7.3.6 db2
  697. do_test backup-7.3.1 {
  698. db2 close
  699. db3 close
  700. forcedelete test2.db
  701. sqlite3 db2 test2.db
  702. sqlite3 db3 test2.db
  703. sqlite3_backup B db2 main db main
  704. execsql { BEGIN ; CREATE TABLE t2(a, b); } db3
  705. B step 5
  706. } {SQLITE_BUSY}
  707. do_test backup-7.3.2 {
  708. execsql { COMMIT } db3
  709. B step 5000
  710. } {SQLITE_DONE}
  711. do_test backup-7.3.3 {
  712. B finish
  713. } {SQLITE_OK}
  714. test_contents backup-7.3.4 db main db2 main
  715. integrity_check backup-7.3.5 db2
  716. catch { db2 close }
  717. catch { db3 close }
  718. #-----------------------------------------------------------------------
  719. # The following tests, backup-8.*, test attaching multiple backup
  720. # processes to the same source database. Also, reading from the source
  721. # database while a read transaction is active.
  722. #
  723. # These tests reuse the database "test.db" left over from backup-7.*.
  724. #
  725. do_test backup-8.1 {
  726. catch { forcedelete test2.db }
  727. catch { forcedelete test3.db }
  728. sqlite3 db2 test2.db
  729. sqlite3 db3 test3.db
  730. sqlite3_backup B2 db2 main db main
  731. sqlite3_backup B3 db3 main db main
  732. list [B2 finish] [B3 finish]
  733. } {SQLITE_OK SQLITE_OK}
  734. do_test backup-8.2 {
  735. sqlite3_backup B3 db3 main db main
  736. sqlite3_backup B2 db2 main db main
  737. list [B2 finish] [B3 finish]
  738. } {SQLITE_OK SQLITE_OK}
  739. do_test backup-8.3 {
  740. sqlite3_backup B2 db2 main db main
  741. sqlite3_backup B3 db3 main db main
  742. B2 step 5
  743. } {SQLITE_OK}
  744. do_test backup-8.4 {
  745. execsql {
  746. BEGIN;
  747. SELECT * FROM sqlite_master;
  748. }
  749. B3 step 5
  750. } {SQLITE_OK}
  751. do_test backup-8.5 {
  752. list [B3 step 5000] [B3 finish]
  753. } {SQLITE_DONE SQLITE_OK}
  754. do_test backup-8.6 {
  755. list [B2 step 5000] [B2 finish]
  756. } {SQLITE_DONE SQLITE_OK}
  757. test_contents backup-8.7 db main db2 main
  758. test_contents backup-8.8 db main db3 main
  759. do_test backup-8.9 {
  760. execsql { PRAGMA lock_status }
  761. } {main shared temp closed}
  762. do_test backup-8.10 {
  763. execsql COMMIT
  764. } {}
  765. catch { db2 close }
  766. catch { db3 close }
  767. #-----------------------------------------------------------------------
  768. # The following tests, backup-9.*, test that:
  769. #
  770. # * Passing 0 as an argument to sqlite3_backup_step() means no pages
  771. # are backed up (backup-9.1.*), and
  772. # * Passing a negative value as an argument to sqlite3_backup_step() means
  773. # all pages are backed up (backup-9.2.*).
  774. #
  775. # These tests reuse the database "test.db" left over from backup-7.*.
  776. #
  777. do_test backup-9.1.1 {
  778. sqlite3 db2 test2.db
  779. sqlite3_backup B db2 main db main
  780. B step 1
  781. } {SQLITE_OK}
  782. do_test backup-9.1.2 {
  783. set nRemaining [B remaining]
  784. expr {$nRemaining>100}
  785. } {1}
  786. do_test backup-9.1.3 {
  787. B step 0
  788. } {SQLITE_OK}
  789. do_test backup-9.1.4 {
  790. B remaining
  791. } $nRemaining
  792. do_test backup-9.2.1 {
  793. B step -1
  794. } {SQLITE_DONE}
  795. do_test backup-9.2.2 {
  796. B remaining
  797. } {0}
  798. do_test backup-9.2.3 {
  799. B finish
  800. } {SQLITE_OK}
  801. catch {db2 close}
  802. ifcapable memorymanage {
  803. db close
  804. forcedelete test.db
  805. forcedelete bak.db
  806. sqlite3 db test.db
  807. sqlite3 db2 test.db
  808. sqlite3 db3 bak.db
  809. do_test backup-10.1.1 {
  810. execsql {
  811. BEGIN;
  812. CREATE TABLE t1(a, b);
  813. INSERT INTO t1 VALUES(1, randstr(1000,1000));
  814. INSERT INTO t1 VALUES(2, randstr(1000,1000));
  815. INSERT INTO t1 VALUES(3, randstr(1000,1000));
  816. INSERT INTO t1 VALUES(4, randstr(1000,1000));
  817. INSERT INTO t1 VALUES(5, randstr(1000,1000));
  818. CREATE INDEX i1 ON t1(a, b);
  819. COMMIT;
  820. }
  821. } {}
  822. do_test backup-10.1.2 {
  823. sqlite3_backup B db3 main db2 main
  824. B step 5
  825. } {SQLITE_OK}
  826. do_test backup-10.1.3 {
  827. execsql {
  828. UPDATE t1 SET b = randstr(500,500);
  829. }
  830. } {}
  831. sqlite3_release_memory [expr 1024*1024]
  832. do_test backup-10.1.3 {
  833. B step 50
  834. } {SQLITE_DONE}
  835. do_test backup-10.1.4 {
  836. B finish
  837. } {SQLITE_OK}
  838. do_test backup-10.1.5 {
  839. execsql { PRAGMA integrity_check } db3
  840. } {ok}
  841. db2 close
  842. db3 close
  843. }
  844. #-----------------------------------------------------------------------
  845. # Test that if the database is written to via the same database handle being
  846. # used as the source by a backup operation:
  847. #
  848. # 10.1.*: If the db is in-memory, the backup is restarted.
  849. # 10.2.*: If the db is a file, the backup is not restarted.
  850. #
  851. db close
  852. forcedelete test.db test.db-journal
  853. foreach {tn file rc} {
  854. 1 test.db SQLITE_DONE
  855. 2 :memory: SQLITE_OK
  856. } {
  857. do_test backup-10.$tn.1 {
  858. sqlite3 db $file
  859. execsql {
  860. CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB);
  861. BEGIN;
  862. INSERT INTO t1 VALUES(NULL, randomblob(200));
  863. INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
  864. INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
  865. INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
  866. INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
  867. INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
  868. INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
  869. INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
  870. INSERT INTO t1 SELECT NULL, randomblob(200) FROM t1;
  871. COMMIT;
  872. SELECT count(*) FROM t1;
  873. }
  874. } {256}
  875. do_test backup-10.$tn.2 {
  876. set pgs [execsql {pragma page_count}]
  877. expr {$pgs > 50 && $pgs < 75}
  878. } {1}
  879. do_test backup-10.$tn.3 {
  880. forcedelete bak.db bak.db-journal
  881. sqlite3 db2 bak.db
  882. sqlite3_backup B db2 main db main
  883. B step 50
  884. } {SQLITE_OK}
  885. do_test backup-10.$tn.4 {
  886. execsql { UPDATE t1 SET b = randomblob(200) WHERE a IN (1, 250) }
  887. } {}
  888. do_test backup-10.$tn.5 {
  889. B step 50
  890. } $rc
  891. do_test backup-10.$tn.6 {
  892. B finish
  893. } {SQLITE_OK}
  894. db2 close
  895. }
  896. finish_test