From 8e5ba84643c14446c4d14bb878cebdde9babbef6 Mon Sep 17 00:00:00 2001 From: drh Date: Thu, 18 Jul 2002 01:27:17 +0000 Subject: [PATCH] Make the automatic database upgrade work even if there are triggers. Add tests for automatic upgrade and for failing if reading a more advanced version of the database. Ticket #107. (CVS 682) FossilOrigin-Name: 0493e39c1cbbe4a38e990a2370181a79606af222 --- manifest | 13 ++-- manifest.uuid | 2 +- src/main.c | 12 +++- test/version.test | 169 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 187 insertions(+), 9 deletions(-) create mode 100644 test/version.test diff --git a/manifest b/manifest index 65e3a042ee..f37095bb77 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sfor\sticket\s#107:\sFix\sa\sdesign\sdefect\sin\sindices\sthat\swas\scausing\squeries\nto\sfail\swhen\susing\san\sindex\son\sa\scolumn\scontaining\san\sempty\sstring.\s\sThis\nfix\sis\san\sincompatible\sfile-format\schange.\s(CVS\s681) -D 2002-07-18T00:34:10 +C Make\sthe\sautomatic\sdatabase\supgrade\swork\seven\sif\sthere\sare\striggers.\s\sAdd\ntests\sfor\sautomatic\supgrade\sand\sfor\sfailing\sif\sreading\sa\smore\sadvanced\sversion\nof\sthe\sdatabase.\s\sTicket\s#107.\s(CVS\s682) +D 2002-07-18T01:27:18 F Makefile.in 6291a33b87d2a395aafd7646ee1ed562c6f2c28c F Makefile.template 4e11752e0b5c7a043ca50af4296ec562857ba495 F README a4c0ba11354ef6ba0776b400d057c59da47a4cc0 @@ -28,7 +28,7 @@ F src/func.c e45cd908b9b723d9b91473d09e12c23f786b3fc2 F src/hash.c 6a6236b89c8c060c65dabd300a1c8ce7c10edb72 F src/hash.h cd0433998bc1a3759d244e1637fe5a3c13b53bf8 F src/insert.c 9bc794863ea2988a7b8667ef010b3c46b26dba38 -F src/main.c dbe691d2b6e0b2b0e0e87ca42c04506ead1c0550 +F src/main.c e22729b9780f2f3a2c69715f53d67666072a63d5 F src/md5.c 0ae1f3e2cac92d06fc6246d1b4b8f61a2fe66d3b F src/os.c edb22daad525f49681f41c76683a16c1d39755c7 F src/os.h 5b9a69875c880d1665ae040cbca1f7b9c82198ab @@ -107,6 +107,7 @@ F test/trigger3.test 7dfe798d7e72c13720394685fe353112e3f31adf F test/unique.test 572aa791327c1e8d797932263e9d67f176cfdb44 F test/update.test 7ffb062d580a972e7870d0f51d5af3ab9bfeae08 F test/vacuum.test 059871b312eb910bbe49dafde1d01490cc2c6bbe +F test/version.test c970e4196ac995d22ce51a9cb1f0285f8c950deb F test/view.test 3afca084dab44c7a5772d3c6a326adf93ad52050 F test/where.test 1f87bec674bf85d74ac1cc5b2cd3d89be1e87b1d F tool/diffdb.c 7524b1b5df217c20cd0431f6789851a4e0cb191b @@ -141,7 +142,7 @@ F www/speed.tcl da8afcc1d3ccc5696cfb388a68982bc3d9f7f00f F www/sqlite.tcl ae3dcfb077e53833b59d4fcc94d8a12c50a44098 F www/tclsqlite.tcl 1db15abeb446aad0caf0b95b8b9579720e4ea331 F www/vdbe.tcl 2013852c27a02a091d39a766bc87cff329f21218 -P bbca16f88d00cd33ac7229edf3ee4623eff6e62f -R 3b401eb81b101736a092f34a44910d1f +P 20d152fcddb4fa53556a9c93c7a869600a7c5183 +R 234db4e0b851adaa70ea9179904f5e16 U drh -Z 3a1e3e849570ce46d4051f5d0915780a +Z 8a3f7ec18696f6fb7843f936645bc3ff diff --git a/manifest.uuid b/manifest.uuid index 7de7eb6145..549a0a047b 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -20d152fcddb4fa53556a9c93c7a869600a7c5183 \ No newline at end of file +0493e39c1cbbe4a38e990a2370181a79606af222 \ No newline at end of file diff --git a/src/main.c b/src/main.c index 6db127f83f..5012dafc76 100644 --- a/src/main.c +++ b/src/main.c @@ -14,7 +14,7 @@ ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** -** $Id: main.c,v 1.87 2002/07/18 00:34:12 drh Exp $ +** $Id: main.c,v 1.88 2002/07/18 01:27:18 drh Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -102,13 +102,21 @@ static int upgrade_3_callback(void *pDb, int argc, char **argv, char **NotUsed){ sqlite *db = (sqlite*)pDb; int rc; + Table *pTab; + Trigger *pTrig; + pTab = sqliteFindTable(db, argv[0]); + if( pTab ){ + pTrig = pTab->pTrigger; + pTab->pTrigger = 0; /* Disable all triggers before rebuilding the table */ + } rc = sqlite_exec_printf(db, "CREATE TEMP TABLE sqlite_x AS SELECT * FROM '%q'; " "DELETE FROM '%q'; " "INSERT INTO '%q' SELECT * FROM sqlite_x; " "DROP TABLE sqlite_x;", 0, 0, 0, argv[0], argv[0], argv[0]); + if( pTab ) pTab->pTrigger = pTrig; /* Re-enable triggers */ return rc!=SQLITE_OK; } @@ -241,7 +249,7 @@ int sqliteInit(sqlite *db, char **pzErrMsg){ }else if( db->file_format>3 ){ sqliteBtreeCloseCursor(curMain); sqliteSetString(pzErrMsg, "unsupported file format", 0); - rc = SQLITE_ERROR; + return SQLITE_ERROR; } /* Read the schema information out of the schema tables diff --git a/test/version.test b/test/version.test new file mode 100644 index 0000000000..f742f87f8b --- /dev/null +++ b/test/version.test @@ -0,0 +1,169 @@ +# 2002 July 17 +# +# 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 testing the ability of the library to detect +# past or future file format version numbers and respond appropriately. +# +# $Id: version.test,v 1.1 2002/07/18 01:27:19 drh Exp $ + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Current file format version +set VX 3 + +# Create a new database +# +do_test version-1.1 { + execsql { + CREATE TABLE t1(x); + INSERT INTO t1 VALUES(1); + INSERT INTO t1 SELECT x+1 FROM t1; + INSERT INTO t1 SELECT x+2 FROM t1; + INSERT INTO t1 SELECT x+4 FROM t1; + SELECT * FROM t1; + } +} {1 2 3 4 5 6 7 8} + +# Make sure the version number is set correctly +# +do_test version-1.2 { + db close + set ::bt [btree_open test.db] + set ::meta [btree_get_meta $::bt] + lindex $::meta 2 +} $VX + +# Increase the file_format number by one. Verify that the +# file will refuse to open. +# +do_test version-1.3 { + set m2 [lreplace $::meta 2 2 [expr {$::VX+1}]] + btree_begin_transaction $::bt + eval btree_update_meta $::bt $m2 + btree_commit $::bt + set rc [catch {sqlite db test.db} msg] + lappend rc $msg +} {1 {unsupported file format}} + +# Decrease the file_format number by one. Verify that the +# file will open correctly. +# +do_test version-1.4 { + set m2 [lreplace $::meta 2 2 [expr {$::VX-1}]] + btree_begin_transaction $::bt + eval btree_update_meta $::bt $m2 + btree_commit $::bt + sqlite db test.db + execsql { + SELECT * FROM t1; + } +} {1 2 3 4 5 6 7 8} + +# Set the file_format number to 2. This should cause the automatic +# upgrade processing to run. +# +do_test version-1.5 { + set m2 [lreplace $::meta 2 2 2] + btree_begin_transaction $::bt + eval btree_update_meta $::bt $m2 + btree_commit $::bt + sqlite db test.db + execsql { + SELECT * FROM t1; + } +} {1 2 3 4 5 6 7 8} +do_test version-1.6 { + set ::meta [btree_get_meta $::bt] + lindex $::meta 2 +} $VX + +# Add some triggers, views, and indices to the schema and make sure the +# automatic upgrade still works. +# +do_test version-1.7 { + execsql { + CREATE INDEX i1 ON t1(x); + DELETE FROM t1; + CREATE TABLE t2(a INTEGER PRIMARY KEY, b UNIQUE, c); + CREATE TABLE cnt(name,ins, del); + INSERT INTO cnt VALUES('t1',0,0); + INSERT INTO cnt VALUES('t2',0,0); + CREATE TRIGGER r1 AFTER INSERT ON t1 FOR EACH ROW BEGIN + UPDATE cnt SET ins=ins+1 WHERE name='t1'; + END; + CREATE TRIGGER r2 AFTER DELETE ON t1 FOR EACH ROW BEGIN + UPDATE cnt SET del=del+1 WHERE name='t1'; + END; + CREATE TRIGGER r3 AFTER INSERT ON t2 FOR EACH ROW BEGIN + UPDATE cnt SET ins=ins+1 WHERE name='t2'; + END; + CREATE TRIGGER r4 AFTER DELETE ON t2 FOR EACH ROW BEGIN + UPDATE cnt SET del=del+1 WHERE name='t2'; + END; + CREATE VIEW v1 AS SELECT x+100 FROM t1; + CREATE VIEW v2 AS SELECT sum(ins), sum(del) FROM cnt; + INSERT INTO t1 VALUES(1); + INSERT INTO t1 SELECT x+1 FROM t1; + INSERT INTO t1 SELECT x+2 FROM t1; + INSERT INTO t1 SELECT x+4 FROM t1; + SELECT * FROM t1; + } +} {1 2 3 4 5 6 7 8} +do_test version-1.8 { + execsql { + SELECT * FROM v2; + } +} {8 0} +do_test version-1.9 { + execsql { + SELECT * FROM cnt; + } +} {t1 8 0 t2 0 0} +do_test version-1.10 { + execsql { + INSERT INTO t2 SELECT x*3, x*2, x FROM t1; + SELECT * FROM t2; + } +} {3 2 1 6 4 2 9 6 3 12 8 4 15 10 5 18 12 6 21 14 7 24 16 8} +do_test version-1.11 { + execsql { + SELECT * FROM cnt; + } +} {t1 8 0 t2 8 0} + +# Here we do the upgrade test. +# +do_test version-1.12 { + db close + set m2 [lreplace $::meta 2 2 2] + btree_begin_transaction $::bt + eval btree_update_meta $::bt $m2 + btree_commit $::bt + sqlite db test.db + execsql { + SELECT * FROM cnt; + } +} {t1 8 0 t2 8 0} +do_test version-1.13 { + execsql { + SELECT * FROM v1; + } +} {101 102 103 104 105 106 107 108} +do_test version-1.14 { + execsql { + SELECT * FROM v2; + } +} {16 0} + + + +finish_test