/ Check-in [e1d8ec85]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Code simplification: ALTER TABLE ADD COLUMN always upgrades the file_format to 4 if is not there already. No need to upgrade to only 2 or 3 since format 4 has now been supported for over 10 years.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e1d8ec85546caeca3bb7a05ad962a2aed91194ce
User & Date: drh 2016-02-09 16:09:22
References
2016-07-14
18:46 New ticket [f68bf685] ALTER TABLE corrupts DESC indexes. artifact: 2db52790 user: drh
Context
2016-02-09
17:12
Fix the top-level makefiles to always include SQLITE_ENABLE_EXPLAIN_COMMENTS when building the shell. Still need to fix main.mk and the amalgamation tarball. check-in: 99228239 user: drh tags: trunk
16:09
Code simplification: ALTER TABLE ADD COLUMN always upgrades the file_format to 4 if is not there already. No need to upgrade to only 2 or 3 since format 4 has now been supported for over 10 years. check-in: e1d8ec85 user: drh tags: trunk
02:12
Make sure every co-routines has its own set of temporary registers and does not share temporaries, since a co-routine might expect the content of a temporary register to be preserved across an OP_Yield. Proposed fix for ticket [d06a25c84454a]. check-in: ca72be86 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/alter.c.

   584    584   
   585    585   exit_rename_table:
   586    586     sqlite3SrcListDelete(db, pSrc);
   587    587     sqlite3DbFree(db, zName);
   588    588     db->flags = savedDbFlags;
   589    589   }
   590    590   
   591         -
   592         -/*
   593         -** Generate code to make sure the file format number is at least minFormat.
   594         -** The generated code will increase the file format number if necessary.
   595         -*/
   596         -void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
   597         -  Vdbe *v;
   598         -  v = sqlite3GetVdbe(pParse);
   599         -  /* The VDBE should have been allocated before this routine is called.
   600         -  ** If that allocation failed, we would have quit before reaching this
   601         -  ** point */
   602         -  if( ALWAYS(v) ){
   603         -    int r1 = sqlite3GetTempReg(pParse);
   604         -    int r2 = sqlite3GetTempReg(pParse);
   605         -    int addr1;
   606         -    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
   607         -    sqlite3VdbeUsesBtree(v, iDb);
   608         -    sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
   609         -    addr1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
   610         -    sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); VdbeCoverage(v);
   611         -    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, minFormat);
   612         -    sqlite3VdbeJumpHere(v, addr1);
   613         -    sqlite3ReleaseTempReg(pParse, r1);
   614         -    sqlite3ReleaseTempReg(pParse, r2);
   615         -  }
   616         -}
   617         -
   618    591   /*
   619    592   ** This function is called after an "ALTER TABLE ... ADD" statement
   620    593   ** has been parsed. Argument pColDef contains the text of the new
   621    594   ** column definition.
   622    595   **
   623    596   ** The Table structure pParse->pNewTable was extended to include
   624    597   ** the new column during parsing.
................................................................................
   629    602     int iDb;                  /* Database number */
   630    603     const char *zDb;          /* Database name */
   631    604     const char *zTab;         /* Table name */
   632    605     char *zCol;               /* Null-terminated column definition */
   633    606     Column *pCol;             /* The new column */
   634    607     Expr *pDflt;              /* Default value for the new column */
   635    608     sqlite3 *db;              /* The database connection; */
          609  +  Vdbe *v = pParse->pVdbe;  /* The prepared statement under construction */
   636    610   
   637    611     db = pParse->db;
   638    612     if( pParse->nErr || db->mallocFailed ) return;
          613  +  assert( v!=0 );
   639    614     pNew = pParse->pNewTable;
   640    615     assert( pNew );
   641    616   
   642    617     assert( sqlite3BtreeHoldsAllMutexes(db) );
   643    618     iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
   644    619     zDb = db->aDb[iDb].zName;
   645    620     zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
