os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/avtrans.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
# 2001 September 15
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.  This
sl@0
    12
# file is a copy of "trans.test" modified to run under autovacuum mode.
sl@0
    13
# the point is to stress the autovacuum logic and try to get it to fail.
sl@0
    14
#
sl@0
    15
# $Id: avtrans.test,v 1.6 2007/09/12 17:01:45 danielk1977 Exp $
sl@0
    16
sl@0
    17
sl@0
    18
set testdir [file dirname $argv0]
sl@0
    19
source $testdir/tester.tcl
sl@0
    20
sl@0
    21
sl@0
    22
# Create several tables to work with.
sl@0
    23
#
sl@0
    24
do_test avtrans-1.0 {
sl@0
    25
  execsql {
sl@0
    26
    PRAGMA auto_vacuum=ON;
sl@0
    27
    CREATE TABLE one(a int PRIMARY KEY, b text);
sl@0
    28
    INSERT INTO one VALUES(1,'one');
sl@0
    29
    INSERT INTO one VALUES(2,'two');
sl@0
    30
    INSERT INTO one VALUES(3,'three');
sl@0
    31
    SELECT b FROM one ORDER BY a;
sl@0
    32
  }
sl@0
    33
} {one two three}
sl@0
    34
do_test avtrans-1.1 {
sl@0
    35
  execsql {
sl@0
    36
    CREATE TABLE two(a int PRIMARY KEY, b text);
sl@0
    37
    INSERT INTO two VALUES(1,'I');
sl@0
    38
    INSERT INTO two VALUES(5,'V');
sl@0
    39
    INSERT INTO two VALUES(10,'X');
sl@0
    40
    SELECT b FROM two ORDER BY a;
sl@0
    41
  }
sl@0
    42
} {I V X}
sl@0
    43
do_test avtrans-1.9 {
sl@0
    44
  sqlite3 altdb test.db
sl@0
    45
  execsql {SELECT b FROM one ORDER BY a} altdb
sl@0
    46
} {one two three}
sl@0
    47
do_test avtrans-1.10 {
sl@0
    48
  execsql {SELECT b FROM two ORDER BY a} altdb
sl@0
    49
} {I V X}
sl@0
    50
integrity_check avtrans-1.11
sl@0
    51
sl@0
    52
# Basic transactions
sl@0
    53
#
sl@0
    54
do_test avtrans-2.1 {
sl@0
    55
  set v [catch {execsql {BEGIN}} msg]
sl@0
    56
  lappend v $msg
sl@0
    57
} {0 {}}
sl@0
    58
do_test avtrans-2.2 {
sl@0
    59
  set v [catch {execsql {END}} msg]
sl@0
    60
  lappend v $msg
sl@0
    61
} {0 {}}
sl@0
    62
do_test avtrans-2.3 {
sl@0
    63
  set v [catch {execsql {BEGIN TRANSACTION}} msg]
sl@0
    64
  lappend v $msg
sl@0
    65
} {0 {}}
sl@0
    66
do_test avtrans-2.4 {
sl@0
    67
  set v [catch {execsql {COMMIT TRANSACTION}} msg]
sl@0
    68
  lappend v $msg
sl@0
    69
} {0 {}}
sl@0
    70
do_test avtrans-2.5 {
sl@0
    71
  set v [catch {execsql {BEGIN TRANSACTION 'foo'}} msg]
sl@0
    72
  lappend v $msg
sl@0
    73
} {0 {}}
sl@0
    74
do_test avtrans-2.6 {
sl@0
    75
  set v [catch {execsql {ROLLBACK TRANSACTION 'foo'}} msg]
sl@0
    76
  lappend v $msg
sl@0
    77
} {0 {}}
sl@0
    78
do_test avtrans-2.10 {
sl@0
    79
  execsql {
sl@0
    80
    BEGIN;
sl@0
    81
    SELECT a FROM one ORDER BY a;
sl@0
    82
    SELECT a FROM two ORDER BY a;
sl@0
    83
    END;
sl@0
    84
  }
sl@0
    85
} {1 2 3 1 5 10}
sl@0
    86
integrity_check avtrans-2.11
sl@0
    87
sl@0
    88
# Check the locking behavior
sl@0
    89
#
sl@0
    90
sqlite3_soft_heap_limit 0
sl@0
    91
do_test avtrans-3.1 {
sl@0
    92
  execsql {
sl@0
    93
    BEGIN;
sl@0
    94
    UPDATE one SET a = 0 WHERE 0;
sl@0
    95
    SELECT a FROM one ORDER BY a;
sl@0
    96
  }
sl@0
    97
} {1 2 3}
sl@0
    98
do_test avtrans-3.2 {
sl@0
    99
  catchsql {
sl@0
   100
    SELECT a FROM two ORDER BY a;
sl@0
   101
  } altdb
sl@0
   102
} {0 {1 5 10}}
sl@0
   103
do_test avtrans-3.3 {
sl@0
   104
  catchsql {
sl@0
   105
    SELECT a FROM one ORDER BY a;
sl@0
   106
  } altdb
sl@0
   107
} {0 {1 2 3}}
sl@0
   108
do_test avtrans-3.4 {
sl@0
   109
  catchsql {
sl@0
   110
    INSERT INTO one VALUES(4,'four');
sl@0
   111
  }
sl@0
   112
} {0 {}}
sl@0
   113
do_test avtrans-3.5 {
sl@0
   114
  catchsql {
sl@0
   115
    SELECT a FROM two ORDER BY a;
sl@0
   116
  } altdb
sl@0
   117
} {0 {1 5 10}}
sl@0
   118
