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