interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / memdb1.test

480 lines · 11193 bytes Raw Blame History
1# 2018-01-02
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 the "memdb" VFS
13#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17set testprefix memdb1
18do_not_use_codec
19
20ifcapable !deserialize {
21 finish_test
22 return
23}
24
25# Create a MEMDB and populate it with some dummy data.
26# Then extract the database into the $::db1 variable.
27# Verify that the size of $::db1 is the same as the size of
28# the database.
29#
30unset -nocomplain db1
31unset -nocomplain sz1
32unset -nocomplain pgsz
33do_test 100 {
34 db eval {
35 CREATE TABLE t1(a,b);
36 INSERT INTO t1 VALUES(1,2);
37 }
38 set ::pgsz [db one {PRAGMA page_size}]
39 set ::sz1 [expr {$::pgsz*[db one {PRAGMA page_count}]}]
40 set ::db1 [db serialize]
41 expr {[string length $::db1]==$::sz1}
42} 1
43set fd [open db1.db wb]
44puts -nonewline $fd $db1
45close $fd
46
47# Create a new MEMDB and initialize it to the content of $::db1
48# Verify that the content is the same.
49#
50db close
51sqlite3 db
52db deserialize $db1
53do_execsql_test 110 {
54 SELECT * FROM t1;
55} {1 2}
56
57# What happens when we try to VACUUM a MEMDB database?
58#
59do_execsql_test 120 {
60 PRAGMA auto_vacuum = off;
61 VACUUM;
62} {}
63do_execsql_test 130 {
64 CREATE TABLE t2(x, y);
65 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)
66 INSERT INTO t2(x, y) SELECT x, randomblob(1000) FROM c;
67 DROP TABLE t2;
68 PRAGMA page_count;
69} {116}
70do_execsql_test 140 {
71 VACUUM;
72 PRAGMA page_count;
73} {2}
74
75do_test 150 {
76 catch {db deserialize -unknown 1 $db1} msg
77 set msg
78} {unknown option: -unknown}
79do_test 151 {
80 db deserialize -readonly 1 $db1
81 db eval {SELECT * FROM t1}
82} {1 2}
83do_test 152 {
84 catchsql {INSERT INTO t1 VALUES(3,4);}
85} {1 {attempt to write a readonly database}}
86
87do_test 160 {
88 db deserialize -maxsize 32768 $db1
89 db eval {SELECT * FROM t1}
90} {1 2}
91do_test 161 {
92 db eval {INSERT INTO t1 VALUES(3,4); SELECT * FROM t1}
93} {1 2 3 4}
94do_test 162 {
95 catchsql {INSERT INTO t1 VALUES(5,randomblob(100000))}
96} {1 {database or disk is full}}
97
98
99# Build a largish on-disk database and serialize it. Verify that the
100# serialization works.
101#
102db close
103forcedelete test.db
104sqlite3 db test.db
105do_execsql_test 200 {
106 CREATE TABLE t3(x, y);
107 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<400)
108 INSERT INTO t3(x, y) SELECT x, randomblob(1000) FROM c;
109 PRAGMA quick_check;
110} {ok}
111set fd [open test.db rb]
112unset -nocomplain direct
113set direct [read $fd]
114close $fd
115do_test 210 {
116 string length [db serialize]
117} [string length $direct]
118do_test 220 {
119 db eval {ATTACH ':memory:' AS aux1}
120 db deserialize aux1 $::direct
121 db eval {
122 SELECT x, y FROM main.t3 EXCEPT SELECT x, y FROM aux1.t3;
123 }
124} {}
125unset -nocomplain direct
126
127# Do the same with a :memory: database.
128#
129db close
130sqlite3 db :memory:
131do_execsql_test 300 {
132 CREATE TABLE t3(x, y);
133 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<400)
134 INSERT INTO t3(x, y) SELECT x, randomblob(1000) FROM c;
135 PRAGMA quick_check;
136} {ok}
137do_test 310 {
138 db eval {ATTACH ':memory:' AS aux1}
139 db deserialize aux1 [db serialize main]
140 db eval {
141 SELECT x, y FROM main.t3 EXCEPT SELECT x, y FROM aux1.t3;
142 }
143} {}
144
145# Deserialize an empty database
146#
147db close
148sqlite3 db
149db deserialize {}
150do_execsql_test 400 {
151 PRAGMA integrity_check;
152} {ok}
153do_execsql_test 410 {
154 CREATE TABLE t4(a,b);
155 INSERT INTO t4 VALUES('hello','world!');
156 PRAGMA integrity_check;
157 SELECT * FROM t4;
158} {ok hello world!}
159do_execsql_test 420 {
160 PRAGMA journal_mode=TRUNCATE;
161 PRAGMA journal_mode=OFF;
162 PRAGMA journal_mode=DELETE;
163 PRAGMA journal_mode=WAL;
164 PRAGMA journal_mode=PERSIST;
165 PRAGMA journal_mode=MEMORY;
166 PRAGMA journal_mode=OFF;
167 PRAGMA journal_mode=DELETE;
168} {truncate off delete delete persist memory off delete}
169
170# Deserialize something that is not a database.
171#
172db close
173sqlite3 db
174do_test 500 {
175 set rc [catch {db deserialize not-a-database} msg]
176 lappend rc $msg
177} {0 {}}
178do_catchsql_test 510 {
179 PRAGMA integrity_check;
180} {1 {file is not a database}}
181
182# Abuse the serialize and deserialize commands. Make sure errors are caught.
183#
184do_test 600 {
185 set rc [catch {db deserialize} msg]
186 lappend rc $msg
187} {1 {wrong # args: should be "db deserialize ?DATABASE? VALUE"}}
188do_test 610 {
189 set rc [catch {db deserialize a b c} msg]
190 lappend rc $msg
191} {1 {unknown option: a}}
192do_test 620 {
193 set rc [catch {db serialize a b} msg]
194 lappend rc $msg
195} {1 {wrong # args: should be "db serialize ?DATABASE?"}}
196
197# 2021-07-19 https://sqlite.org/forum/forumpost/e1cbb5f450b98aa6
198# The TEMP database cannot participate in serialization or
199# deserialization.
200#
201reset_db
202do_test 650 {
203 db eval {
204 CREATE TEMP TABLE t0(a);
205 CREATE TABLE t1(x);
206 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000)
207 INSERT INTO t1(x) SELECT random() FROM c;
208 }
209 set rc [catch {db deserialize temp [db serialize main]} err]
210 lappend rc err
211} {1 err}
212
213#-------------------------------------------------------------------------
214ifcapable vtab {
215 reset_db
216 do_execsql_test 700 {
217 CREATE TABLE t1(a, b);
218 PRAGMA schema_version = 0;
219 }
220 do_test 710 {
221 set ser [db serialize main]
222 db close
223 sqlite3 db
224 db deserialize main $ser
225 catchsql {
226 CREATE VIRTUAL TABLE t1 USING rtree(id, a, b, c, d);
227 }
228 } {1 {table t1 already exists}}
229}
230
231
232#-------------------------------------------------------------------------
233# dbsqlfuzz 0a13dfb474d4f2f11a48a2ea57075c96fb456dd7
234#
235if {[wal_is_capable]} {
236 reset_db
237 do_execsql_test 800 {
238 PRAGMA auto_vacuum = 0;
239 PRAGMA page_size = 8192;
240 PRAGMA journal_mode = wal;
241 CREATE TABLE t1(x, y);
242 INSERT INTO t1 VALUES(1, 2);
243 CREATE TABLE t2(x, y);
244 } {wal}
245 db close
246
247 set fd [open test.db]
248 fconfigure $fd -translation binary
249 set data [read $fd [expr 20*1024]]
250 close $fd
251
252 sqlite3 db ""
253 db deserialize $data
254
255 do_execsql_test 810 {
256 PRAGMA locking_mode = exclusive;
257 SELECT * FROM t1
258 } {exclusive 1 2}
259
260 do_execsql_test 820 {
261 INSERT INTO t1 VALUES(3, 4);
262 SELECT * FROM t1;
263 } {1 2 3 4}
264
265 do_catchsql_test 830 {
266 PRAGMA wal_checkpoint;
267 } {1 {database disk image is malformed}}
268}
269
270# 2024-01-20
271# https://sqlite.org/forum/forumpost/498777780e16880a
272#
273# Make sure a database is initialized before serializing it.
274#
275reset_db
276sqlite3 dbempty :memory:
277do_test 900 {
278 set len [string length [dbempty serialize]]
279 expr {$len>0}
280} 1
281dbempty close
282
283
284#-------------------------------------------------------------------------
285# Make sure a database cannot be overwritten by deserialize() if
286# there is an open transaction on it.
287#
288do_execsql_test 1000 {
289 CREATE TABLE t(x);
290 INSERT INTO t VALUES(1),(2);
291}
292
293set blob [db serialize main]
294
295do_test 1010 {
296 set seen 0
297 list [catch {
298 db eval {SELECT x FROM t} {
299 incr seen
300 if {$seen == 1} {
301 db deserialize main $blob
302 }
303 }
304 } msg] $msg
305} {1 {unable to set MEMDB content}}
306
307forcedelete test.db2
308sqlite3 db2 test.db2
309do_test 1020 {
310 set seen 0
311 sqlite3_backup B db2 main db main
312 B step 2
313 set res [list [catch {
314 db deserialize main $blob
315 } msg] $msg]
316 B finish
317 set res
318} {1 {unable to set MEMDB content}}
319db2 close
320
321#-------------------------------------------------------------------------
322# Check that things work if the destination database of a backup is
323# overwritten using sqlite3_deserialize() between sqlite3_backup_init()
324# and the first call to sqlite3_backup_step(). Forum post 15d82885e2.
325#
326reset_db
327do_execsql_test 1100 {
328 CREATE TABLE t(x);
329 INSERT INTO t VALUES(1),(2);
330}
331
332set blob [db serialize main]
333
334forcedelete test.db2
335sqlite3 db2 test.db2
336do_test 1110 {
337 set seen 0
338 sqlite3_backup B db2 main db main
339 db2 deserialize main $blob
340 B step 2
341 B finish
342} {SQLITE_OK}
343
344#-------------------------------------------------------------------------
345# Check that deserializing into the main db does not leave existing
346# eponymous virtual tables with a dangling schema pointer.
347#
348# Report: https://sqlite.org/bugs/forumpost/2fbe01f58f
349#
350ifcapable vtab {
351 reset_db
352 sqlite3 db :memory:
353
354 do_execsql_test 1210 {
355 CREATE TABLE t(x);
356 SELECT * FROM json_each('[1]');
357 } {
358 0 1 integer 1 1 {} {$[0]} {$}
359 }
360
361 set blob [db serialize main]
362 db deserialize main $blob
363
364 do_execsql_test 1220 {
365 SELECT * FROM json_each('[1]');
366 } {
367 0 1 integer 1 1 {} {$[0]} {$}
368 }
369
370 # Attempt to deserialize while a statement that uses an eponymous
371 # virtual table is still active. The deserialize should fail.
372 #
373 do_test 1230 {
374 set res [list]
375 set msg ""
376 db eval { SELECT value FROM json_each('[1,2,3]') } {
377 if {$msg==""} {
378 catch { db deserialize main $blob } msg
379 }
380 lappend res $value
381 }
382 set res
383 } {1 2 3}
384 do_test 1240 {
385 set msg
386 } {unable to set MEMDB content}
387 do_execsql_test 1250 {
388 SELECT value FROM json_each('[4,5]');
389 } {4 5}
390}
391
392#-------------------------------------------------------------------------
393# Test that deserializing expires all prepared statements.
394#
395reset_db
396do_execsql_test 1300 {
397 CREATE TABLE t1(a, b);
398 CREATE INDEX i1 ON t1(a);
399 INSERT INTO t1 VALUES(1, 1), (2, 2);
400}
401
402set blob [db serialize main]
403
404reset_db
405do_execsql_test 1310 {
406 CREATE TABLE t2(a, b);
407 CREATE TABLE t1(c, d);
408 INSERT INTO t1 VALUES(1, 1), (2, 2);
409}
410
411do_execsql_test 1320 {
412 SELECT * FROM t1;
413} {1 1 2 2}
414
415do_test 1330 {
416 db deserialize main $blob
417} {}
418
419do_execsql_test 1340 {
420 SELECT * FROM t1;
421} {1 1 2 2}
422
423# Also test that deserializing does not interrupt statements reading
424# from other databases.
425do_execsql_test 1350 {
426 ATTACH '' AS aux;
427 CREATE TABLE aux.t3(x);
428 INSERT INTO aux.t3 VALUES(1), (2), (3);
429}
430do_test 1360 {
431 set res [list]
432 db eval { SELECT (SELECT x FROM t3 WHERE t3.x=o.x) AS a FROM t3 AS o } {
433 db deserialize main $blob
434 lappend res $a
435 }
436 set res
437} {1 2 3}
438
439#-------------------------------------------------------------------------
440# A TEMP trigger on a table in a database overwritten by a deserialize()
441# is orphaned in the same way as if the db was DETACHed.
442#
443reset_db
444do_execsql_test 1400 {
445 CREATE TABLE t1(x);
446}
447set blob [db serialize main]
448
449do_execsql_test 1410 {
450 CREATE TEMP TABLE tlog(y);
451 CREATE TEMP TRIGGER tr AFTER INSERT ON main.t1 BEGIN
452 INSERT INTO tlog VALUES(new.x);
453 END;
454 INSERT INTO t1 VALUES(1);
455 SELECT * FROM tlog;
456} {1}
457
458do_test 1420 { db deserialize main $blob } {}
459
460do_execsql_test 1430 {
461 INSERT INTO t1 VALUES(2);
462 SELECT * FROM tlog;
463} {1}
464
465do_execsql_test 1440 {
466 SELECT name, tbl_name FROM temp.sqlite_schema WHERE type='trigger';
467} {tr t1}
468
469do_execsql_test 1450 {
470 CREATE TEMP TABLE t1(x);
471 INSERT INTO temp.t1 VALUES(3);
472 SELECT * FROM tlog;
473} {1}
474
475do_execsql_test 1460 {
476 DROP TRIGGER temp.tr;
477 SELECT name FROM temp.sqlite_schema WHERE type='trigger';
478} {}
479
480finish_test