os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/jrnlmode.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2008 April 17
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library. The focus
sl@0
    12
# of these tests is the journal mode pragma.
sl@0
    13
#
sl@0
    14
# $Id: jrnlmode.test,v 1.6 2008/09/26 21:08:08 drh Exp $
sl@0
    15
sl@0
    16
set testdir [file dirname $argv0]
sl@0
    17
source $testdir/tester.tcl
sl@0
    18
sl@0
    19
ifcapable {!pager_pragmas} {
sl@0
    20
  finish_test
sl@0
    21
  return
sl@0
    22
}
sl@0
    23
sl@0
    24
#----------------------------------------------------------------------
sl@0
    25
# Test cases jrnlmode-1.X test the PRAGMA logic.
sl@0
    26
#
sl@0
    27
do_test jrnlmode-1.0 {
sl@0
    28
  execsql {
sl@0
    29
    PRAGMA journal_mode;
sl@0
    30
    PRAGMA main.journal_mode;
sl@0
    31
    PRAGMA temp.journal_mode;
sl@0
    32
  } 
sl@0
    33
} [list delete delete delete]
sl@0
    34
do_test jrnlmode-1.1 {
sl@0
    35
  execsql {
sl@0
    36
    PRAGMA journal_mode = persist;
sl@0
    37
  } 
sl@0
    38
} {persist}
sl@0
    39
do_test jrnlmode-1.2 {
sl@0
    40
  execsql {
sl@0
    41
    PRAGMA journal_mode;
sl@0
    42
    PRAGMA main.journal_mode;
sl@0
    43
    PRAGMA temp.journal_mode;
sl@0
    44
  } 
sl@0
    45
} [list persist persist persist]
sl@0
    46
do_test jrnlmode-1.4 {
sl@0
    47
  execsql {
sl@0
    48
    PRAGMA journal_mode = off;
sl@0
    49
  } 
sl@0
    50
} {off}
sl@0
    51
do_test jrnlmode-1.5 {
sl@0
    52
  execsql {
sl@0
    53
    PRAGMA journal_mode;
sl@0
    54
    PRAGMA main.journal_mode;
sl@0
    55
    PRAGMA temp.journal_mode;
sl@0
    56
  } 
sl@0
    57
} {off off off}
sl@0
    58
do_test jrnlmode-1.6 {
sl@0
    59
  execsql {
sl@0
    60
    PRAGMA journal_mode = delete;
sl@0
    61
  } 
sl@0
    62
} {delete}
sl@0
    63
do_test jrnlmode-1.7 {
sl@0
    64
  execsql {
sl@0
    65
    PRAGMA journal_mode;
sl@0
    66
    PRAGMA main.journal_mode;
sl@0
    67
    PRAGMA temp.journal_mode;
sl@0
    68
  } 
sl@0
    69
} {delete delete delete}
sl@0
    70
do_test jrnlmode-1.7.1 {
sl@0
    71
  execsql {
sl@0
    72
    PRAGMA journal_mode = truncate;
sl@0
    73
  } 
sl@0
    74
} {truncate}
sl@0
    75
do_test jrnlmode-1.7.2 {
sl@0
    76
  execsql {
sl@0
    77
    PRAGMA journal_mode;
sl@0
    78
    PRAGMA main.journal_mode;
sl@0
    79
    PRAGMA temp.journal_mode;
sl@0
    80
  } 
sl@0
    81
} {truncate truncate truncate}
sl@0
    82
do_test jrnlmode-1.8 {
sl@0
    83
  execsql {
sl@0
    84
    PRAGMA journal_mode = off;
sl@0
    85
    PRAGMA journal_mode = invalid;
sl@0
    86
  } 
sl@0
    87
} {off off}
sl@0
    88
