summaryrefslogtreecommitdiff
path: root/test/memdb1.test
blob: 44da9a0f2ea15201431c3cb1e11e2f50e5b4e753 (plain) (blame)
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
# 2018-01-02
#
# 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 the "memdb" VFS
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix memdb1
do_not_use_codec

ifcapable !deserialize {
  finish_test
  return
}

# Create a MEMDB and populate it with some dummy data.
# Then extract the database into the $::db1 variable.
# Verify that the size of $::db1 is the same as the size of
# the database.
#
unset -nocomplain db1
unset -nocomplain sz1
unset -nocomplain pgsz
do_test 100 {
  db eval {
    CREATE TABLE t1(a,b);
    INSERT INTO t1 VALUES(1,2);
  }
  set ::pgsz [db one {PRAGMA page_size}]
  set ::sz1 [expr {$::pgsz*[db one {PRAGMA page_count}]}]
  set ::db1 [db serialize]
  expr {[string length $::db1]==$::sz1}
} 1
set fd [open db1.db wb]
puts -nonewline $fd $db1
close $fd

# Create a new MEMDB and initialize it to the content of $::db1
# Verify that the content is the same.
#
db close
sqlite3 db
db deserialize $db1
do_execsql_test 110 {
  SELECT * FROM t1;
} {1 2}

# What happens when we try to VACUUM a MEMDB database?
#
do_execsql_test 120 {
  PRAGMA auto_vacuum = off;
  VACUUM;
} {}
do_execsql_test 130 {
  CREATE TABLE t2(x, y);
  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<100)
   INSERT INTO t2(x, y) SELECT x, randomblob(1000) FROM c;
  DROP TABLE t2;
  PRAGMA page_count;
} {116}
do_execsql_test 140 {
  VACUUM;
  PRAGMA page_count;
} {2}

do_test 150 {
  catch {db deserialize -unknown 1 $db1} msg
  set msg
} {unknown option: -unknown}
do_test 151 {
  db deserialize -readonly 1 $db1
  db eval {SELECT * FROM t1}
} {1 2}
do_test 152 {
  catchsql {INSERT INTO t1 VALUES(3,4);}
} {1 {attempt to write a readonly database}}

do_test 160 {
  db deserialize -maxsize 32768 $db1
  db eval {SELECT * FROM t1}
} {1 2}
do_test 161 {
  db eval {INSERT INTO t1 VALUES(3,4); SELECT * FROM t1}
} {1 2 3 4}
do_test 162 {
  catchsql {INSERT INTO t1 VALUES(5,randomblob(100000))}
} {1 {database or disk is full}}


# Build a largish on-disk database and serialize it.  Verify that the
# serialization works.
#
db close
forcedelete test.db
sqlite3 db test.db
do_execsql_test 200 {
  CREATE TABLE t3(x, y);
  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<400)
   INSERT INTO t3(x, y) SELECT x, randomblob(1000) FROM c;
  PRAGMA quick_check;
} {ok}
set fd [open test.db rb]
unset -nocomplain direct
set direct [read $fd]
close $fd
do_test 210 {
  string length [db serialize]
} [string length $direct]
do_test 220 {
  db eval {ATTACH ':memory:' AS aux1}
  db deserialize aux1 $::direct
  db eval {
     SELECT x, y FROM main.t3 EXCEPT SELECT x, y FROM aux1.t3;
  }
} {}
unset -nocomplain direct

# Do the same with a :memory: database.
#
db close
sqlite3 db :memory:
do_execsql_test 300 {
  CREATE TABLE t3(x, y);
  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<400)
   INSERT INTO t3(x, y) SELECT x, randomblob(1000) FROM c;
  PRAGMA quick_check;
} {ok}
do_test 310 {
  db eval {ATTACH ':memory:' AS aux1}
  db deserialize aux1 [db serialize main]
  db eval {
     SELECT x, y FROM main.t3 EXCEPT SELECT x, y FROM aux1.t3;
  }
} {}

# Deserialize an empty database
#
db close
sqlite3 db
db deserialize {}
do_execsql_test 400 {
  PRAGMA integrity_check;
} {ok}
do_execsql_test 410 {
  CREATE TABLE t4(a,b);
  INSERT INTO t4 VALUES('hello','world!');
  PRAGMA integrity_check;
  SELECT * FROM t4;
} {ok hello world!}
do_execsql_test 420 {
  PRAGMA journal_mode=TRUNCATE;
  PRAGMA journal_mode=OFF;
  PRAGMA journal_mode=DELETE;
  PRAGMA journal_mode=WAL;
  PRAGMA journal_mode=PERSIST;
  PRAGMA journal_mode=MEMORY;
  PRAGMA journal_mode=OFF;
  PRAGMA journal_mode=DELETE;
} {truncate off delete delete persist memory off delete}

