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