/ Check-in [f4497b01]
Login

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

Overview
Comment:Reload the entire schema after renaming a column in order to ensure that the schema for any tables for which parent key definitions were changed are reloaded.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | alter-table-rename-column
Files: files | file ages | folders
SHA3-256:f4497b0136e9fcb1121a834c86f84eeaf8f1e7d3157d409484e086bcef3487f1
User & Date: dan 2018-08-11 13:40:20
Context
2018-08-11
17:34
Fix a bug causing all ALTER TABLE RENAME COLUMN commands to fail if ANALYZE had been run on the database. Also prevent the user from renaming the columns of system tables. check-in: ca644a28 user: dan tags: alter-table-rename-column
13:40
Reload the entire schema after renaming a column in order to ensure that the schema for any tables for which parent key definitions were changed are reloaded. check-in: f4497b01 user: dan tags: alter-table-rename-column
2018-08-10
20:19
Ensure the schema cookie is changed when a column is renamed. check-in: 2dec9ea4 user: dan tags: alter-table-rename-column
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/alter.c.

   827    827     sqlite3NestedParse(pParse, 
   828    828         "UPDATE \"%w\".%s SET "
   829    829         "sql = sqlite_rename_column(sql, %d, %Q, %Q, %Q) "
   830    830         "WHERE type = 'table' OR (type='index' AND tbl_name = %Q AND sql!='')",
   831    831         zDb, MASTER_NAME, iCol, zNew, pTab->zName, zOld, pTab->zName
   832    832     );
   833    833   
   834         -  /* Drop and reload the internal table schema. */
   835         -  sqlite3ChangeCookie(pParse, iSchema);
   836         -  reloadTableSchema(pParse, pTab, pTab->zName);
          834  +  /* Drop and reload the database schema. */
          835  +  if( db->mallocFailed==0 ){
          836  +    assert( pParse->pVdbe );
          837  +    sqlite3ChangeCookie(pParse, iSchema);
          838  +    sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iSchema, 0);
          839  +  }
   837    840   
   838    841    exit_rename_column:
   839    842     sqlite3SrcListDelete(db, pSrc);
   840    843     sqlite3DbFree(db, zOld);
   841    844     sqlite3DbFree(db, zNew);
   842    845     return;
   843    846   }

Changes to src/prepare.c.

   128    128   ** Attempt to read the database schema and initialize internal
   129    129   ** data structures for a single database file.  The index of the
   130    130   ** database file is given by iDb.  iDb==0 is used for the main
   131    131   ** database.  iDb==1 should never be used.  iDb>=2 is used for
   132    132   ** auxiliary databases.  Return one of the SQLITE_ error codes to
   133    133   ** indicate success or failure.
   134    134   */
   135         -static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
          135  +int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
   136    136     int rc;
   137    137     int i;
   138    138   #ifndef SQLITE_OMIT_DEPRECATED
   139    139     int size;
   140    140   #endif
   141    141     Db *pDb;
   142    142     char const *azArg[4];

Changes to src/sqliteInt.h.

  3795   3795   void sqlite3ExprListSetSortOrder(ExprList*,int);
  3796   3796   void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
  3797   3797   void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
  3798   3798   void sqlite3ExprListDelete(sqlite3*, ExprList*);
  3799   3799   u32 sqlite3ExprListFlags(const ExprList*);
  3800   3800   int sqlite3Init(sqlite3*, char**);
  3801   3801   int sqlite3InitCallback(void*, int, char**, char**);
         3802  +int sqlite3InitOne(sqlite3*, int, char**);
  3802   3803   void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
  3803   3804   #ifndef SQLITE_OMIT_VIRTUALTABLE
  3804   3805   Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
  3805   3806   #endif
  3806   3807   void sqlite3ResetAllSchemasOfConnection(sqlite3*);
  3807   3808   void sqlite3ResetOneSchema(sqlite3*,int);
  3808   3809   void sqlite3CollapseDatabaseArray(sqlite3*);

Changes to src/vdbe.c.

  5742   5742       assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
  5743   5743     }
  5744   5744   #endif
  5745   5745   
  5746   5746     iDb = pOp->p1;
  5747   5747     assert( iDb>=0 && iDb<db->nDb );
  5748   5748     assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
         5749  +
         5750  +#ifndef SQLITE_OMIT_ALTERTABLE
         5751  +  if( pOp->p4.z==0 ){
         5752  +    sqlite3SchemaClear(db->aDb[iDb].pSchema);
         5753  +    rc = sqlite3InitOne(db, iDb, &p->zErrMsg);
         5754  +    db->mDbFlags |= DBFLAG_SchemaChange;
         5755  +  }else
         5756  +#endif
  5749   5757     /* Used to be a conditional */ {
  5750   5758       zMaster = MASTER_NAME;
  5751   5759       initData.db = db;
  5752   5760       initData.iDb = pOp->p1;
  5753   5761       initData.pzErrMsg = &p->zErrMsg;
  5754   5762       zSql = sqlite3MPrintf(db,
  5755   5763          "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",

Changes to test/altercol.test.

   146    146     ALTER TABLE p1 RENAME d TO "silly name";
   147    147     SELECT sql FROM sqlite_master WHERE name IN ('c1', 'p1');
   148    148   } {
   149    149     {CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "silly name"))}
   150    150     {CREATE TABLE p1(c, "silly name", PRIMARY KEY(c, "silly name"))}
   151    151   }
   152    152   
   153         -# do_execsql_test 4.2 { INSERT INTO c1 VALUES(1, 2); }
          153  +do_execsql_test 4.2 { INSERT INTO c1 VALUES(1, 2); }
   154    154   
   155    155   do_execsql_test 4.3 {
   156    156     CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1);
   157    157   }
   158    158   
   159    159   do_execsql_test 4.4 {
   160    160     ALTER TABLE p1 RENAME "silly name" TO reasonable;