do_test avtrans-3.6 {
sl@0
   119
  catchsql {
sl@0
   120
    SELECT a FROM one ORDER BY a;
sl@0
   121
  } altdb
sl@0
   122
} {0 {1 2 3}}
sl@0
   123
do_test avtrans-3.7 {
sl@0
   124
  catchsql {
sl@0
   125
    INSERT INTO two VALUES(4,'IV');
sl@0
   126
  }
sl@0
   127
} {0 {}}
sl@0
   128
do_test avtrans-3.8 {
sl@0
   129
  catchsql {
sl@0
   130
    SELECT a FROM two ORDER BY a;
sl@0
   131
  } altdb
sl@0
   132
} {0 {1 5 10}}
sl@0
   133
do_test avtrans-3.9 {
sl@0
   134
  catchsql {
sl@0
   135
    SELECT a FROM one ORDER BY a;
sl@0
   136
  } altdb
sl@0
   137
} {0 {1 2 3}}
sl@0
   138
do_test avtrans-3.10 {
sl@0
   139
  execsql {END TRANSACTION}
sl@0
   140
} {}
sl@0
   141
do_test avtrans-3.11 {
sl@0
   142
  set v [catch {execsql {
sl@0
   143
    SELECT a FROM two ORDER BY a;
sl@0
   144
  } altdb} msg]
sl@0
   145
  lappend v $msg
sl@0
   146
} {0 {1 4 5 10}}
sl@0
   147
do_test avtrans-3.12 {
sl@0
   148
  set v [catch {execsql {
sl@0
   149
    SELECT a FROM one ORDER BY a;
sl@0
   150
  } altdb} msg]
sl@0
   151
  lappend v $msg
sl@0
   152
} {0 {1 2 3 4}}
sl@0
   153
do_test avtrans-3.13 {
sl@0
   154
  set v [catch {execsql {
sl@0
   155
    SELECT a FROM two ORDER BY a;
sl@0
   156
  } db} msg]
sl@0
   157
  lappend v $msg
sl@0
   158
} {0 {1 4 5 10}}
sl@0
   159
do_test avtrans-3.14 {
sl@0
   160
  set v [catch {execsql {
sl@0
   161
    SELECT a FROM one ORDER BY a;
sl@0
   162
  } db} msg]
sl@0
   163
  lappend v $msg
sl@0
   164
} {0 {1 2 3 4}}
sl@0
   165
sqlite3_soft_heap_limit $soft_limit
sl@0
   166
integrity_check avtrans-3.15
sl@0
   167
sl@0
   168
do_test avtrans-4.1 {
sl@0
   169
  set v [catch {execsql {
sl@0
   170
    COMMIT;
sl@0
   171
  } db} msg]
sl@0
   172
  lappend v $msg
sl@0
   173
} {1 {cannot commit - no transaction is active}}
sl@0
   174
do_test avtrans-4.2 {
sl@0
   175
  set v [catch {execsql {
sl@0
   176
    ROLLBACK;
sl@0
   177
  } db} msg]
sl@0
   178
  lappend v $msg
sl@0
   179
} {1 {cannot rollback - no transaction is active}}
sl@0
   180
do_test avtrans-4.3 {
sl@0
   181
  catchsql {
sl@0
   182
    BEGIN TRANSACTION;
sl@0
   183
    UPDATE two SET a = 0 WHERE 0;
sl@0
   184
    SELECT a FROM two ORDER BY a;
sl@0
   185
  } db
sl@0
   186
} {0 {1 4 5 10}}
sl@0
   187
do_test avtrans-4.4 {
sl@0
   188
  catchsql {
sl@0
   189
    SELECT a FROM two ORDER BY a;
sl@0
   190
  } altdb
sl@0
   191
} {0 {1 4 5 10}}
sl@0
   192
do_test avtrans-4.5 {
sl@0
   193
  catchsql {
sl@0
   194
    SELECT a FROM one ORDER BY a;
sl@0
   195
  } altdb
sl@0
   196
} {0 {1 2 3 4}}
sl@0
   197
do_test avtrans-4.6 {
sl@0
   198
  catchsql {
sl@0
   199
    BEGIN TRANSACTION;
sl@0
   200
    SELECT a FROM one ORDER BY a;
sl@0
   201
  } db
sl@0
   202
} {1 {cannot start a transaction within a transaction}}
sl@0
   203
do_test avtrans-4.7 {
sl@0
   204
  catchsql {
sl@0
   205
    SELECT a FROM two ORDER BY a;
sl@0
   206
  } altdb
sl@0
   207
} {0 {1 4 5 10}}
sl@0
   208
do_test avtrans-4.8 {
sl@0
   209
  catchsql {
sl@0
   210
    SELECT a FROM one ORDER BY a;
sl@0
   211
  } altdb
sl@0
   212
} {0 {1 2 3 4}}
sl@0
   213
do_test avtrans-4.9 {
sl@0
   214
  set v [catch {execsql {
sl@0
   215
    END TRANSACTION;
sl@0
   216
    SELECT a FROM two ORDER BY a;
sl@0
   217
  } db} msg]
sl@0
   218
  lappend v $msg
sl@0
   219
} {0 {1 4 5 10}}
sl@0
   220
do_test avtrans-4.10 {
sl@0
   221
  set v [catch {execsql {
sl@0
   222
    SELECT a FROM two ORDER BY a;
sl@0
   223
  } altdb} msg]
sl@0
   224
  lappend v $msg
sl@0
   225
} {0 {1 4 5 10}}
sl@0
   226
do_test avtrans-4.11 {
sl@0
   227
  set v [catch {execsql {
sl@0
   228
    SELECT a FROM one ORDER BY a;
sl@0
   229
  } altdb} msg]
sl@0
   230
  lappend v $msg
sl@0
   231
} {0 {1 2 3 4}}
sl@0
   232