ifcapable attach {
sl@0
    89
  do_test jrnlmode-1.9 {
sl@0
    90
    execsql {
sl@0
    91
      PRAGMA journal_mode = PERSIST;
sl@0
    92
      ATTACH ':memory:' as aux1;
sl@0
    93
    }
sl@0
    94
    execsql {
sl@0
    95
      PRAGMA main.journal_mode;
sl@0
    96
      PRAGMA aux1.journal_mode;
sl@0
    97
    }
sl@0
    98
  } {persist persist}
sl@0
    99
  do_test jrnlmode-1.10 {
sl@0
   100
    execsql {
sl@0
   101
      PRAGMA main.journal_mode = OFF;
sl@0
   102
    }
sl@0
   103
    execsql {
sl@0
   104
      PRAGMA main.journal_mode;
sl@0
   105
      PRAGMA temp.journal_mode;
sl@0
   106
      PRAGMA aux1.journal_mode;
sl@0
   107
    }
sl@0
   108
  } {off persist persist}
sl@0
   109
  do_test jrnlmode-1.11 {
sl@0
   110
    execsql {
sl@0
   111
      PRAGMA journal_mode;
sl@0
   112
    }
sl@0
   113
  } {persist}
sl@0
   114
  do_test jrnlmode-1.12 {
sl@0
   115
    execsql {
sl@0
   116
      ATTACH ':memory:' as aux2;
sl@0
   117
    }
sl@0
   118
    execsql {
sl@0
   119
      PRAGMA main.journal_mode;
sl@0
   120
      PRAGMA aux1.journal_mode;
sl@0
   121
      PRAGMA aux2.journal_mode;
sl@0
   122
    }
sl@0
   123
  } {off persist persist}
sl@0
   124
  do_test jrnlmode-1.11 {
sl@0
   125
    execsql {
sl@0
   126
      PRAGMA aux1.journal_mode = DELETE;
sl@0
   127
    }
sl@0
   128
    execsql {
sl@0
   129
      PRAGMA main.journal_mode;
sl@0
   130
      PRAGMA aux1.journal_mode;
sl@0
   131
      PRAGMA aux2.journal_mode;
sl@0
   132
    }
sl@0
   133
  } {off delete persist}
sl@0
   134
  do_test jrnlmode-1.12 {
sl@0
   135
    execsql {
sl@0
   136
      PRAGMA journal_mode = delete;
sl@0
   137
    }
sl@0
   138
    execsql {
sl@0
   139
      PRAGMA main.journal_mode;
sl@0
   140
      PRAGMA temp.journal_mode;
sl@0
   141
      PRAGMA aux1.journal_mode;
sl@0
   142
      PRAGMA aux2.journal_mode;
sl@0
   143
    }
sl@0
   144
  } {delete delete delete delete}
sl@0
   145
  do_test jrnlmode-1.13 {
sl@0
   146
    execsql {
sl@0
   147
      ATTACH ':memory:' as aux3;
sl@0
   148
    }
sl@0
   149
    execsql {
sl@0
   150
      PRAGMA main.journal_mode;
sl@0
   151
      PRAGMA temp.journal_mode;
sl@0
   152
      PRAGMA aux1.journal_mode;
sl@0
   153
      PRAGMA aux2.journal_mode;
sl@0
   154
      PRAGMA aux3.journal_mode;
sl@0
   155
    }
sl@0
   156
  } {delete delete delete delete delete}
sl@0
   157
  do_test jrnlmode-1.14 {
sl@0
   158
    execsql {
sl@0
   159
      PRAGMA journal_mode = TRUNCATE;
sl@0
   160
    }
sl@0
   161
    execsql {
sl@0
   162
      PRAGMA main.journal_mode;
sl@0
   163
      PRAGMA temp.journal_mode;
sl@0
   164
      PRAGMA aux1.journal_mode;
sl@0
   165
      PRAGMA aux2.journal_mode;
sl@0
   166
      PRAGMA aux3.journal_mode;
sl@0
   167
    }
sl@0
   168
  } {truncate truncate truncate truncate truncate}
sl@0
   169
  
sl@0
   170
  do_test jrnlmode-1.99 {
sl@0
   171
    execsql {
sl@0
   172
      DETACH aux1;
sl@0
   173
      DETACH aux2;
sl@0
   174
      DETACH aux3;
sl@0
   175
    }
sl@0
   176
  } {}
sl@0
   177
}
sl@0
   178
