]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Fix ALTER TABLE so that it does not promote the schema version past 3, as
authordrh <drh@noemail.net>
Thu, 14 Jul 2016 19:13:11 +0000 (19:13 +0000)
committerdrh <drh@noemail.net>
Thu, 14 Jul 2016 19:13:11 +0000 (19:13 +0000)
that will cause DESC indexes to go corrupt.
Ticket [f68bf68513a1c].

FossilOrigin-Name: a7db6e45ad45be9b3003f61d4163f543498a7c9d

manifest
manifest.uuid
src/alter.c
test/alter3.test
test/alter4.test

index c9585f19cd260a69785ea8905d36545429d87a6b..966b26fc0eaf6121ac36acd45168400d15ffc9c4 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sharmless\scompiler\swarnings\sin\sshell.c\sfor\sNetBSD.
-D 2016-07-13T13:05:13.449
+C Fix\sALTER\sTABLE\sso\sthat\sit\sdoes\snot\spromote\sthe\sschema\sversion\spast\s3,\sas\nthat\swill\scause\sDESC\sindexes\sto\sgo\scorrupt.\nTicket\s[f68bf68513a1c].
+D 2016-07-14T19:13:11.812
 F Makefile.in 6c20d44f72d4564f11652b26291a214c8367e5db
 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
 F Makefile.msc d66d0395c38571aab3804f8db0fa20707ae4609a
@@ -320,7 +320,7 @@ F spec.template 86a4a43b99ebb3e75e6b9a735d5fd293a24e90ca
 F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b
 F sqlite3.1 fc7ad8990fc8409983309bb80de8c811a7506786
 F sqlite3.pc.in 48fed132e7cb71ab676105d2a4dc77127d8c1f3a
-F src/alter.c 1bb0709b3048e24217b80ec6bd78a3e99a47c01b
+F src/alter.c cc28ab933ae615b22add0d609794ffb6596b42ea
 F src/analyze.c 37fedc80ac966ce1745811746e68e4d8fa64c7fe
 F src/attach.c 771153bd1f4ab0b97a44a13dde2c7e5e1efeba22
 F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240
@@ -475,8 +475,8 @@ F test/alias.test 4529fbc152f190268a15f9384a5651bbbabc9d87
 F test/all.test 6ff7b43c2b4b905c74dc4a813d201d0fa64c5783
 F test/alter.test 2facdddf08d0d48e75dc6cc312cd2b030f4835dd
 F test/alter2.test 7ea05c7d92ac99349a802ef7ada17294dd647060
-F test/alter3.test b3568d11c38c4599c92f24242eda34144d78dc10
-F test/alter4.test c461150723ac957f3b2214aa0b11552cd72023ec
+F test/alter3.test 4d79934d812eaeacc6f22781a080f8cfe012fdc3
+F test/alter4.test 0c33c542247ba5aee4f4a0133ac44bcf8f97e5e0
 F test/altermalloc.test e81ac9657ed25c6c5bb09bebfa5a047cd8e4acfc
 F test/amatch1.test b5ae7065f042b7f4c1c922933f4700add50cdb9f
 F test/analyze.test 3eb35a4af972f98422e5dc0586501b17d103d321
@@ -1505,7 +1505,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 021d0fb8d85e44839d2b4fdb90b15f0e1f2442e6
-R f2c7097376b33c065f9732879605d098
+P 824b39e54fb9ba562be4d92cc9a54aee1cdf84cb
+R 7d842c7254944c448483c1d7ddff9ad6
 U drh
-Z e160e6d632f23e86ebc551e9950cc211
+Z f6a6c237e43006b812e6af2a3e69c586
index 6b7a74bfc9c2b34f825b3a62911b53da9c1f46d2..1fc14b81a9c2d53d0eb18581dd027dcd114ba916 100644 (file)
@@ -1 +1 @@
-824b39e54fb9ba562be4d92cc9a54aee1cdf84cb
\ No newline at end of file
+a7db6e45ad45be9b3003f61d4163f543498a7c9d
\ No newline at end of file
index 642c1fb6777c81707995ec38ed757829863d566b..806ed49172a84095ae4bebef5b4e14fe8f5ceba8 100644 (file)
@@ -601,6 +601,7 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
   Expr *pDflt;              /* Default value for the new column */
   sqlite3 *db;              /* The database connection; */
   Vdbe *v = pParse->pVdbe;  /* The prepared statement under construction */