# Deserialize something that is not a database.
#
db close
sqlite3 db
do_test 500 {
  set rc [catch {db deserialize not-a-database} msg]
  lappend rc $msg
} {0 {}}
do_catchsql_test 510 {
  PRAGMA integrity_check;
} {1 {file is not a database}}

# Abuse the serialize and deserialize commands.  Make sure errors are caught.
#
do_test 600 {
  set rc [catch {db deserialize} msg]
  lappend rc $msg
} {1 {wrong # args: should be "db deserialize ?DATABASE? VALUE"}}
do_test 610 {
  set rc [catch {db deserialize a b c} msg]
  lappend rc $msg
} {1 {unknown option: a}}
do_test 620 {
  set rc [catch {db serialize a b} msg]
  lappend rc $msg
} {1 {wrong # args: should be "db serialize ?DATABASE?"}}

# 2021-07-19 https://sqlite.org/forum/forumpost/e1cbb5f450b98aa6
# The TEMP database cannot participate in serialization or
# deserialization.
#
reset_db
do_test 650 {
  db eval {
    CREATE TEMP TABLE t0(a);
    CREATE TABLE t1(x);
    WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000)
    INSERT INTO t1(x) SELECT random() FROM c;
  }
  set rc [catch {db deserialize temp [db serialize main]} err]
  lappend rc err
} {1 err}

#-------------------------------------------------------------------------
ifcapable vtab {
  reset_db
  do_execsql_test 700 {
    CREATE TABLE t1(a, b);
    PRAGMA schema_version = 0;
  }
  do_test 710 {
    set ser [db serialize main]
    db close
    sqlite3 db
    db deserialize main $ser
    catchsql {
      CREATE VIRTUAL TABLE t1 USING rtree(id, a, b, c, d);
    }
  } {1 {table t1 already exists}}
}


#-------------------------------------------------------------------------
# dbsqlfuzz  0a13dfb474d4f2f11a48a2ea57075c96fb456dd7
#
if {[wal_is_capable]} {
  reset_db
  do_execsql_test 800 {
    PRAGMA auto_vacuum = 0;
    PRAGMA page_size = 8192;
    PRAGMA journal_mode = wal;
    CREATE TABLE t1(x, y);
    INSERT INTO t1 VALUES(1, 2);
    CREATE TABLE t2(x, y);
  } {wal}
  db close
  
  set fd [open test.db]
  fconfigure $fd -translation binary
  set data [read $fd [expr 20*1024]]
  close $fd
  
  sqlite3 db ""
  db deserialize $data
  
  do_execsql_test 810 {
    PRAGMA locking_mode = exclusive;
    SELECT * FROM t1
  } {exclusive 1 2}
  
  do_execsql_test 820 {
    INSERT INTO t1 VALUES(3, 4);
    SELECT * FROM t1;
  } {1 2 3 4}
  
  do_catchsql_test 830 {
    PRAGMA wal_checkpoint;
  } {1 {database disk image is malformed}}
}

# 2024-01-20
# https://sqlite.org/forum/forumpost/498777780e16880a
#
# Make sure a database is initialized before serializing it.
#
reset_db
sqlite3 dbempty :memory:
do_test 900 {
  set len [string length [dbempty serialize]]
  expr {$len>0}
} 1
dbempty close


#-------------------------------------------------------------------------
# Make sure a database cannot be overwritten by deserialize() if 
# there is an open transaction on it. 
#
do_execsql_test 1000 {
  CREATE TABLE t(x); 
  INSERT INTO t VALUES(1),(2);
}

set blob [db serialize main]

do_test 1010 {
  set seen 0
  list [catch {
    db eval {SELECT x FROM t} {
      incr seen
      if {$seen == 1} {
        db deserialize main $blob
      }
    }
  } msg] $msg
} {1 {unable to set MEMDB content}}

forcedelete test.db2
sqlite3 db2 test.db2
do_test 1020 {
  set seen 0
  sqlite3_backup B db2 main db main
  B step 2
  set res [list [catch {
    db deserialize main $blob
  } msg] $msg]
  B finish
  set res
} {1 {unable to set MEMDB content}}
db2 close

#-------------------------------------------------------------------------
# Check that things work if the destination database of a backup is
# overwritten using sqlite3_deserialize() between sqlite3_backup_init()
# and the first call to sqlite3_backup_step(). Forum post 15d82885e2.
#
reset_db
do_execsql_test 1100 {
  CREATE TABLE t(x); 
  INSERT INTO t VALUES(1),(2);
}

set blob [db serialize main]

forcedelete test.db2
sqlite3 db2 test.db2
do_test 1110 {
  set seen 0
  sqlite3_backup B db2 main db main
  db2 deserialize main $blob
  B step 2
  B finish
} {SQLITE_OK}

#-------------------------------------------------------------------------
# Check that deserializing into the main db does not leave existing
# eponymous virtual tables with a dangling schema pointer. 
#
# Report: https://sqlite.org/bugs/forumpost/2fbe01f58f
#
ifcapable vtab {
  reset_db
  sqlite3 db :memory:
  
  do_execsql_test 1210 {
    CREATE TABLE t(x); 
    SELECT * FROM json_each('[1]');
  } {
    0 1 integer 1 1 {} {$[0]} {$}
  }
  
  set blob [db serialize main]
  db deserialize main $blob
  
  do_execsql_test 1220 {
    SELECT  *  FROM json_each('[1]');
  } {
    0 1 integer 1 1 {} {$[0]} {$}
  }
  
  # Attempt to deserialize while a statement that uses an eponymous
  # virtual table is still active. The deserialize should fail.
  #
  do_test 1230 {
    set res [list]
    set msg ""
    db eval { SELECT value FROM json_each('[1,2,3]') } {
      if {$msg==""} {
        catch { db deserialize main $blob } msg
      }
      lappend res $value
    }
    set res
  } {1 2 3}
  do_test 1240 {
    set msg
  } {unable to set MEMDB content}
  do_execsql_test 1250 {
    SELECT value FROM json_each('[4,5]');
  } {4 5}
}

#-------------------------------------------------------------------------
# Test that deserializing expires all prepared statements.
#
reset_db
do_execsql_test 1300 {
  CREATE TABLE t1(a, b);
  CREATE INDEX i1 ON t1(a);
  INSERT INTO t1 VALUES(1, 1), (2, 2);
}

set blob [db serialize main]

reset_db
do_execsql_test 1310 {
  CREATE TABLE t2(a, b);
  CREATE TABLE t1(c, d);
  INSERT INTO t1 VALUES(1, 1), (2, 2);
}

do_execsql_test 1320 {
  SELECT * FROM t1;
} {1 1 2 2}

do_test 1330 {
  db deserialize main $blob
} {}

do_execsql_test 1340 {
  SELECT * FROM t1;
} {1 1 2 2}

# Also test that deserializing does not interrupt statements reading 
# from other databases. 
do_execsql_test 1350 {
  ATTACH '' AS aux;
  CREATE TABLE aux.t3(x);
  INSERT INTO aux.t3 VALUES(1), (2), (3);
}
do_test 1360 {
  set res [list]
  db eval { SELECT (SELECT x FROM t3 WHERE t3.x=o.x) AS a FROM t3 AS o } {
    db deserialize main $blob
    lappend res $a
  }
  set res
} {1 2 3}

#-------------------------------------------------------------------------
# A TEMP trigger on a table in a database overwritten by a deserialize() 
# is orphaned in the same way as if the db was DETACHed.
#
reset_db
do_execsql_test 1400 {
  CREATE TABLE t1(x);
}
set blob [db serialize main]

do_execsql_test 1410 {
  CREATE TEMP TABLE tlog(y);
  CREATE TEMP TRIGGER tr AFTER INSERT ON main.t1 BEGIN
    INSERT INTO tlog VALUES(new.x);
  END;
  INSERT INTO t1 VALUES(1);
  SELECT * FROM tlog;
} {1}

do_test 1420 { db deserialize main $blob } {}

do_execsql_test 1430 {
  INSERT INTO t1 VALUES(2);
  SELECT * FROM tlog;
} {1}

do_execsql_test 1440 {
  SELECT name, tbl_name FROM temp.sqlite_schema WHERE type='trigger';
} {tr t1}

do_execsql_test 1450 {
  CREATE TEMP TABLE t1(x);
  INSERT INTO temp.t1 VALUES(3);
  SELECT * FROM tlog;
} {1}

do_execsql_test 1460 {
  DROP TRIGGER temp.tr;
  SELECT name FROM temp.sqlite_schema WHERE type='trigger';
} {}

finish_test