integrity_check avtrans-4.12
sl@0
   233
do_test avtrans-4.98 {
sl@0
   234
  altdb close
sl@0
   235
  execsql {
sl@0
   236
    DROP TABLE one;
sl@0
   237
    DROP TABLE two;
sl@0
   238
  }
sl@0
   239
} {}
sl@0
   240
integrity_check avtrans-4.99
sl@0
   241
sl@0
   242
# Check out the commit/rollback behavior of the database
sl@0
   243
#
sl@0
   244
do_test avtrans-5.1 {
sl@0
   245
  execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
sl@0
   246
} {}
sl@0
   247
do_test avtrans-5.2 {
sl@0
   248
  execsql {BEGIN TRANSACTION}
sl@0
   249
  execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
sl@0
   250
} {}
sl@0
   251
do_test avtrans-5.3 {
sl@0
   252
  execsql {CREATE TABLE one(a text, b int)}
sl@0
   253
  execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
sl@0
   254
} {one}
sl@0
   255
do_test avtrans-5.4 {
sl@0
   256
  execsql {SELECT a,b FROM one ORDER BY b}
sl@0
   257
} {}
sl@0
   258
do_test avtrans-5.5 {
sl@0
   259
  execsql {INSERT INTO one(a,b) VALUES('hello', 1)}
sl@0
   260
  execsql {SELECT a,b FROM one ORDER BY b}
sl@0
   261
} {hello 1}
sl@0
   262
do_test avtrans-5.6 {
sl@0
   263
  execsql {ROLLBACK}
sl@0
   264
  execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
sl@0
   265
} {}
sl@0
   266
do_test avtrans-5.7 {
sl@0
   267
  set v [catch {
sl@0
   268
    execsql {SELECT a,b FROM one ORDER BY b}
sl@0
   269
  } msg]
sl@0
   270
  lappend v $msg
sl@0
   271
} {1 {no such table: one}}
sl@0
   272
sl@0
   273
# Test commits and rollbacks of table CREATE TABLEs, CREATE INDEXs
sl@0
   274
# DROP TABLEs and DROP INDEXs
sl@0
   275
#
sl@0
   276
do_test avtrans-5.8 {
sl@0
   277
  execsql {
sl@0
   278
    SELECT name fROM sqlite_master 
sl@0
   279
    WHERE type='table' OR type='index'
sl@0
   280
    ORDER BY name
sl@0
   281
  }
sl@0
   282
} {}
sl@0
   283
do_test avtrans-5.9 {
sl@0
   284
  execsql {
sl@0
   285
    BEGIN TRANSACTION;
sl@0
   286
    CREATE TABLE t1(a int, b int, c int);
sl@0
   287
    SELECT name fROM sqlite_master 
sl@0
   288
    WHERE type='table' OR type='index'
sl@0
   289
    ORDER BY name;
sl@0
   290
  }
sl@0
   291
} {t1}
sl@0
   292
do_test avtrans-5.10 {
sl@0
   293
  execsql {
sl@0
   294
    CREATE INDEX i1 ON t1(a);
sl@0
   295
    SELECT name fROM sqlite_master 
sl@0
   296
    WHERE type='table' OR type='index'
sl@0
   297
    ORDER BY name;
sl@0
   298
  }
sl@0
   299
} {i1 t1}
sl@0
   300
do_test avtrans-5.11 {
sl@0
   301
  execsql {
sl@0
   302
    COMMIT;
sl@0
   303
    SELECT name fROM sqlite_master 
sl@0
   304
    WHERE type='table' OR type='index'
sl@0
   305
    ORDER BY name;
sl@0
   306
  }
sl@0
   307
} {i1 t1}
sl@0
   308
do_test avtrans-5.12 {
sl@0
   309
  execsql {
sl@0
   310
    BEGIN TRANSACTION;
sl@0
   311
    CREATE TABLE t2(a int, b int, c int);
sl@0
   312
    CREATE INDEX i2a ON t2(a);
sl@0
   313
    CREATE INDEX i2b ON t2(b);
sl@0
   314
    DROP TABLE t1;
sl@0
   315
    SELECT name fROM sqlite_master 
sl@0
   316
    WHERE type='table' OR type='index'
sl@0
   317
    ORDER BY name;
sl@0
   318
  }
sl@0
   319
} {i2a i2b t2}
sl@0
   320
do_test avtrans-5.13 {
sl@0
   321
  execsql {
sl@0
   322
    ROLLBACK;
sl@0
   323
    SELECT name fROM sqlite_master 
sl@0
   324
    WHERE type='table' OR type='index'
sl@0
   325
    ORDER BY name;
sl@0
   326
  }
sl@0
   327
} {i1 t1}
sl@0
   328
do_test avtrans-5.14 {
sl@0
   329
  execsql {
sl@0
   330
    BEGIN TRANSACTION;
sl@0
   331
    DROP INDEX i1;
sl@0
   332
    SELECT name fROM sqlite_master 
sl@0
   333
    WHERE type='table' OR type='index'
sl@0
   334
    ORDER BY name;
sl@0
   335
  }
sl@0
   336
} {t1}
sl@0
   337
do_test avtrans-5.15 {
sl@0
   338
  execsql {
sl@0
   339
    ROLLBACK;
sl@0
   340
    SELECT name fROM sqlite_master 
sl@0
   341
    WHERE type='table' OR type='index'
sl@0
   342
    ORDER BY name;
sl@0
   343
  }
sl@0
   344
} {i1 t1}
sl@0
   345