................................................................................
   721    696         zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
   722    697         zTab
   723    698       );
   724    699       sqlite3DbFree(db, zCol);
   725    700       db->flags = savedDbFlags;
   726    701     }
   727    702   
   728         -  /* If the default value of the new column is NULL, then set the file
          703  +  /* If the default value of the new column is NULL, then the file
   729    704     ** format to 2. If the default value of the new column is not NULL,
   730         -  ** the file format becomes 3.
          705  +  ** the file format be 3.  Back when this feature was first added
          706  +  ** in 2006, we went to the trouble to upgrade the file format to the
          707  +  ** minimum support values.  But 10-years on, we can assume that all
          708  +  ** extent versions of SQLite support file-format 4, so we always and
          709  +  ** unconditionally upgrade to 4.
   731    710     */
   732         -  sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
          711  +  sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, 
          712  +                    SQLITE_MAX_FILE_FORMAT);
   733    713   
   734    714     /* Reload the schema of the modified table. */
   735    715     reloadTableSchema(pParse, pTab, pTab->zName);
   736    716   }
   737    717   
   738    718   /*
   739    719   ** This function is called by the parser after the table-name in

Changes to src/sqliteInt.h.

  3760   3760   int sqlite3FindDb(sqlite3*, Token*);
  3761   3761   int sqlite3FindDbName(sqlite3 *, const char *);
  3762   3762   int sqlite3AnalysisLoad(sqlite3*,int iDB);
  3763   3763   void sqlite3DeleteIndexSamples(sqlite3*,Index*);
  3764   3764   void sqlite3DefaultRowEst(Index*);
  3765   3765   void sqlite3RegisterLikeFunctions(sqlite3*, int);
  3766   3766   int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
  3767         -void sqlite3MinimumFileFormat(Parse*, int, int);
  3768   3767   void sqlite3SchemaClear(void *);
  3769   3768   Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
  3770   3769   int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
  3771   3770   KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
  3772   3771   void sqlite3KeyInfoUnref(KeyInfo*);
  3773   3772   KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
  3774   3773   KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);

Changes to test/alter3.test.

   180    180       ALTER TABLE t1 ADD c;
   181    181       SELECT * FROM t1;
   182    182     }
   183    183   } {1 100 {} 2 300 {}}
   184    184   if {!$has_codec} {
   185    185     do_test alter3-3.3 {
   186    186       get_file_format
   187         -  } {3}
          187  +  } {4}
   188    188   }
   189    189   ifcapable schema_version {
   190    190     do_test alter3-3.4 {
   191    191       execsql {
   192    192         PRAGMA schema_version;
   193    193       }
   194    194     } {11}
................................................................................
   216    216       ALTER TABLE t1 ADD c DEFAULT 'hello world';
   217    217       SELECT * FROM t1;
   218    218     }
   219    219   } {1 100 {hello world} 2 300 {hello world}}
   220    220   if {!$has_codec} {
   221    221     do_test alter3-4.3 {
   222    222       get_file_format
   223         -  } {3}
          223  +  } {4}
   224    224   }
   225    225   ifcapable schema_version {
   226    226     do_test alter3-4.4 {
   227    227       execsql {
   228    228         PRAGMA schema_version;
   229    229       }
   230    230     } {21}
................................................................................
   266    266           PRAGMA aux.schema_version;
   267    267         }
   268    268       } {31}
   269    269     }
   270    270     if {!$has_codec} {
   271    271       do_test alter3-5.5 {
   272    272         list [get_file_format test2.db] [get_file_format]
   273         -    } {2 3}
          273  +    } {4 4}
   274    274     }
   275    275     do_test alter3-5.6 {
   276    276       execsql {
   277    277         ALTER TABLE aux.t1 ADD COLUMN d DEFAULT 1000;
   278    278         SELECT sql FROM aux.sqlite_master;
   279    279       }
   280    280     } {{CREATE TABLE t1(a,b, c VARCHAR(128), d DEFAULT 1000)}}
................................................................................
   343    343       } {1}
   344    344       do_test alter3-7.2 {
   345    345         execsql {
   346    346           CREATE TABLE abc(a, b, c);
   347    347           ALTER TABLE abc ADD d DEFAULT NULL;
   348    348         }
   349    349         get_file_format
   350         -    } {2}
          350  +    } {4}
   351    351       do_test alter3-7.3 {
   352    352         execsql {
   353    353           ALTER TABLE abc ADD e DEFAULT 10;
   354    354         }
   355    355         get_file_format
   356         -    } {3}
          356  +    } {4}
   357    357       do_test alter3-7.4 {
   358    358         execsql {
   359    359           ALTER TABLE abc ADD f DEFAULT NULL;
   360    360         }
   361    361         get_file_format
   362         -    } {3}
          362  +    } {4}
   363    363       do_test alter3-7.5 {
   364    364         execsql {
   365    365           VACUUM;
   366    366         }
   367    367         get_file_format
   368    368       } {1}
   369    369     }