sl@0
   179
ifcapable attach {
sl@0
   180
  file delete -force test2.db
sl@0
   181
  do_test jrnlmode-2.1 {
sl@0
   182
    execsql {
sl@0
   183
      ATTACH 'test2.db' AS aux;
sl@0
   184
      PRAGMA main.journal_mode = persist;
sl@0
   185
      PRAGMA aux.journal_mode = persist;
sl@0
   186
      CREATE TABLE abc(a, b, c);
sl@0
   187
      CREATE TABLE aux.def(d, e, f);
sl@0
   188
    }
sl@0
   189
    execsql {
sl@0
   190
      BEGIN;
sl@0
   191
      INSERT INTO abc VALUES(1, 2, 3);
sl@0
   192
      INSERT INTO def VALUES(4, 5, 6);
sl@0
   193
      COMMIT;
sl@0
   194
    }
sl@0
   195
    list [file exists test.db-journal] [file exists test2.db-journal]
sl@0
   196
  } {1 1}
sl@0
   197
sl@0
   198
  do_test jrnlmode-2.2 {
sl@0
   199
    file size test.db-journal
sl@0
   200
  } {0}
sl@0
   201
sl@0
   202
  do_test jrnlmode-2.3 {
sl@0
   203
    execsql {
sl@0
   204
      SELECT * FROM abc;
sl@0
   205
    }
sl@0
   206
  } {1 2 3}
sl@0
   207
sl@0
   208
  do_test jrnlmode-2.4 {
sl@0
   209
    file size test.db-journal
sl@0
   210
  } {0}
sl@0
   211
sl@0
   212
  do_test jrnlmode-2.5 {
sl@0
   213
    execsql {
sl@0
   214
      SELECT * FROM def;
sl@0
   215
    }
sl@0
   216
  } {4 5 6}
sl@0
   217
sl@0
   218
#----------------------------------------------------------------------
sl@0
   219
# Test caes jrnlmode-3.X verify that ticket #3127 has been fixed.
sl@0
   220
#
sl@0
   221
  db close
sl@0
   222
  file delete -force test2.db
sl@0
   223
  file delete -force test.db
sl@0
   224
  sqlite3 db test.db
sl@0
   225
sl@0
   226
  do_test jrnlmode-3.1 {
sl@0
   227
    execsql { 
sl@0
   228
      CREATE TABLE x(n INTEGER); 
sl@0
   229
      ATTACH 'test2.db' AS a; 
sl@0
   230
      create table a.x ( n integer ); 
sl@0
   231
      insert into a.x values(1); 
sl@0
   232
      insert into a.x values (2); 
sl@0
   233
      insert into a.x values (3); 
sl@0
   234
      insert into a.x values (4); 
sl@0
   235
    }
sl@0
   236
  } {}
sl@0
   237
  
sl@0
   238
  do_test jrnlmode-3.2 {
sl@0
   239
    execsql { PRAGMA journal_mode=off; }
sl@0
   240
    execsql { 
sl@0
   241
      BEGIN IMMEDIATE;
sl@0
   242
      INSERT OR IGNORE INTO main.x SELECT * FROM a.x;
sl@0
   243
      COMMIT;
sl@0
   244
    }
sl@0
   245
  } {}
sl@0
   246
}
sl@0
   247
sl@0
   248