do_test avtrans-5.16 {
sl@0
   346
  execsql {
sl@0
   347
    BEGIN TRANSACTION;
sl@0
   348
    DROP INDEX i1;
sl@0
   349
    CREATE TABLE t2(x int, y int, z int);
sl@0
   350
    CREATE INDEX i2x ON t2(x);
sl@0
   351
    CREATE INDEX i2y ON t2(y);
sl@0
   352
    INSERT INTO t2 VALUES(1,2,3);
sl@0
   353
    SELECT name fROM sqlite_master 
sl@0
   354
    WHERE type='table' OR type='index'
sl@0
   355
    ORDER BY name;
sl@0
   356
  }
sl@0
   357
} {i2x i2y t1 t2}
sl@0
   358
do_test avtrans-5.17 {
sl@0
   359
  execsql {
sl@0
   360
    COMMIT;
sl@0
   361
    SELECT name fROM sqlite_master 
sl@0
   362
    WHERE type='table' OR type='index'
sl@0
   363
    ORDER BY name;
sl@0
   364
  }
sl@0
   365
} {i2x i2y t1 t2}
sl@0
   366
do_test avtrans-5.18 {
sl@0
   367
  execsql {
sl@0
   368
    SELECT * FROM t2;
sl@0
   369
  }
sl@0
   370
} {1 2 3}
sl@0
   371
do_test avtrans-5.19 {
sl@0
   372
  execsql {
sl@0
   373
    SELECT x FROM t2 WHERE y=2;
sl@0
   374
  }
sl@0
   375
} {1}
sl@0
   376
do_test avtrans-5.20 {
sl@0
   377
  execsql {
sl@0
   378
    BEGIN TRANSACTION;
sl@0
   379
    DROP TABLE t1;
sl@0
   380
    DROP TABLE t2;
sl@0
   381
    SELECT name fROM sqlite_master 
sl@0
   382
    WHERE type='table' OR type='index'
sl@0
   383
    ORDER BY name;
sl@0
   384
  }
sl@0
   385
} {}
sl@0
   386
do_test avtrans-5.21 {
sl@0
   387
  set r [catch {execsql {
sl@0
   388
    SELECT * FROM t2
sl@0
   389
  }} msg]
sl@0
   390
  lappend r $msg
sl@0
   391
} {1 {no such table: t2}}
sl@0
   392
do_test avtrans-5.22 {
sl@0
   393
  execsql {
sl@0
   394
    ROLLBACK;
sl@0
   395
    SELECT name fROM sqlite_master 
sl@0
   396
    WHERE type='table' OR type='index'
sl@0
   397
    ORDER BY name;
sl@0
   398
  }
sl@0
   399
} {i2x i2y t1 t2}
sl@0
   400
do_test avtrans-5.23 {
sl@0
   401
  execsql {
sl@0
   402
    SELECT * FROM t2;
sl@0
   403
  }
sl@0
   404
} {1 2 3}
sl@0
   405
integrity_check avtrans-5.23
sl@0
   406
sl@0
   407
sl@0
   408
# Try to DROP and CREATE tables and indices with the same name
sl@0
   409
# within a transaction.  Make sure ROLLBACK works.
sl@0
   410
#
sl@0
   411
do_test avtrans-6.1 {
sl@0
   412
  execsql2 {
sl@0
   413
    INSERT INTO t1 VALUES(1,2,3);
sl@0
   414
    BEGIN TRANSACTION;
sl@0
   415
    DROP TABLE t1;
sl@0
   416
    CREATE TABLE t1(p,q,r);
sl@0
   417
    ROLLBACK;
sl@0
   418
    SELECT * FROM t1;
sl@0
   419
  }
sl@0
   420
} {a 1 b 2 c 3}
sl@0
   421
do_test avtrans-6.2 {
sl@0
   422
  execsql2 {
sl@0
   423
    INSERT INTO t1 VALUES(1,2,3);
sl@0
   424
    BEGIN TRANSACTION;
sl@0
   425
    DROP TABLE t1;
sl@0
   426
    CREATE TABLE t1(p,q,r);
sl@0
   427
    COMMIT;
sl@0
   428
    SELECT * FROM t1;
sl@0
   429
  }
sl@0
   430
} {}
sl@0
   431
do_test avtrans-6.3 {
sl@0
   432
  execsql2 {
sl@0
   433
    INSERT INTO t1 VALUES(1,2,3);
sl@0
   434
    SELECT * FROM t1;
sl@0
   435
  }
sl@0
   436
} {p 1 q 2 r 3}
sl@0
   437
do_test avtrans-6.4 {
sl@0
   438
  execsql2 {
sl@0
   439
    BEGIN TRANSACTION;
sl@0
   440
    DROP TABLE t1;
sl@0
   441
    CREATE TABLE t1(a,b,c);
sl@0
   442
    INSERT INTO t1 VALUES(4,5,6);
sl@0
   443
    SELECT * FROM t1;
sl@0
   444
    DROP TABLE t1;
sl@0
   445
  }
sl@0
   446
} {a 4 b 5 c 6}
sl@0
   447
do_test avtrans-6.5 {
sl@0
   448
  execsql2 {
sl@0
   449
    ROLLBACK;
sl@0
   450
    SELECT * FROM t1;
sl@0
   451
  }
sl@0
   452
} {p 1 q 2 r 3}
sl@0
   453
do_test avtrans-6.6 {
sl@0
   454
  execsql2 {
sl@0
   455
    BEGIN TRANSACTION;
sl@0
   456
    DROP TABLE t1;
sl@0
   457
    CREATE TABLE t1(a,b,c);
sl@0
   458
    INSERT INTO t1 VALUES(4,5,6);
sl@0
   459
    SELECT * FROM t1;
sl@0
   460
    DROP TABLE t1;
sl@0
   461
  }
sl@0
   462
} {a 4 b 5 c 6}
sl@0
   463
