blob: de9a9572cc979880049ee89cd9fda694c056ca41 [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#
shanece14f622009-02-11 16:06:18 +000014# $Id: backup.test,v 1.7 2009/02/11 16:06:19 shane 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
shanece14f622009-02-11 16:06:18 +0000564 catch {db close}
565 catch {db2 close}
566 catch {db3 close}
danielk197704103022009-02-03 16:51:24 +0000567 catch { file delete bak.db }
568 sqlite3 db2 bak.db
569 catch { file delete $file }
570 sqlite3 db $file
571 sqlite3 db3 $file
572 do_test backup-5.$iTest.5.1 {
573 execsql {
574 PRAGMA auto_vacuum = incremental;
575 BEGIN;
576 CREATE TABLE t1(a, b);
577 CREATE INDEX i1 ON t1(a, b);
578 INSERT INTO t1 VALUES(1, randstr(1000,1000));
579 INSERT INTO t1 VALUES(2, randstr(1000,1000));
580 INSERT INTO t1 VALUES(3, randstr(1000,1000));
581 INSERT INTO t1 VALUES(4, randstr(1000,1000));
582 INSERT INTO t1 VALUES(5, randstr(1000,1000));
583 COMMIT;
584 }
585 } {}
586 do_test backup-5.$iTest.5.2 {
587 sqlite3_backup B db2 main db main
588 B step 8
589 } {SQLITE_OK}
590 do_test backup-5.$iTest.5.3 {
591 execsql {
592 DELETE FROM t1;
593 PRAGMA incremental_vacuum;
594 } $writer
595 B step 50
596 } {SQLITE_DONE}
597 do_test backup-5.$iTest.5.4 {
598 B finish
599 } {SQLITE_OK}
600 integrity_check backup-5.$iTest.5.5 db2
601 test_contents backup-5.$iTest.5.6 db main db2 main
shanece14f622009-02-11 16:06:18 +0000602 catch {db close}
603 catch {db2 close}
604 catch {db3 close}
danielk197704103022009-02-03 16:51:24 +0000605}
danielk197704103022009-02-03 16:51:24 +0000606#
607# End of backup-5.* tests.
608#---------------------------------------------------------------------
609
610#---------------------------------------------------------------------
611# Test the sqlite3_backup_remaining() and backup_pagecount() APIs.
612#
613do_test backup-6.1 {
614 catch { file delete -force test.db }
615 catch { file delete -force test2.db }
616 sqlite3 db test.db
617 sqlite3 db2 test2.db
618 execsql {
619 BEGIN;
620 CREATE TABLE t1(a, b);
621 CREATE INDEX i1 ON t1(a, b);
622 INSERT INTO t1 VALUES(1, randstr(1000,1000));
623 INSERT INTO t1 VALUES(2, randstr(1000,1000));
624 INSERT INTO t1 VALUES(3, randstr(1000,1000));
625 INSERT INTO t1 VALUES(4, randstr(1000,1000));
626 INSERT INTO t1 VALUES(5, randstr(1000,1000));
627 COMMIT;
628 }
629} {}
630do_test backup-6.2 {
631 set nTotal [expr {[file size test.db]/1024}]
632 sqlite3_backup B db2 main db main
633 B step 1
634} {SQLITE_OK}
635do_test backup-6.3 {
636 B pagecount
637} $nTotal
638do_test backup-6.4 {
639 B remaining
640} [expr $nTotal-1]
641do_test backup-6.5 {
642 B step 5
643 list [B remaining] [B pagecount]
644} [list [expr $nTotal-6] $nTotal]
645do_test backup-6.6 {
646 execsql { CREATE TABLE t2(a PRIMARY KEY, b) }
647 B step 1
648 list [B remaining] [B pagecount]
649} [list [expr $nTotal-5] [expr $nTotal+2]]
650
651do_test backup-6.X {
652 B finish
653} {SQLITE_OK}
654
shanece14f622009-02-11 16:06:18 +0000655catch {db close}
656catch {db2 close}
danielk197704103022009-02-03 16:51:24 +0000657
658#---------------------------------------------------------------------
659# Test cases backup-7.* test that SQLITE_BUSY and SQLITE_LOCKED errors
660# are returned correctly:
661#
662# backup-7.1.*: Source database is externally locked (return SQLITE_BUSY).
663#
664# backup-7.2.*: Attempt to step the backup process while a
665# write-transaction is underway on the source pager (return
666# SQLITE_LOCKED).
667#
668# backup-7.3.*: Destination database is externally locked (return SQLITE_BUSY).
669#
670do_test backup-7.0 {
671 catch { file delete -force test.db }
672 catch { file delete -force test2.db }
673 sqlite3 db2 test2.db
674 sqlite3 db test.db
675 execsql {
676 CREATE TABLE t1(a, b);
677 CREATE INDEX i1 ON t1(a, b);
678 INSERT INTO t1 VALUES(1, randstr(1000,1000));
679 INSERT INTO t1 SELECT a+ 1, randstr(1000,1000) FROM t1;
680 INSERT INTO t1 SELECT a+ 2, randstr(1000,1000) FROM t1;
681 INSERT INTO t1 SELECT a+ 4, randstr(1000,1000) FROM t1;
682 INSERT INTO t1 SELECT a+ 8, randstr(1000,1000) FROM t1;
683 INSERT INTO t1 SELECT a+16, randstr(1000,1000) FROM t1;
684 INSERT INTO t1 SELECT a+32, randstr(1000,1000) FROM t1;
685 INSERT INTO t1 SELECT a+64, randstr(1000,1000) FROM t1;
686 }
687} {}
688
689do_test backup-7.1.1 {
690 sqlite3_backup B db2 main db main
691 B step 5
692} {SQLITE_OK}
693do_test backup-7.1.2 {
694 sqlite3 db3 test.db
695 execsql { BEGIN EXCLUSIVE } db3
696 B step 5
697} {SQLITE_BUSY}
698do_test backup-7.1.3 {
699 execsql { ROLLBACK } db3
700 B step 5
701} {SQLITE_OK}
702do_test backup-7.2.1 {
703 execsql {
704 BEGIN;
705 INSERT INTO t1 VALUES(1, 4);
706 }
707} {}
708do_test backup-7.2.2 {
709 B step 5000
710} {SQLITE_LOCKED}
711do_test backup-7.2.3 {
712 execsql { ROLLBACK }
713 B step 5000
714} {SQLITE_DONE}
715do_test backup-7.2.4 {
716 B finish
717} {SQLITE_OK}
718test_contents backup-7.2.5 db main db2 main
719integrity_check backup-7.3.6 db2
720
721do_test backup-7.3.1 {
722 db2 close
723 db3 close
724 file delete -force test2.db
725 sqlite3 db2 test2.db
726 sqlite3 db3 test2.db
727
728 sqlite3_backup B db2 main db main
729 execsql { BEGIN ; CREATE TABLE t2(a, b); } db3
730
731 B step 5
732} {SQLITE_BUSY}
733do_test backup-7.3.2 {
734 execsql { COMMIT } db3
735 B step 5000
736} {SQLITE_DONE}
737do_test backup-7.3.3 {
738 B finish
739} {SQLITE_OK}
740test_contents backup-7.3.4 db main db2 main
741integrity_check backup-7.3.5 db2
742catch { db2 close }
743catch { db3 close }
744
745#-----------------------------------------------------------------------
746# The following tests, backup-8.*, test attaching multiple backup
747# processes to the same source database. Also, reading from the source
748# database while a read transaction is active.
749#
750# These tests reuse the database "test.db" left over from backup-7.*.
751#
752do_test backup-8.1 {
753 catch { file delete -force test2.db }
754 catch { file delete -force test3.db }
755 sqlite3 db2 test2.db
756 sqlite3 db3 test3.db
757
758 sqlite3_backup B2 db2 main db main
759 sqlite3_backup B3 db3 main db main
760 list [B2 finish] [B3 finish]
761} {SQLITE_OK SQLITE_OK}
762do_test backup-8.2 {
763 sqlite3_backup B3 db3 main db main
764 sqlite3_backup B2 db2 main db main
765 list [B2 finish] [B3 finish]
766} {SQLITE_OK SQLITE_OK}
767do_test backup-8.3 {
768 sqlite3_backup B2 db2 main db main
769 sqlite3_backup B3 db3 main db main
770 B2 step 5
771} {SQLITE_OK}
772do_test backup-8.4 {
773 execsql {
774 BEGIN;
775 SELECT * FROM sqlite_master;
776 }
777 B3 step 5
778} {SQLITE_OK}
779do_test backup-8.5 {
780 list [B3 step 5000] [B3 finish]
781} {SQLITE_DONE SQLITE_OK}
782do_test backup-8.6 {
783 list [B2 step 5000] [B2 finish]
784} {SQLITE_DONE SQLITE_OK}
785test_contents backup-8.7 db main db2 main
786test_contents backup-8.8 db main db3 main
787do_test backup-8.9 {
788 execsql { PRAGMA lock_status }
789} {main shared temp closed}
790do_test backup-8.10 {
791 execsql COMMIT
792} {}
793catch { db2 close }
794catch { db3 close }
795
danielk197703ab0352009-02-06 05:59:44 +0000796#-----------------------------------------------------------------------
797# The following tests, backup-9.*, test that:
798#
799# * Passing 0 as an argument to sqlite3_backup_step() means no pages
800# are backed up (backup-9.1.*), and
801# * Passing a negative value as an argument to sqlite3_backup_step() means
802# all pages are backed up (backup-9.2.*).
803#
804# These tests reuse the database "test.db" left over from backup-7.*.
805#
806do_test backup-9.1.1 {
807 sqlite3 db2 test2.db
808 sqlite3_backup B db2 main db main
809 B step 1
810} {SQLITE_OK}
811do_test backup-9.1.2 {
812 set nRemaining [B remaining]
813 expr {$nRemaining>100}
814} {1}
815do_test backup-9.1.3 {
816 B step 0
817} {SQLITE_OK}
818do_test backup-9.1.4 {
819 B remaining
820} $nRemaining
821
822do_test backup-9.2.1 {
823 B step -1
824} {SQLITE_DONE}
825do_test backup-9.2.2 {
826 B remaining
827} {0}
828do_test backup-9.2.3 {
829 B finish
830} {SQLITE_OK}
831catch {db2 close}
danielk197704103022009-02-03 16:51:24 +0000832
833finish_test
danielk197703ab0352009-02-06 05:59:44 +0000834