/ Check-in [ca644a28]
Login

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

Overview
Comment: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.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | alter-table-rename-column
Files: files | file ages | folders
SHA3-256: ca644a2877c26561f8fb3b3feb74a070cd46621acb105577f04bc936c9b72a95
User & Date: dan 2018-08-11 17:34:38
Context
2018-08-11
17:49
Fix a bug causing ALTER TABLE RENAME COLUMN to fail when renaming an IPK column that is used in a CHECK constraint. check-in: 6595c881 user: dan tags: alter-table-rename-column
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
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/alter.c.

   802    802     char *zOld = 0;
   803    803     char *zNew = 0;
   804    804     const char *zDb;
   805    805     int iSchema;
   806    806   
   807    807     pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
   808    808     if( !pTab ) goto exit_rename_column;
          809  +  if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ) goto exit_rename_column;
          810  +  
   809    811     iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
   810    812     assert( iSchema>=0 );
   811    813     zDb = db->aDb[iSchema].zDbSName;
   812    814   
   813    815     zOld = sqlite3NameFromToken(db, pOld);
   814    816     if( !zOld ) goto exit_rename_column;
   815    817     for(iCol=0; iCol<pTab->nCol; iCol++){
................................................................................
   819    821       sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld);
   820    822       goto exit_rename_column;
   821    823     }
   822    824   
   823    825     zNew = sqlite3NameFromToken(db, pNew);
   824    826     if( !zNew ) goto exit_rename_column;
   825    827   
   826         -
   827    828     sqlite3NestedParse(pParse, 
   828    829         "UPDATE \"%w\".%s SET "
   829    830         "sql = sqlite_rename_column(sql, %d, %Q, %Q, %Q) "
   830         -      "WHERE type = 'table' OR (type='index' AND tbl_name = %Q AND sql!='')",
          831  +      "WHERE name NOT LIKE 'sqlite_%%' AND ("
          832  +      "   type = 'table' OR (type='index' AND tbl_name = %Q)"
          833  +      ")",
   831    834         zDb, MASTER_NAME, iCol, zNew, pTab->zName, zOld, pTab->zName
   832    835     );
   833    836   
   834    837     /* Drop and reload the database schema. */
   835         -  if( db->mallocFailed==0 ){
   836         -    assert( pParse->pVdbe );
          838  +  if( pParse->pVdbe ){
   837    839       sqlite3ChangeCookie(pParse, iSchema);
   838    840       sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iSchema, 0);
   839    841     }
   840    842   
   841    843    exit_rename_column:
   842    844     sqlite3SrcListDelete(db, pSrc);
   843    845     sqlite3DbFree(db, zOld);

Changes to test/altercol.test.

   160    160     ALTER TABLE p1 RENAME "silly name" TO reasonable;
   161    161     SELECT sql FROM sqlite_master WHERE name IN ('c1', 'c2', 'p1');
   162    162   } {
   163    163     {CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "reasonable"))}
   164    164     {CREATE TABLE p1(c, "reasonable", PRIMARY KEY(c, "reasonable"))}
   165    165     {CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1)}
   166    166   }
          167  +
          168  +#-------------------------------------------------------------------------
          169  +
          170  +do_execsql_test 5.0 {
          171  +  CREATE TABLE t5(a, b, c);
          172  +  CREATE INDEX t5a ON t5(a);
          173  +  INSERT INTO t5 VALUES(1, 2, 3), (4, 5, 6);
          174  +  ANALYZE;
          175  +}
          176  +
          177  +do_execsql_test 5.1 {
          178  +  ALTER TABLE t5 RENAME b TO big;
          179  +  SELECT big FROM t5;
          180  +} {2 5}
          181  +
          182  +do_catchsql_test 6.1 {
          183  +  ALTER TABLE sqlite_stat1 RENAME tbl TO thetable;
          184  +} {1 {table sqlite_stat1 may not be altered}}
   167    185   
   168    186   finish_test
   169    187