do_test avtrans-6.7 {
sl@0
   464
  catchsql {
sl@0
   465
    COMMIT;
sl@0
   466
    SELECT * FROM t1;
sl@0
   467
  }
sl@0
   468
} {1 {no such table: t1}}
sl@0
   469
sl@0
   470
# Repeat on a table with an automatically generated index.
sl@0
   471
#
sl@0
   472
do_test avtrans-6.10 {
sl@0
   473
  execsql2 {
sl@0
   474
    CREATE TABLE t1(a unique,b,c);
sl@0
   475
    INSERT INTO t1 VALUES(1,2,3);
sl@0
   476
    BEGIN TRANSACTION;
sl@0
   477
    DROP TABLE t1;
sl@0
   478
    CREATE TABLE t1(p unique,q,r);
sl@0
   479
    ROLLBACK;
sl@0
   480
    SELECT * FROM t1;
sl@0
   481
  }
sl@0
   482
} {a 1 b 2 c 3}
sl@0
   483
do_test avtrans-6.11 {
sl@0
   484
  execsql2 {
sl@0
   485
    BEGIN TRANSACTION;
sl@0
   486
    DROP TABLE t1;
sl@0
   487
    CREATE TABLE t1(p unique,q,r);
sl@0
   488
    COMMIT;
sl@0
   489
    SELECT * FROM t1;
sl@0
   490
  }
sl@0
   491
} {}
sl@0
   492
do_test avtrans-6.12 {
sl@0
   493
  execsql2 {
sl@0
   494
    INSERT INTO t1 VALUES(1,2,3);
sl@0
   495
    SELECT * FROM t1;
sl@0
   496
  }
sl@0
   497
} {p 1 q 2 r 3}
sl@0
   498
do_test avtrans-6.13 {
sl@0
   499
  execsql2 {
sl@0
   500
    BEGIN TRANSACTION;
sl@0
   501
    DROP TABLE t1;
sl@0
   502
    CREATE TABLE t1(a unique,b,c);
sl@0
   503
    INSERT INTO t1 VALUES(4,5,6);
sl@0
   504
    SELECT * FROM t1;
sl@0
   505
    DROP TABLE t1;
sl@0
   506
  }
sl@0
   507
} {a 4 b 5 c 6}
sl@0
   508
do_test avtrans-6.14 {
sl@0
   509
  execsql2 {
sl@0
   510
    ROLLBACK;
sl@0
   511
    SELECT * FROM t1;
sl@0
   512
  }
sl@0
   513
} {p 1 q 2 r 3}
sl@0
   514
do_test avtrans-6.15 {
sl@0
   515
  execsql2 {
sl@0
   516
    BEGIN TRANSACTION;
sl@0
   517
    DROP TABLE t1;
sl@0
   518
    CREATE TABLE t1(a unique,b,c);
sl@0
   519
    INSERT INTO t1 VALUES(4,5,6);
sl@0
   520
    SELECT * FROM t1;
sl@0
   521
    DROP TABLE t1;
sl@0
   522
  }
sl@0
   523
} {a 4 b 5 c 6}
sl@0
   524
do_test avtrans-6.16 {
sl@0
   525
  catchsql {
sl@0
   526
    COMMIT;
sl@0
   527
    SELECT * FROM t1;
sl@0
   528
  }
sl@0
   529
} {1 {no such table: t1}}
sl@0
   530
sl@0
   531
do_test avtrans-6.20 {
sl@0
   532
  execsql {
sl@0
   533
    CREATE TABLE t1(a integer primary key,b,c);
sl@0
   534
    INSERT INTO t1 VALUES(1,-2,-3);
sl@0
   535
    INSERT INTO t1 VALUES(4,-5,-6);
sl@0
   536
    SELECT * FROM t1;
sl@0
   537
  }
sl@0
   538
} {1 -2 -3 4 -5 -6}
sl@0
   539
do_test avtrans-6.21 {
sl@0
   540
  execsql {
sl@0
   541
    CREATE INDEX i1 ON t1(b);
sl@0
   542
    SELECT * FROM t1 WHERE b<1;
sl@0
   543
  }
sl@0
   544
} {4 -5 -6 1 -2 -3}
sl@0
   545
do_test avtrans-6.22 {
sl@0
   546
  execsql {
sl@0
   547
    BEGIN TRANSACTION;
sl@0
   548
    DROP INDEX i1;
sl@0
   549
    SELECT * FROM t1 WHERE b<1;
sl@0
   550
    ROLLBACK;
sl@0
   551
  }
sl@0
   552
} {1 -2 -3 4 -5 -6}
sl@0
   553
do_test avtrans-6.23 {
sl@0
   554
  execsql {
sl@0
   555
    SELECT * FROM t1 WHERE b<1;
sl@0
   556
  }
sl@0
   557
} {4 -5 -6 1 -2 -3}
sl@0
   558
do_test avtrans-6.24 {
sl@0
   559
  execsql {
sl@0
   560
    BEGIN TRANSACTION;
sl@0
   561
    DROP TABLE t1;
sl@0
   562
    ROLLBACK;
sl@0
   563
    SELECT * FROM t1 WHERE b<1;
sl@0
   564
  }
sl@0
   565
} {4 -5 -6 1 -2 -3}
sl@0
   566
sl@0
   567
do_test avtrans-6.25 {
sl@0
   568
  execsql {
sl@0
   569
    BEGIN TRANSACTION;
sl@0
   570
    DROP INDEX i1;
sl@0
   571
    CREATE INDEX i1 ON t1(c);
sl@0
   572
    SELECT * FROM t1 WHERE b<1;
sl@0
   573
  }
sl@0
   574
} {1 -2 -3 4 -5 -6}
sl@0
   575