+  int r1;                   /* Temporary registers */
 
   db = pParse->db;
   if( pParse->nErr || db->mallocFailed ) return;
@@ -695,16 +696,18 @@ void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
     db->flags = savedDbFlags;
   }
 
-  /* If the default value of the new column is NULL, then the file
-  ** format to 2. If the default value of the new column is not NULL,
-  ** the file format be 3.  Back when this feature was first added
-  ** in 2006, we went to the trouble to upgrade the file format to the
-  ** minimum support values.  But 10-years on, we can assume that all
-  ** extent versions of SQLite support file-format 4, so we always and
-  ** unconditionally upgrade to 4.
+  /* Make sure the schema version is at least 3.  But do not upgrade
+  ** from less than 3 to 4, as that will corrupt any preexisting DESC
+  ** index.
   */
-  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 
-                    SQLITE_MAX_FILE_FORMAT);
+  r1 = sqlite3GetTempReg(pParse);
+  sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
+  sqlite3VdbeUsesBtree(v, iDb);
+  sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
+  sqlite3VdbeAddOp2(v, OP_IfPos, r1, sqlite3VdbeCurrentAddr(v)+2);
+  VdbeCoverage(v);
+  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 3);
+  sqlite3ReleaseTempReg(pParse, r1);
 
   /* Reload the schema of the modified table. */
   reloadTableSchema(pParse, pTab, pTab->zName);
index f8ebe056f202da50db52ec34b323eb875916db6c..44b31c983391cde71d2aa909a384fa08370fdf7a 100644 (file)
@@ -184,7 +184,7 @@ do_test alter3-3.2 {
 if {!$has_codec} {
   do_test alter3-3.3 {
     get_file_format
-  } {4}
+  } {3}
 }
 ifcapable schema_version {
   do_test alter3-3.4 {
@@ -220,7 +220,7 @@ do_test alter3-4.2 {
 if {!$has_codec} {
   do_test alter3-4.3 {
     get_file_format
-  } {4}
+  } {3}
 }
 ifcapable schema_version {
   do_test alter3-4.4 {
@@ -270,7 +270,7 @@ ifcapable attach {
   if {!$has_codec} {
     do_test alter3-5.5 {
       list [get_file_format test2.db] [get_file_format]
-    } {4 4}
+    } {3 3}
   }
   do_test alter3-5.6 {
     execsql {
@@ -347,19 +347,19 @@ if {!$has_codec} {
         ALTER TABLE abc ADD d DEFAULT NULL;
       }
       get_file_format
-    } {4}
+    } {3}
     do_test alter3-7.3 {
       execsql {
         ALTER TABLE abc ADD e DEFAULT 10;
       }
       get_file_format
-    } {4}
+    } {3}
     do_test alter3-7.4 {
       execsql {
         ALTER TABLE abc ADD f DEFAULT NULL;
       }
       get_file_format
-    } {4}
+    } {3}
     do_test alter3-7.5 {
       execsql {
         VACUUM;
index ac39d614a540b16fe3bbf2c98bdab6f4c4410117..2cd78777a25fc57e33770c32ee384a73351f0027 100644 (file)
@@ -355,4 +355,23 @@ do_execsql_test alter4-9.3 {
   SELECT typeof(c), c FROM t5;
 } {real 9.22337203685478e+18}
 
+# Confirm that doing an ALTER TABLE on a legacy format database
+# does not corrupt DESC indexes.
+#
+# Ticket https://www.sqlite.org/src/tktview/f68bf68513a1c
+#
+do_test alter4-10.1 {
+  db close
+  sqlite3 db :memory:
+  db eval {
+    PRAGMA legacy_file_format=on;
+    CREATE TABLE t1(a,b,c);
+    CREATE INDEX t1a ON t1(a DESC);
+    INSERT INTO t1 VALUES(1,2,3);
+    INSERT INTO t1 VALUES(2,3,4);
+    ALTER TABLE t1 ADD COLUMN d;
+    PRAGMA integrity_check;
+  }
+} {ok}
+
 finish_test