/ Check-in [2dec9ea4]
Login

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

Overview
Comment:Ensure the schema cookie is changed when a column is renamed.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | alter-table-rename-column
Files: files | file ages | folders
SHA3-256:2dec9ea4ab1779d00bb22add0aa9a8536661d8abc9f4c2fb8628e04d4cd84326
User & Date: dan 2018-08-10 20:19:34
Context
2018-08-11
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
19:33
Fix harmless compiler warnings. check-in: 9564d700 user: drh tags: alter-table-rename-column
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/alter.c.

   818    818     if( iCol==pTab->nCol ){
   819    819       sqlite3ErrorMsg(pParse, "no such column: \"%s\"", zOld);
   820    820       goto exit_rename_column;
   821    821     }
   822    822   
   823    823     zNew = sqlite3NameFromToken(db, pNew);
   824    824     if( !zNew ) goto exit_rename_column;
          825  +
   825    826   
   826    827     sqlite3NestedParse(pParse, 
   827    828         "UPDATE \"%w\".%s SET "
   828    829         "sql = sqlite_rename_column(sql, %d, %Q, %Q, %Q) "
   829    830         "WHERE type = 'table' OR (type='index' AND tbl_name = %Q AND sql!='')",
   830    831         zDb, MASTER_NAME, iCol, zNew, pTab->zName, zOld, pTab->zName
   831    832     );
   832    833   
   833    834     /* Drop and reload the internal table schema. */
          835  +  sqlite3ChangeCookie(pParse, iSchema);
   834    836     reloadTableSchema(pParse, pTab, pTab->zName);
   835    837   
   836    838    exit_rename_column:
   837    839     sqlite3SrcListDelete(db, pSrc);
   838    840     sqlite3DbFree(db, zOld);
   839    841     sqlite3DbFree(db, zNew);
   840    842     return;

Changes to test/altercol.test.

    94     94   
    95     95   #-------------------------------------------------------------------------
    96     96   #
    97     97   do_execsql_test 2.0 {
    98     98     CREATE TABLE t3(a, b, c, d, e, f, g, h, i, j, k, l, m, FOREIGN KEY (b, c, d, e, f, g, h, i, j, k, l, m) REFERENCES t4);
    99     99   }
   100    100   
   101         -do_execsql_test 2.1 {
          101  +sqlite3 db2 test.db
          102  +do_execsql_test -db db2 2.1 { SELECT b FROM t3 }
          103  +
          104  +do_execsql_test 2.2 {
   102    105     ALTER TABLE t3 RENAME b TO biglongname;
   103    106     SELECT sql FROM sqlite_master WHERE name='t3';
   104    107   } {{CREATE TABLE t3(a, biglongname, c, d, e, f, g, h, i, j, k, l, m, FOREIGN KEY (biglongname, c, d, e, f, g, h, i, j, k, l, m) REFERENCES t4)}}
   105    108   
          109  +do_execsql_test -db db2 2.3 { SELECT biglongname FROM t3 }
   106    110   
   107    111   #-------------------------------------------------------------------------
   108    112   #
   109    113   do_execsql_test 3.0 {
   110    114     CREATE TABLE t4(x, y, z);
   111    115     CREATE TRIGGER ttt AFTER INSERT ON t4 WHEN new.y<0 BEGIN
   112    116       SELECT 1, 2, 3, 4;
................................................................................
   142    146     ALTER TABLE p1 RENAME d TO "silly name";
   143    147     SELECT sql FROM sqlite_master WHERE name IN ('c1', 'p1');
   144    148   } {
   145    149     {CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "silly name"))}
   146    150     {CREATE TABLE p1(c, "silly name", PRIMARY KEY(c, "silly name"))}
   147    151   }
   148    152   
   149         -do_execsql_test 4.2 {
          153  +# do_execsql_test 4.2 { INSERT INTO c1 VALUES(1, 2); }
          154  +
          155  +do_execsql_test 4.3 {
   150    156     CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1);
   151    157   }
   152    158   
   153         -do_execsql_test 4.1 {
          159  +do_execsql_test 4.4 {
   154    160     ALTER TABLE p1 RENAME "silly name" TO reasonable;
   155    161     SELECT sql FROM sqlite_master WHERE name IN ('c1', 'c2', 'p1');
   156    162   } {
   157    163     {CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "reasonable"))}
   158    164     {CREATE TABLE p1(c, "reasonable", PRIMARY KEY(c, "reasonable"))}
   159    165     {CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1)}
   160    166   }
   161    167   
   162    168   finish_test
   163    169