do_test avtrans-6.26 {
sl@0
   576
  execsql {
sl@0
   577
    SELECT * FROM t1 WHERE c<1;
sl@0
   578
  }
sl@0
   579
} {4 -5 -6 1 -2 -3}
sl@0
   580
do_test avtrans-6.27 {
sl@0
   581
  execsql {
sl@0
   582
    ROLLBACK;
sl@0
   583
    SELECT * FROM t1 WHERE b<1;
sl@0
   584
  }
sl@0
   585
} {4 -5 -6 1 -2 -3}
sl@0
   586
do_test avtrans-6.28 {
sl@0
   587
  execsql {
sl@0
   588
    SELECT * FROM t1 WHERE c<1;
sl@0
   589
  }
sl@0
   590
} {1 -2 -3 4 -5 -6}
sl@0
   591
sl@0
   592
# The following repeats steps 6.20 through 6.28, but puts a "unique"
sl@0
   593
# constraint the first field of the table in order to generate an
sl@0
   594
# automatic index.
sl@0
   595
#
sl@0
   596
do_test avtrans-6.30 {
sl@0
   597
  execsql {
sl@0
   598
    BEGIN TRANSACTION;
sl@0
   599
    DROP TABLE t1;
sl@0
   600
    CREATE TABLE t1(a int unique,b,c);
sl@0
   601
    COMMIT;
sl@0
   602
    INSERT INTO t1 VALUES(1,-2,-3);
sl@0
   603
    INSERT INTO t1 VALUES(4,-5,-6);
sl@0
   604
    SELECT * FROM t1 ORDER BY a;
sl@0
   605
  }
sl@0
   606
} {1 -2 -3 4 -5 -6}
sl@0
   607
do_test avtrans-6.31 {
sl@0
   608
  execsql {
sl@0
   609
    CREATE INDEX i1 ON t1(b);
sl@0
   610
    SELECT * FROM t1 WHERE b<1;
sl@0
   611
  }
sl@0
   612
} {4 -5 -6 1 -2 -3}
sl@0
   613
do_test avtrans-6.32 {
sl@0
   614
  execsql {
sl@0
   615
    BEGIN TRANSACTION;
sl@0
   616
    DROP INDEX i1;
sl@0
   617
    SELECT * FROM t1 WHERE b<1;
sl@0
   618
    ROLLBACK;
sl@0
   619
  }
sl@0
   620
} {1 -2 -3 4 -5 -6}
sl@0
   621
do_test avtrans-6.33 {
sl@0
   622
  execsql {
sl@0
   623
    SELECT * FROM t1 WHERE b<1;
sl@0
   624
  }
sl@0
   625
} {4 -5 -6 1 -2 -3}
sl@0
   626
do_test avtrans-6.34 {
sl@0
   627
  execsql {
sl@0
   628
    BEGIN TRANSACTION;
sl@0
   629
    DROP TABLE t1;
sl@0
   630
    ROLLBACK;
sl@0
   631
    SELECT * FROM t1 WHERE b<1;
sl@0
   632
  }
sl@0
   633
} {4 -5 -6 1 -2 -3}
sl@0
   634
sl@0
   635
do_test avtrans-6.35 {
sl@0
   636
  execsql {
sl@0
   637
    BEGIN TRANSACTION;
sl@0
   638
    DROP INDEX i1;
sl@0
   639
    CREATE INDEX i1 ON t1(c);
sl@0
   640
    SELECT * FROM t1 WHERE b<1;
sl@0
   641
  }
sl@0
   642
} {1 -2 -3 4 -5 -6}
sl@0
   643
do_test avtrans-6.36 {
sl@0
   644
  execsql {
sl@0
   645
    SELECT * FROM t1 WHERE c<1;
sl@0
   646
  }
sl@0
   647
} {4 -5 -6 1 -2 -3}
sl@0
   648
do_test avtrans-6.37 {
sl@0
   649
  execsql {
sl@0
   650
    DROP INDEX i1;
sl@0
   651
    SELECT * FROM t1 WHERE c<1;
sl@0
   652
  }
sl@0
   653
} {1 -2 -3 4 -5 -6}
sl@0
   654
do_test avtrans-6.38 {
sl@0
   655
  execsql {
sl@0
   656
    ROLLBACK;
sl@0
   657
    SELECT * FROM t1 WHERE b<1;
sl@0
   658
  }
sl@0
   659
} {4 -5 -6 1 -2 -3}
sl@0
   660
do_test avtrans-6.39 {
sl@0
   661
  execsql {
sl@0
   662
    SELECT * FROM t1 WHERE c<1;
sl@0
   663
  }
sl@0
   664
} {1 -2 -3 4 -5 -6}
sl@0
   665
integrity_check avtrans-6.40
sl@0
   666
sl@0
   667
ifcapable !floatingpoint {
sl@0
   668
  finish_test
sl@0
   669
  return
sl@0
   670
}
sl@0
   671
sl@0
   672
# Test to make sure rollback restores the database back to its original
sl@0
   673
# state.
sl@0
   674
#
sl@0
   675
do_test avtrans-7.1 {
sl@0
   676
  execsql {BEGIN}
sl@0
   677
  for {set i 0} {$i<1000} {incr i} {
sl@0
   678
    set r1 [expr {rand()}]
sl@0
   679
    set r2 [expr {rand()}]
sl@0
   680
    set r3 [expr {rand()}]
sl@0
   681
    execsql "INSERT INTO t2 VALUES($r1,$r2,$r3)"
sl@0
   682
  }
sl@0
   683
  execsql {COMMIT}
sl@0
   684
  set ::checksum [execsql {SELECT md5sum(x,y,z) FROM t2}]
sl@0
   685
  set ::checksum2 [
sl@0
   686
    execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   687
  ]
sl@0
   688
  execsql {SELECT count(*) FROM t2}
sl@0
   689
} {1001}
sl@0
   690
