1*4520Snw141292 2*4520Snw141292#pragma ident "%Z%%M% %I% %E% SMI" 3*4520Snw141292 4*4520Snw141292# 2002 July 17 5*4520Snw141292# 6*4520Snw141292# The author disclaims copyright to this source code. In place of 7*4520Snw141292# a legal notice, here is a blessing: 8*4520Snw141292# 9*4520Snw141292# May you do good and not evil. 10*4520Snw141292# May you find forgiveness for yourself and forgive others. 11*4520Snw141292# May you share freely, never taking more than you give. 12*4520Snw141292# 13*4520Snw141292#*********************************************************************** 14*4520Snw141292# This file implements regression tests for SQLite library. The 15*4520Snw141292# focus of this file is testing the ability of the library to detect 16*4520Snw141292# past or future file format version numbers and respond appropriately. 17*4520Snw141292# 18*4520Snw141292# $Id: version.test,v 1.9 2004/02/12 19:01:05 drh Exp $ 19*4520Snw141292 20*4520Snw141292set testdir [file dirname $argv0] 21*4520Snw141292source $testdir/tester.tcl 22*4520Snw141292 23*4520Snw141292# Current file format version 24*4520Snw141292set VX 4 25*4520Snw141292 26*4520Snw141292# Create a new database 27*4520Snw141292# 28*4520Snw141292do_test version-1.1 { 29*4520Snw141292 execsql { 30*4520Snw141292 CREATE TABLE t1(x); 31*4520Snw141292 INSERT INTO t1 VALUES(1); 32*4520Snw141292 INSERT INTO t1 SELECT x+1 FROM t1; 33*4520Snw141292 INSERT INTO t1 SELECT x+2 FROM t1; 34*4520Snw141292 INSERT INTO t1 SELECT x+4 FROM t1; 35*4520Snw141292 SELECT * FROM t1; 36*4520Snw141292 } 37*4520Snw141292} {1 2 3 4 5 6 7 8} 38*4520Snw141292 39*4520Snw141292# Make sure the version number is set correctly 40*4520Snw141292# 41*4520Snw141292do_test version-1.2 { 42*4520Snw141292 db close 43*4520Snw141292 set ::bt [btree_open test.db] 44*4520Snw141292 btree_begin_transaction $::bt 45*4520Snw141292 set ::meta [btree_get_meta $::bt] 46*4520Snw141292 btree_rollback $::bt 47*4520Snw141292 lindex $::meta 2 48*4520Snw141292} $VX 49*4520Snw141292 50*4520Snw141292# Increase the file_format number by one. Verify that the 51*4520Snw141292# file will refuse to open. 52*4520Snw141292# 53*4520Snw141292do_test version-1.3 { 54*4520Snw141292 set m2 [lreplace $::meta 2 2 [expr {$::VX+1}]] 55*4520Snw141292 btree_begin_transaction $::bt 56*4520Snw141292 eval btree_update_meta $::bt $m2 57*4520Snw141292 btree_commit $::bt 58*4520Snw141292 set rc [catch {sqlite db test.db} msg] 59*4520Snw141292 lappend rc $msg 60*4520Snw141292} {1 {unsupported file format}} 61*4520Snw141292 62*4520Snw141292# Decrease the file_format number by one. Verify that the 63*4520Snw141292# file will open correctly. 64*4520Snw141292# 65*4520Snw141292do_test version-1.4 { 66*4520Snw141292 set m2 [lreplace $::meta 2 2 [expr {$::VX-1}]] 67*4520Snw141292 btree_begin_transaction $::bt 68*4520Snw141292 eval btree_update_meta $::bt $m2 69*4520Snw141292 btree_commit $::bt 70*4520Snw141292 sqlite db test.db 71*4520Snw141292 execsql { 72*4520Snw141292 SELECT * FROM t1; 73*4520Snw141292 } 74*4520Snw141292} {1 2 3 4 5 6 7 8} 75*4520Snw141292 76*4520Snw141292# Set the file_format number to 2. This should cause the automatic 77*4520Snw141292# upgrade processing to run. 78*4520Snw141292# 79*4520Snw141292do_test version-1.5 { 80*4520Snw141292 set m2 [lreplace $::meta 2 2 2] 81*4520Snw141292 btree_begin_transaction $::bt 82*4520Snw141292 eval btree_update_meta $::bt $m2 83*4520Snw141292 btree_commit $::bt 84*4520Snw141292 sqlite db test.db 85*4520Snw141292 execsql { 86*4520Snw141292 SELECT * FROM t1; 87*4520Snw141292 } 88*4520Snw141292} {1 2 3 4 5 6 7 8} 89*4520Snw141292do_test version-1.6 { 90*4520Snw141292 set ::meta [btree_get_meta $::bt] 91*4520Snw141292 lindex $::meta 2 92*4520Snw141292} $VX 93*4520Snw141292 94*4520Snw141292# Add some triggers, views, and indices to the schema and make sure the 95*4520Snw141292# automatic upgrade still works. 96*4520Snw141292# 97*4520Snw141292do_test version-1.7 { 98*4520Snw141292 execsql { 99*4520Snw141292 CREATE INDEX i1 ON t1(x); 100*4520Snw141292 DELETE FROM t1; 101*4520Snw141292 CREATE TABLE t2(a INTEGER PRIMARY KEY, b UNIQUE, c); 102*4520Snw141292 CREATE TABLE cnt(name,ins, del); 103*4520Snw141292 INSERT INTO cnt VALUES('t1',0,0); 104*4520Snw141292 INSERT INTO cnt VALUES('t2',0,0); 105*4520Snw141292 CREATE TRIGGER r1 AFTER INSERT ON t1 FOR EACH ROW BEGIN 106*4520Snw141292 UPDATE cnt SET ins=ins+1 WHERE name='t1'; 107*4520Snw141292 END; 108*4520Snw141292 CREATE TRIGGER r2 AFTER DELETE ON t1 FOR EACH ROW BEGIN 109*4520Snw141292 UPDATE cnt SET del=del+1 WHERE name='t1'; 110*4520Snw141292 END; 111*4520Snw141292 CREATE TRIGGER r3 AFTER INSERT ON t2 FOR EACH ROW BEGIN 112*4520Snw141292 UPDATE cnt SET ins=ins+1 WHERE name='t2'; 113*4520Snw141292 END; 114*4520Snw141292 CREATE TRIGGER r4 AFTER DELETE ON t2 FOR EACH ROW BEGIN 115*4520Snw141292 UPDATE cnt SET del=del+1 WHERE name='t2'; 116*4520Snw141292 END; 117*4520Snw141292 CREATE VIEW v1 AS SELECT x+100 FROM t1; 118*4520Snw141292 CREATE VIEW v2 AS SELECT sum(ins), sum(del) FROM cnt; 119*4520Snw141292 INSERT INTO t1 VALUES(1); 120*4520Snw141292 INSERT INTO t1 SELECT x+1 FROM t1; 121*4520Snw141292 INSERT INTO t1 SELECT x+2 FROM t1; 122*4520Snw141292 INSERT INTO t1 SELECT x+4 FROM t1; 123*4520Snw141292 SELECT * FROM t1; 124*4520Snw141292 } 125*4520Snw141292} {1 2 3 4 5 6 7 8} 126*4520Snw141292do_test version-1.8 { 127*4520Snw141292 execsql { 128*4520Snw141292 SELECT * FROM v2; 129*4520Snw141292 } 130*4520Snw141292} {8 0} 131*4520Snw141292do_test version-1.9 { 132*4520Snw141292 execsql { 133*4520Snw141292 SELECT * FROM cnt; 134*4520Snw141292 } 135*4520Snw141292} {t1 8 0 t2 0 0} 136*4520Snw141292do_test version-1.10 { 137*4520Snw141292 execsql { 138*4520Snw141292 INSERT INTO t2 SELECT x*3, x*2, x FROM t1; 139*4520Snw141292 SELECT * FROM t2; 140*4520Snw141292 } 141*4520Snw141292} {3 2 1 6 4 2 9 6 3 12 8 4 15 10 5 18 12 6 21 14 7 24 16 8} 142*4520Snw141292do_test version-1.11 { 143*4520Snw141292 execsql { 144*4520Snw141292 SELECT * FROM cnt; 145*4520Snw141292 } 146*4520Snw141292} {t1 8 0 t2 8 0} 147*4520Snw141292 148*4520Snw141292# Here we do the upgrade test. 149*4520Snw141292# 150*4520Snw141292do_test version-1.12 { 151*4520Snw141292 db close 152*4520Snw141292 set m2 [lreplace $::meta 2 2 2] 153*4520Snw141292 btree_begin_transaction $::bt 154*4520Snw141292 eval btree_update_meta $::bt $m2 155*4520Snw141292 btree_commit $::bt 156*4520Snw141292 sqlite db test.db 157*4520Snw141292 execsql { 158*4520Snw141292 SELECT * FROM cnt; 159*4520Snw141292 } 160*4520Snw141292} {t1 8 0 t2 8 0} 161*4520Snw141292do_test version-1.13 { 162*4520Snw141292 execsql { 163*4520Snw141292 SELECT * FROM v1; 164*4520Snw141292 } 165*4520Snw141292} {101 102 103 104 105 106 107 108} 166*4520Snw141292do_test version-1.14 { 167*4520Snw141292 execsql { 168*4520Snw141292 SELECT * FROM v2; 169*4520Snw141292 } 170*4520Snw141292} {16 0} 171*4520Snw141292 172*4520Snw141292# Try to do an upgrade where the database file is read-only 173*4520Snw141292# 174*4520Snw141292do_test version-2.1 { 175*4520Snw141292 db close 176*4520Snw141292 set m2 [lreplace $::meta 2 2 2] 177*4520Snw141292 btree_begin_transaction $::bt 178*4520Snw141292 eval btree_update_meta $::bt $m2 179*4520Snw141292 btree_commit $::bt 180*4520Snw141292 btree_close $::bt 181*4520Snw141292 catch {file attributes test.db -permissions 0444} 182*4520Snw141292 catch {file attributes test.db -readonly 1} 183*4520Snw141292 if {[file writable test.db]} { 184*4520Snw141292 error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user" 185*4520Snw141292 } 186*4520Snw141292 set rc [catch {sqlite db test.db} msg] 187*4520Snw141292 lappend rc $msg 188*4520Snw141292} {1 {unable to upgrade database to the version 2.6 format: attempt to write a readonly database}} 189*4520Snw141292do_test version-2.2 { 190*4520Snw141292 file delete -force test.db 191*4520Snw141292 set fd [open test.db w] 192*4520Snw141292 set txt "This is not a valid database file\n" 193*4520Snw141292 while {[string length $txt]<4092} {append txt $txt} 194*4520Snw141292 puts $fd $txt 195*4520Snw141292 close $fd 196*4520Snw141292 set rc [catch {sqlite db test.db} msg] 197*4520Snw141292 lappend rc $msg 198*4520Snw141292} {1 {file is encrypted or is not a database}} 199*4520Snw141292 200*4520Snw141292 201*4520Snw141292finish_test 202