ifcapable autovacuum&&pragma {
sl@0
   249
  db close
sl@0
   250
  file delete -force test.db
sl@0
   251
  sqlite3 db test.db
sl@0
   252
  do_test jrnlmode-4.1 {
sl@0
   253
    execsql {
sl@0
   254
      PRAGMA cache_size = 1;
sl@0
   255
      PRAGMA auto_vacuum = 1;
sl@0
   256
      CREATE TABLE abc(a, b, c);
sl@0
   257
    }
sl@0
   258
    execsql { PRAGMA page_count }
sl@0
   259
  } {3}
sl@0
   260
sl@0
   261
  do_test jrnlmode-4.2 {
sl@0
   262
    execsql { PRAGMA journal_mode = off }
sl@0
   263
  } {off}
sl@0
   264
sl@0
   265
  do_test jrnlmode-4.3 {
sl@0
   266
    execsql { INSERT INTO abc VALUES(1, 2, randomblob(2000)) }
sl@0
   267
  } {}
sl@0
   268
sl@0
   269
  # This will attempt to truncate the database file. Check that this
sl@0
   270
  # is not a problem when journal_mode=off.
sl@0
   271
  do_test jrnlmode-4.4 {
sl@0
   272
    execsql { DELETE FROM abc }
sl@0
   273
  } {}
sl@0
   274
sl@0
   275
  integrity_check jrnlmode-4.5
sl@0
   276
}
sl@0
   277
sl@0
   278
#------------------------------------------------------------------------
sl@0
   279
# The following test caes, jrnlmode-5.*, test the journal_size_limit
sl@0
   280
# pragma.
sl@0
   281