do_test avtrans-7.2 {
sl@0
   691
  execsql {SELECT md5sum(x,y,z) FROM t2}
sl@0
   692
} $checksum
sl@0
   693
do_test avtrans-7.2.1 {
sl@0
   694
  execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   695
} $checksum2
sl@0
   696
do_test avtrans-7.3 {
sl@0
   697
  execsql {
sl@0
   698
    BEGIN;
sl@0
   699
    DELETE FROM t2;
sl@0
   700
    ROLLBACK;
sl@0
   701
    SELECT md5sum(x,y,z) FROM t2;
sl@0
   702
  }
sl@0
   703
} $checksum
sl@0
   704
do_test avtrans-7.4 {
sl@0
   705
  execsql {
sl@0
   706
    BEGIN;
sl@0
   707
    INSERT INTO t2 SELECT * FROM t2;
sl@0
   708
    ROLLBACK;
sl@0
   709
    SELECT md5sum(x,y,z) FROM t2;
sl@0
   710
  }
sl@0
   711
} $checksum
sl@0
   712
do_test avtrans-7.5 {
sl@0
   713
  execsql {
sl@0
   714
    BEGIN;
sl@0
   715
    DELETE FROM t2;
sl@0
   716
    ROLLBACK;
sl@0
   717
    SELECT md5sum(x,y,z) FROM t2;
sl@0
   718
  }
sl@0
   719
} $checksum
sl@0
   720
do_test avtrans-7.6 {
sl@0
   721
  execsql {
sl@0
   722
    BEGIN;
sl@0
   723
    INSERT INTO t2 SELECT * FROM t2;
sl@0
   724
    ROLLBACK;
sl@0
   725
    SELECT md5sum(x,y,z) FROM t2;
sl@0
   726
  }
sl@0
   727
} $checksum
sl@0
   728
do_test avtrans-7.7 {
sl@0
   729
  execsql {
sl@0
   730
    BEGIN;
sl@0
   731
    CREATE TABLE t3 AS SELECT * FROM t2;
sl@0
   732
    INSERT INTO t2 SELECT * FROM t3;
sl@0
   733
    ROLLBACK;
sl@0
   734
    SELECT md5sum(x,y,z) FROM t2;
sl@0
   735
  }
sl@0
   736
} $checksum
sl@0
   737
do_test avtrans-7.8 {
sl@0
   738
  execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   739
} $checksum2
sl@0
   740
ifcapable tempdb {
sl@0
   741
  do_test avtrans-7.9 {
sl@0
   742
    execsql {
sl@0
   743
      BEGIN;
sl@0
   744
      CREATE TEMP TABLE t3 AS SELECT * FROM t2;
sl@0
   745
      INSERT INTO t2 SELECT * FROM t3;
sl@0
   746
      ROLLBACK;
sl@0
   747
      SELECT md5sum(x,y,z) FROM t2;
sl@0
   748
    }
sl@0
   749
  } $checksum
sl@0
   750
}
sl@0
   751
do_test avtrans-7.10 {
sl@0
   752
  execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   753
} $checksum2
sl@0
   754
ifcapable tempdb {
sl@0
   755
  do_test avtrans-7.11 {
sl@0
   756
    execsql {
sl@0
   757
      BEGIN;
sl@0
   758
      CREATE TEMP TABLE t3 AS SELECT * FROM t2;
sl@0
   759
      INSERT INTO t2 SELECT * FROM t3;
sl@0
   760
      DROP INDEX i2x;
sl@0
   761
      DROP INDEX i2y;
sl@0
   762
      CREATE INDEX i3a ON t3(x);
sl@0
   763
      ROLLBACK;
sl@0
   764
      SELECT md5sum(x,y,z) FROM t2;
sl@0
   765
    }
sl@0
   766
  } $checksum
sl@0
   767
}
sl@0
   768
do_test avtrans-7.12 {
sl@0
   769
  execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   770
} $checksum2
sl@0
   771
ifcapable tempdb {
sl@0
   772
  do_test avtrans-7.13 {
sl@0
   773
    execsql {
sl@0
   774
      BEGIN;
sl@0
   775
      DROP TABLE t2;
sl@0
   776
      ROLLBACK;
sl@0
   777
      SELECT md5sum(x,y,z) FROM t2;
sl@0
   778
    }
sl@0
   779
  } $checksum
sl@0
   780
}
sl@0
   781
do_test avtrans-7.14 {
sl@0
   782
  execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   783
} $checksum2
sl@0
   784
integrity_check avtrans-7.15
sl@0
   785
sl@0
   786
# Arrange for another process to begin modifying the database but abort
sl@0
   787
# and die in the middle of the modification.  Then have this process read
sl@0
   788
# the database.  This process should detect the journal file and roll it
sl@0
   789
# back.  Verify that this happens correctly.
sl@0
   790
#
sl@0
   791
set fd [open test.tcl w]
sl@0
   792
puts $fd {
sl@0
   793
  sqlite3 db test.db
sl@0
   794
  db eval {
sl@0
   795
    PRAGMA default_cache_size=20;
sl@0
   796
    BEGIN;
sl@0
   797
    CREATE TABLE t3 AS SELECT * FROM t2;
sl@0
   798
    DELETE FROM t2;
sl@0
   799
  }
sl@0
   800
  sqlite_abort
sl@0
   801
}
sl@0
   802
close $fd
sl@0
   803