ifcapable pragma {
sl@0
   282
  db close
sl@0
   283
  file delete -force test.db test2.db test3.db
sl@0
   284
  sqlite3 db test.db
sl@0
   285
sl@0
   286
  do_test jrnlmode-5.1 {
sl@0
   287
    execsql {pragma page_size=1024}
sl@0
   288
    execsql {pragma journal_mode=persist}
sl@0
   289
  } {persist}
sl@0
   290
sl@0
   291
  do_test jrnlmode-5.2 {
sl@0
   292
    execsql { PRAGMA journal_size_limit }
sl@0
   293
  } {-1}
sl@0
   294
  do_test jrnlmode-5.3 {
sl@0
   295
    execsql { 
sl@0
   296
      ATTACH 'test2.db' AS aux;
sl@0
   297
      PRAGMA aux.journal_size_limit;
sl@0
   298
    }
sl@0
   299
  } {-1}
sl@0
   300
  do_test jrnlmode-5.4 {
sl@0
   301
    execsql { PRAGMA aux.journal_size_limit = 10240 }
sl@0
   302
  } {10240}
sl@0
   303
  do_test jrnlmode-5.5 {
sl@0
   304
    execsql { PRAGMA main.journal_size_limit = 20480 }
sl@0
   305
  } {20480}
sl@0
   306
  do_test jrnlmode-5.6 {
sl@0
   307
    execsql { PRAGMA journal_size_limit }
sl@0
   308
  } {20480}
sl@0
   309
  do_test jrnlmode-5.7 {
sl@0
   310
    execsql { PRAGMA aux.journal_size_limit }
sl@0
   311
  } {10240}
sl@0
   312
sl@0
   313
  do_test jrnlmode-5.8 {
sl@0
   314
    execsql { ATTACH 'test3.db' AS aux2 }
sl@0
   315
  } {}
sl@0
   316
sl@0
   317
  do_test jrnlmode-5.9 {
sl@0
   318
    execsql {
sl@0
   319
      CREATE TABLE main.t1(a, b, c);
sl@0
   320
      CREATE TABLE aux.t2(a, b, c);
sl@0
   321
      CREATE TABLE aux2.t3(a, b, c);
sl@0
   322
    }
sl@0
   323
  } {}
sl@0
   324
  do_test jrnlmode-5.10 {
sl@0
   325
    list \
sl@0
   326
      [file exists test.db-journal]  \
sl@0
   327
      [file exists test2.db-journal] \
sl@0
   328
      [file exists test3.db-journal]
sl@0
   329
  } {1 1 1}
sl@0
   330
  do_test jrnlmode-5.11 {
sl@0
   331
    execsql {
sl@0
   332
      BEGIN;
sl@0
   333
      INSERT INTO t3 VALUES(randomblob(1000),randomblob(1000),randomblob(1000));
sl@0
   334
      INSERT INTO t3 
sl@0
   335
          SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
sl@0
   336
      INSERT INTO t3 
sl@0
   337
          SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
sl@0
   338
      INSERT INTO t3 
sl@0
   339
          SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
sl@0
   340
      INSERT INTO t3 
sl@0
   341
          SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
sl@0
   342
      INSERT INTO t3 
sl@0
   343
          SELECT randomblob(1000),randomblob(1000),randomblob(1000) FROM t3;
sl@0
   344
      INSERT INTO t2 SELECT * FROM t3;
sl@0
   345
      INSERT INTO t1 SELECT * FROM t2;
sl@0
   346
      COMMIT;
sl@0
   347
    }
sl@0
   348
    list \
sl@0
   349
      [file exists test.db-journal]  \
sl@0
   350
      [file exists test2.db-journal] \
sl@0
   351
      [file exists test3.db-journal] \
sl@0
   352
      [file size test.db-journal]    \
sl@0
   353
      [file size test2.db-journal]   \
sl@0
   354
      [file size test3.db-journal]
sl@0
   355
  } {1 1 1 0 0 0}
sl@0
   356
sl@0
   357
  do_test jrnlmode-5.12 {
sl@0
   358
    execsql {
sl@0
   359
      BEGIN;
sl@0
   360
      UPDATE t1 SET a = randomblob(1000);
sl@0
   361
    }
sl@0
   362
    expr {[file size test.db-journal]>30000}
sl@0
   363
  } {1}
sl@0
   364
  do_test jrnlmode-5.13 {
sl@0
   365
    execsql COMMIT
sl@0
   366
    file size test.db-journal
sl@0
   367
  } {20480}
sl@0
   368
sl@0
   369
  do_test jrnlmode-5.14 {
sl@0
   370
    execsql {
sl@0
   371
      BEGIN;
sl@0
   372
      UPDATE t2 SET a = randomblob(1000);
sl@0
   373
    }
sl@0
   374
    expr {[file size test2.db-journal]>30000}
sl@0
   375
  } {1}
sl@0
   376
  do_test jrnlmode-5.15 {
sl@0
   377
    execsql COMMIT
sl@0
   378
    file size test2.db-journal
sl@0
   379
  } {10240}
sl@0
   380
sl@0
   381
  do_test jrnlmode-5.16 {
sl@0
   382
    execsql {
sl@0
   383
      BEGIN;
sl@0
   384
      UPDATE t3 SET a = randomblob(1000);
sl@0
   385
    }
sl@0
   386
    set journalsize [file size test3.db-journal]
sl@0
   387
    expr {$journalsize>30000}
sl@0
   388
  } {1}
sl@0
   389
  do_test jrnlmode-5.17 {
sl@0
   390
    execsql COMMIT
sl@0
   391
    set sz [file size test3.db-journal]
sl@0
   392
    expr {$sz>=$journalsize}
sl@0
   393
  } {1}
sl@0
   394
sl@0
   395
  do_test jrnlmode-5.18 {
sl@0
   396
    execsql {
sl@0
   397
      PRAGMA journal_size_limit = -4;
sl@0
   398
      BEGIN;
sl@0
   399
      UPDATE t1 SET a = randomblob(1000);
sl@0
   400
    }
sl@0
   401
    set journalsize [file size test.db-journal]
sl@0
   402
    expr {$journalsize>30000}
sl@0
   403
  } {1}
sl@0
   404
  do_test jrnlmode-5.19 {
sl@0
   405
    execsql COMMIT
sl@0
   406
    set sz [file size test.db-journal]
sl@0
   407
    expr {$sz>=$journalsize}
sl@0
   408
  } {1}
sl@0
   409
}
sl@0
   410
sl@0
   411
finish_test