do_test avtrans-8.1 {
sl@0
   804
  catch {exec [info nameofexec] test.tcl}
sl@0
   805
  execsql {SELECT md5sum(x,y,z) FROM t2}
sl@0
   806
} $checksum
sl@0
   807
do_test avtrans-8.2 {
sl@0
   808
  execsql {SELECT md5sum(type,name,tbl_name,rootpage,sql) FROM sqlite_master}
sl@0
   809
} $checksum2
sl@0
   810
integrity_check avtrans-8.3
sl@0
   811
sl@0
   812
# In the following sequence of tests, compute the MD5 sum of the content
sl@0
   813
# of a table, make lots of modifications to that table, then do a rollback.
sl@0
   814
# Verify that after the rollback, the MD5 checksum is unchanged.
sl@0
   815
#
sl@0
   816
do_test avtrans-9.1 {
sl@0
   817
  execsql {
sl@0
   818
    PRAGMA default_cache_size=10;
sl@0
   819
  }
sl@0
   820
  db close
sl@0
   821
  sqlite3 db test.db
sl@0
   822
  execsql {
sl@0
   823
    BEGIN;
sl@0
   824
    CREATE TABLE t3(x TEXT);
sl@0
   825
    INSERT INTO t3 VALUES(randstr(10,400));
sl@0
   826
    INSERT INTO t3 VALUES(randstr(10,400));
sl@0
   827
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   828
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   829
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   830
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   831
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   832
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   833
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   834
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   835
    INSERT INTO t3 SELECT randstr(10,400) FROM t3;
sl@0
   836
    COMMIT;
sl@0
   837
    SELECT count(*) FROM t3;
sl@0
   838
  }
sl@0
   839
} {1024}
sl@0
   840
sl@0
   841
# The following procedure computes a "signature" for table "t3".  If
sl@0
   842
# T3 changes in any way, the signature should change.  
sl@0
   843
#
sl@0
   844
# This is used to test ROLLBACK.  We gather a signature for t3, then
sl@0
   845
# make lots of changes to t3, then rollback and take another signature.
sl@0
   846
# The two signatures should be the same.
sl@0
   847
#
sl@0
   848
proc signature {} {
sl@0
   849
  return [db eval {SELECT count(*), md5sum(x) FROM t3}]
sl@0
   850
}
sl@0
   851
sl@0
   852
# Repeat the following group of tests 20 times for quick testing and
sl@0
   853
# 40 times for full testing.  Each iteration of the test makes table
sl@0
   854
# t3 a little larger, and thus takes a little longer, so doing 40 tests
sl@0
   855
# is more than 2.0 times slower than doing 20 tests.  Considerably more.
sl@0
   856
#
sl@0
   857
if {[info exists ISQUICK]} {
sl@0
   858
  set limit 20
sl@0
   859
} else {
sl@0
   860
  set limit 40
sl@0
   861
}
sl@0
   862
sl@0
   863
# Do rollbacks.  Make sure the signature does not change.
sl@0
   864
#
sl@0
   865
for {set i 2} {$i<=$limit} {incr i} {
sl@0
   866
  set ::sig [signature]
sl@0
   867
  set cnt [lindex $::sig 0]
sl@0
   868
  if {$i%2==0} {
sl@0
   869
    execsql {PRAGMA fullfsync=ON}
sl@0
   870
  } else {
sl@0
   871
    execsql {PRAGMA fullfsync=OFF}
sl@0
   872
  }
sl@0
   873
  set sqlite_sync_count 0
sl@0
   874
  set sqlite_fullsync_count 0
sl@0
   875
  do_test avtrans-9.$i.1-$cnt {
sl@0
   876
     execsql {
sl@0
   877
       BEGIN;
sl@0
   878
       DELETE FROM t3 WHERE random()%10!=0;
sl@0
   879
       INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
sl@0
   880
       INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
sl@0
   881
       ROLLBACK;
sl@0
   882
     }
sl@0
   883
     signature
sl@0
   884
  } $sig
sl@0
   885
  do_test avtrans-9.$i.2-$cnt {
sl@0
   886
     execsql {
sl@0
   887
       BEGIN;
sl@0
   888
       DELETE FROM t3 WHERE random()%10!=0;
sl@0
   889
       INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
sl@0
   890
       DELETE FROM t3 WHERE random()%10!=0;
sl@0
   891
       INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
sl@0
   892
       ROLLBACK;
sl@0
   893
     }
sl@0
   894
     signature
sl@0
   895
  } $sig
sl@0
   896
  if {$i<$limit} {
sl@0
   897
    do_test avtrans-9.$i.3-$cnt {
sl@0
   898
       execsql {
sl@0
   899
         INSERT INTO t3 SELECT randstr(10,400) FROM t3 WHERE random()%10==0;
sl@0
   900
       }
sl@0
   901
    } {}
sl@0
   902
    if {$tcl_platform(platform)=="unix"} {
sl@0
   903
      do_test avtrans-9.$i.4-$cnt {
sl@0
   904
         expr {$sqlite_sync_count>0}
sl@0
   905
      } 1
sl@0
   906
      ifcapable pager_pragmas {
sl@0
   907
        do_test avtrans-9.$i.5-$cnt {
sl@0
   908
           expr {$sqlite_fullsync_count>0}
sl@0
   909
        } [expr {$i%2==0}]
sl@0
   910
      } else {
sl@0
   911
        do_test avtrans-9.$i.5-$cnt {
sl@0
   912
           expr {$sqlite_fullsync_count==0}
sl@0
   913
        } {1}
sl@0
   914
      }
sl@0
   915
    }
sl@0
   916
  }
sl@0
   917
  set ::pager_old_format 0
sl@0
   918
}
sl@0
   919
integrity_check avtrans-10.1
sl@0
   920
   
sl@0
   921
finish_test