# 2009 February 2
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#*************************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is testing that SQLite can handle a subtle 
# file format change that may be used in the future to implement
# "ALTER TABLE ... RENAME COLUMN ... TO".
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix altercol

# If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
ifcapable !altertable {
  finish_test
  return
}

# Drop all the tables and views in the 'main' database of database connect
# [db]. Sort the objects by name before dropping them.
#
proc drop_all_tables_and_views {db} {
  set SQL {
    SELECT name, type FROM sqlite_master 
    WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%'
    ORDER BY 1
  }
  foreach {z t} [db eval $SQL] {
    db eval "DROP $t $z"
  }
}

foreach {tn before after} {
  1 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB)}
    {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB)}

  2 {CREATE TABLE t1(a INTEGER, x TEXT, "b" BLOB)}
    {CREATE TABLE t1(a INTEGER, x TEXT, "d" BLOB)}

  3 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK(b!=''))}
    {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK(d!=''))}

  4 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK(t1.b!=''))}
    {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK(t1.d!=''))}

  5 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, CHECK( coalesce(b,c) ))}
    {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, CHECK( coalesce(d,c) ))}

  6 {CREATE TABLE t1(a INTEGER, "b"TEXT, c BLOB, CHECK( coalesce(b,c) ))}
    {CREATE TABLE t1(a INTEGER, "d"TEXT, c BLOB, CHECK( coalesce(d,c) ))}

  7 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, PRIMARY KEY(b, c))}
    {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, PRIMARY KEY(d, c))}

  8 {CREATE TABLE t1(a INTEGER, b TEXT PRIMARY KEY, c BLOB)}
    {CREATE TABLE t1(a INTEGER, d TEXT PRIMARY KEY, c BLOB)}

  9 {CREATE TABLE t1(a, b TEXT, c, PRIMARY KEY(a, b), UNIQUE("B"))}
    {CREATE TABLE t1(a, d TEXT, c, PRIMARY KEY(a, d), UNIQUE("d"))}

 10 {CREATE TABLE t1(a, b, c);   CREATE INDEX t1i ON t1(a, c)}
    {{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(a, c)}}

 11 {CREATE TABLE t1(a, b, c);   CREATE INDEX t1i ON t1(b, c)}
    {{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(d, c)}}

 12 {CREATE TABLE t1(a, b, c);   CREATE INDEX t1i ON t1(b+b+b+b, c) WHERE b>0}
    {{CREATE TABLE t1(a, d, c)} {CREATE INDEX t1i ON t1(d+d+d+d, c) WHERE d>0}}

 13 {CREATE TABLE t1(a, b, c, FOREIGN KEY (b) REFERENCES t2)}
    {CREATE TABLE t1(a, d, c, FOREIGN KEY (d) REFERENCES t2)}

 14 {CREATE TABLE t1(a INTEGER, b TEXT, c BLOB, PRIMARY KEY(b))}
    {CREATE TABLE t1(a INTEGER, d TEXT, c BLOB, PRIMARY KEY(d))}

 15 {CREATE TABLE t1(a INTEGER, b INTEGER, c BLOB, PRIMARY KEY(b))}
    {CREATE TABLE t1(a INTEGER, d INTEGER, c BLOB, PRIMARY KEY(d))}

 16 {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB)}
    {CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB)}

 17  {CREATE TABLE t1(a INTEGER, b INTEGER PRIMARY KEY, c BLOB, FOREIGN KEY (b) REFERENCES t2)}
     {CREATE TABLE t1(a INTEGER, d INTEGER PRIMARY KEY, c BLOB, FOREIGN KEY (d) REFERENCES t2)}

} {
  reset_db
  do_execsql_test 1.$tn.0 $before

  do_execsql_test 1.$tn.1 {
    INSERT INTO t1 VALUES(1, 2, 3);
  }

  do_execsql_test 1.$tn.2 {
    ALTER TABLE t1 RENAME COLUMN b TO d;
  }

  do_execsql_test 1.$tn.3 {
    SELECT * FROM t1;
  } {1 2 3}

  if {[string first INDEX $before]>0} {
    set res $after
  } else {
    set res [list $after]
  }
  do_execsql_test 1.$tn.4 {
    SELECT sql FROM sqlite_master WHERE tbl_name='t1' AND sql!=''
  } $res
}

#-------------------------------------------------------------------------
#
do_execsql_test 2.0 {
  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);
}

sqlite3 db2 test.db
do_execsql_test -db db2 2.1 { SELECT b FROM t3 }

do_execsql_test 2.2 {
  ALTER TABLE t3 RENAME b TO biglongname;
  SELECT sql FROM sqlite_master WHERE name='t3';
} {{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)}}

do_execsql_test -db db2 2.3 { SELECT biglongname FROM t3 }

#-------------------------------------------------------------------------
#
do_execsql_test 3.0 {
  CREATE TABLE t4(x, y, z);
  CREATE TRIGGER ttt AFTER INSERT ON t4 WHEN new.y<0 BEGIN
    SELECT x, y, z FROM t4;
    DELETE FROM t4 WHERE y=32;
    UPDATE t4 SET x=y+1, y=0 WHERE y=32;
    INSERT INTO t4(x, y, z) SELECT 4, 5, 6 WHERE 0;
  END;
  INSERT INTO t4 VALUES(3, 2, 1);
}

do_execsql_test 3.1 {
  ALTER TABLE t4 RENAME y TO abc;
  SELECT sql FROM sqlite_master WHERE name='t4';
} {{CREATE TABLE t4(x, abc, z)}}

do_execsql_test 3.2 {
  SELECT * FROM t4;
} {3 2 1}

do_execsql_test 3.3 { INSERT INTO t4 VALUES(6, 5, 4); } {}

do_execsql_test 3.4 { SELECT sql FROM sqlite_master WHERE type='trigger' } {
{CREATE TRIGGER ttt AFTER INSERT ON t4 WHEN new.abc<0 BEGIN
    SELECT x, abc, z FROM t4;
    DELETE FROM t4 WHERE abc=32;
    UPDATE t4 SET x=abc+1, abc=0 WHERE abc=32;
    INSERT INTO t4(x, abc, z) SELECT 4, 5, 6 WHERE 0;
  END}
}

#-------------------------------------------------------------------------
#
do_execsql_test 4.0 {
  CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, d));
  CREATE TABLE p1(c, d, PRIMARY KEY(c, d));
  PRAGMA foreign_keys = 1;
  INSERT INTO p1 VALUES(1, 2);
  INSERT INTO p1 VALUES(3, 4);
}

do_execsql_test 4.1 {
  ALTER TABLE p1 RENAME d TO "silly name";
  SELECT sql FROM sqlite_master WHERE name IN ('c1', 'p1');
} {
  {CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "silly name"))}
  {CREATE TABLE p1(c, "silly name", PRIMARY KEY(c, "silly name"))}
}

do_execsql_test 4.2 { INSERT INTO c1 VALUES(1, 2); }

do_execsql_test 4.3 {
  CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1);
}

do_execsql_test 4.4 {
  ALTER TABLE p1 RENAME "silly name" TO reasonable;
  SELECT sql FROM sqlite_master WHERE name IN ('c1', 'c2', 'p1');
} {
  {CREATE TABLE c1(a, b, FOREIGN KEY (a, b) REFERENCES p1(c, "reasonable"))}
  {CREATE TABLE p1(c, "reasonable", PRIMARY KEY(c, "reasonable"))}
  {CREATE TABLE c2(a, b, FOREIGN KEY (a, b) REFERENCES p1)}
}

#-------------------------------------------------------------------------

do_execsql_test 5.0 {
  CREATE TABLE t5(a, b, c);
  CREATE INDEX t5a ON t5(a);
  INSERT INTO t5 VALUES(1, 2, 3), (4, 5, 6);
  ANALYZE;
}

do_execsql_test 5.1 {
  ALTER TABLE t5 RENAME b TO big;
  SELECT big FROM t5;
} {2 5}

do_catchsql_test 6.1 {
  ALTER TABLE sqlite_stat1 RENAME tbl TO thetable;
} {1 {table sqlite_stat1 may not be altered}}

#-------------------------------------------------------------------------
#
do_execsql_test 6.0 {
  CREATE TABLE blob(
    rid INTEGER PRIMARY KEY,
    rcvid INTEGER,
    size INTEGER,
    uuid TEXT UNIQUE NOT NULL,
    content BLOB,
    CHECK( length(uuid)>=40 AND rid>0 )
  );
}

do_execsql_test 6.1 {
  ALTER TABLE "blob" RENAME COLUMN "rid" TO "a1";
}

do_catchsql_test 6.2 {
  ALTER TABLE "blob" RENAME COLUMN "a1" TO [where];
} {0 {}}

do_execsql_test 6.3 {
  SELECT "where" FROM blob;
} {}

#-------------------------------------------------------------------------
# Triggers.
#
db close
db2 close
reset_db
do_execsql_test 7.0 {
  CREATE TABLE c(x);
  INSERT INTO c VALUES(0);
  CREATE TABLE t6("col a", "col b", "col c");
  CREATE TRIGGER zzz AFTER UPDATE OF "col a", "col c" ON t6 BEGIN
    UPDATE c SET x=x+1;
  END;
}

do_execsql_test 7.1.1 {
  INSERT INTO t6 VALUES(0, 0, 0);
  UPDATE t6 SET "col c" = 1;
  SELECT * FROM c;
} {1}

do_execsql_test 7.1.2 {
  ALTER TABLE t6 RENAME "col c" TO "col 3";
}

do_execsql_test 7.1.3 {
  UPDATE t6 SET "col 3" = 0;
  SELECT * FROM c;
} {2}

#-------------------------------------------------------------------------
# Views.
#
reset_db
do_execsql_test 8.0 {
  CREATE TABLE a1(x INTEGER, y TEXT, z BLOB, PRIMARY KEY(x));
  CREATE TABLE a2(a, b, c);
  CREATE VIEW v1 AS SELECT x, y, z FROM a1;
}

do_execsql_test 8.1 {
  ALTER TABLE a1 RENAME y TO yyy;
  SELECT sql FROM sqlite_master WHERE type='view';
} {{CREATE VIEW v1 AS SELECT x, yyy, z FROM a1}}

do_execsql_test 8.2.1 {
  DROP VIEW v1;
  CREATE VIEW v2 AS SELECT x, x+x, a, a+a FROM a1, a2;
} {}
do_execsql_test 8.2.2 {
  ALTER TABLE a1 RENAME x TO xxx;
}
do_execsql_test 8.2.3 {
  SELECT sql FROM sqlite_master WHERE type='view';
} {{CREATE VIEW v2 AS SELECT xxx, xxx+xxx, a, a+a FROM a1, a2}}

do_execsql_test 8.3.1 {
  DROP TABLE a2;
  DROP VIEW v2;
  CREATE TABLE a2(a INTEGER PRIMARY KEY, b, c);
  CREATE VIEW v2 AS SELECT xxx, xxx+xxx, a, a+a FROM a1, a2;
} {}
do_execsql_test 8.3.2 {
  ALTER TABLE a1 RENAME xxx TO x;
}
do_execsql_test 8.3.3 {
  SELECT sql FROM sqlite_master WHERE type='view';
} {{CREATE VIEW v2 AS SELECT x, x+x, a, a+a FROM a1, a2}}

do_execsql_test 8.4.0 {
  CREATE TABLE b1(a, b, c);
  CREATE TABLE b2(x, y, z);
}

do_execsql_test 8.4.1 {
  CREATE VIEW vvv AS SELECT c+c || coalesce(c, c) FROM b1, b2 WHERE x=c GROUP BY c HAVING c>0;
  ALTER TABLE b1 RENAME c TO "a;b";
  SELECT sql FROM sqlite_master WHERE name='vvv';
} {{CREATE VIEW vvv AS SELECT "a;b"+"a;b" || coalesce("a;b", "a;b") FROM b1, b2 WHERE x="a;b" GROUP BY "a;b" HAVING "a;b">0}}

do_execsql_test 8.4.2 {
  CREATE VIEW www AS SELECT b FROM b1 UNION ALL SELECT y FROM b2;
  ALTER TABLE b1 RENAME b TO bbb;
  SELECT sql FROM sqlite_master WHERE name='www';
} {{CREATE VIEW www AS SELECT bbb FROM b1 UNION ALL SELECT y FROM b2}}

db collate nocase {string compare}

do_execsql_test 8.4.3 {
  CREATE VIEW xxx AS SELECT a FROM b1 UNION SELECT x FROM b2 ORDER BY 1 COLLATE nocase;
}

do_execsql_test 8.4.4 {
  ALTER TABLE b2 RENAME x TO hello;
  SELECT sql FROM sqlite_master WHERE name='xxx';
} {{CREATE VIEW xxx AS SELECT a FROM b1 UNION SELECT hello FROM b2 ORDER BY 1 COLLATE nocase}}

do_catchsql_test 8.4.5 {
  CREATE VIEW zzz AS SELECT george, ringo FROM b1;
  ALTER TABLE b1 RENAME a TO aaa;
} {1 {error in view zzz: no such column: george}}

#-------------------------------------------------------------------------
# More triggers.
#
proc do_rename_column_test {tn old new lSchema} {
  for {set i 0} {$i < 2} {incr i} {
    drop_all_tables_and_views db

    set lSorted [list]
    foreach sql $lSchema { 
      execsql $sql 
      lappend lSorted [string trim $sql]
    }
    set lSorted [lsort $lSorted]

    do_execsql_test $tn.$i.1 {
      SELECT sql FROM sqlite_master WHERE sql!='' ORDER BY 1
    } $lSorted

    if {$i==1} {
      db close
      sqlite3 db test.db
    }

    do_execsql_test $tn.$i.2 "ALTER TABLE t1 RENAME $old TO $new"

    do_execsql_test $tn.$i.3 {
      SELECT sql FROM sqlite_master ORDER BY 1
    } [string map [list $old $new] $lSorted]
  }
}

foreach {tn old new lSchema} {
  1 _x_ _xxx_ {
    { CREATE TABLE t1(a, b, _x_) }
    { CREATE TRIGGER AFTER INSERT ON t1 BEGIN
        SELECT _x_ FROM t1;
      END }
  }

  2 _x_ _xxx_ {
    { CREATE TABLE t1(a, b, _x_) }
    { CREATE TABLE t2(c, d, e) }
    { CREATE TRIGGER ttt AFTER INSERT ON t2 BEGIN
        SELECT _x_ FROM t1;
      END }
  }

  3 _x_ _xxx_ {
    { CREATE TABLE t1(a, b, _x_ INTEGER, PRIMARY KEY(_x_), CHECK(_x_>0)) }
    { CREATE TABLE t2(c, d, e) }
    { CREATE TRIGGER ttt AFTER UPDATE  ON t1 BEGIN
        INSERT INTO t2 VALUES(new.a, new.b, new._x_);
      END }
  }

  4 _x_ _xxx_ {
    { CREATE TABLE t1(a, b, _x_ INTEGER, PRIMARY KEY(_x_), CHECK(_x_>0)) }
    { CREATE TRIGGER ttt AFTER UPDATE  ON t1 BEGIN
        INSERT INTO t1 VALUES(new.a, new.b, new._x_)
          ON CONFLICT (_x_) WHERE _x_>10 DO UPDATE SET _x_ = _x_+1;
      END }
  }

  4 _x_ _xxx_ {
    { CREATE TABLE t1(a, b, _x_ INTEGER, PRIMARY KEY(_x_), CHECK(_x_>0)) }
    { CREATE TRIGGER ttt AFTER UPDATE  ON t1 BEGIN
        INSERT INTO t1 VALUES(new.a, new.b, new._x_)
          ON CONFLICT (_x_) WHERE _x_>10 DO NOTHING;
      END }
  }
} {
  do_rename_column_test 9.$tn $old $new $lSchema
}

#-------------------------------------------------------------------------
# Test that views can be edited even if there are missing collation 
# sequences or user defined functions.
#
reset_db

ifcapable vtab {
  foreach {tn old new lSchema} {
    1 _x_ _xxx_ {
      { CREATE TABLE t1(a, b, _x_) }
      { CREATE VIEW s1 AS SELECT a, b, _x_ FROM t1 WHERE _x_='abc' COLLATE xyz }
    }
  
    2 _x_ _xxx_ {
      { CREATE TABLE t1(a, b, _x_) }
      { CREATE VIEW v1 AS SELECT a, b, _x_ FROM t1 WHERE scalar(_x_) }
    }
  
    3 _x_ _xxx_ {
      { CREATE TABLE t1(a, b, _x_) }
      { CREATE VIEW v1 AS SELECT a, b, _x_ FROM t1 WHERE _x_ = unicode(1, 2, 3) }
    }
  
    4 _x_ _xxx_ {
      { CREATE TABLE t1(a, b, _x_) }
      { CREATE VIRTUAL TABLE e1 USING echo(t1) }
    }
  } {
    register_echo_module db
    do_rename_column_test 10.$tn $old $new $lSchema
  }
  
  #--------------------------------------------------------------------------
  # Test that if a view or trigger refers to a virtual table for which the
  # module is not available, RENAME COLUMN cannot proceed.
  #
  reset_db
  register_echo_module db
  do_execsql_test 11.0 {
    CREATE TABLE x1(a, b, c);
    CREATE VIRTUAL TABLE e1 USING echo(x1);
  }
  db close
  sqlite3 db test.db
  
  do_execsql_test 11.1 {
    ALTER TABLE x1 RENAME b TO bbb;
    SELECT sql FROM sqlite_master;
  } { {CREATE TABLE x1(a, bbb, c)} {CREATE VIRTUAL TABLE e1 USING echo(x1)} }
  
  do_execsql_test 11.2 {
    CREATE VIEW v1 AS SELECT e1.*, x1.c FROM e1, x1;
  }
  
  do_catchsql_test 11.3 {
    ALTER TABLE x1 RENAME c TO ccc;
  } {1 {error in view v1: no such module: echo}}
}

#-------------------------------------------------------------------------
# Test some error conditions:
#
#   1. Renaming a column of a system table,
#   2. Renaming a column of a VIEW,
#   3. Renaming a column of a virtual table.
#   4. Renaming a column that does not exist.
#   5. Renaming a column of a table that does not exist.
#
reset_db
do_execsql_test 12.1.1 {
  CREATE TABLE t1(a, b);
  CREATE INDEX t1a ON t1(a);
  INSERT INTO t1 VALUES(1, 1), (2, 2), (3, 4);
  ANALYZE;
}
do_catchsql_test 12.1.2 {
  ALTER TABLE sqlite_stat1 RENAME idx TO theindex;
} {1 {table sqlite_stat1 may not be altered}}
do_execsql_test 12.1.3 {
  SELECT sql FROM sqlite_master WHERE tbl_name = 'sqlite_stat1'
} {{CREATE TABLE sqlite_stat1(tbl,idx,stat)}}

do_execsql_test 12.2.1 {
  CREATE VIEW v1 AS SELECT * FROM t1;
  CREATE VIEW v2(c, d) AS SELECT * FROM t1;
}
do_catchsql_test 12.2.2 {
  ALTER TABLE v1 RENAME a TO z;
} {1 {cannot rename columns of view "v1"}}
do_catchsql_test 12.2.3 {
  ALTER TABLE v2 RENAME c TO y;
} {1 {cannot rename columns of view "v2"}}

ifcapable fts5 {
  do_execsql_test 12.3.1 {
    CREATE VIRTUAL TABLE ft USING fts5(a, b, c);
  }
  do_catchsql_test 12.3.2 {
    ALTER TABLE ft RENAME a TO z;
  } {1 {cannot rename columns of virtual table "ft"}}
}

do_execsql_test 12.4.1 {
  CREATE TABLE t2(x, y, z);
}
do_catchsql_test 12.4.2 {
  ALTER TABLE t2 RENAME COLUMN a TO b;
} {1 {no such column: "a"}}

do_catchsql_test 12.5.1 {
  ALTER TABLE t3 RENAME COLUMN a TO b;
} {1 {no such table: t3}}

#-------------------------------------------------------------------------
# Test the effect of some parse/resolve errors.
#
reset_db
do_execsql_test 13.1.1 {
  CREATE TABLE x1(i INTEGER, t TEXT UNIQUE);
  CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
    SELECT * FROM nosuchtable;
  END;
}

do_catchsql_test 13.1.2 {
  ALTER TABLE x1 RENAME COLUMN t TO ttt;
} {1 {error in trigger tr1: no such table: main.nosuchtable}}

do_execsql_test 13.1.3 {
  DROP TRIGGER tr1;
  CREATE INDEX x1i ON x1(i);
  SELECT sql FROM sqlite_master WHERE name='x1i';
} {{CREATE INDEX x1i ON x1(i)}}

sqlite3_db_config db DEFENSIVE 0
do_execsql_test 13.1.4 {
  PRAGMA writable_schema = ON;
  UPDATE sqlite_master SET sql = 'CREATE INDEX x1i ON x1(j)' WHERE name='x1i';
  PRAGMA writable_schema = OFF;
} {}

do_catchsql_test 13.1.5 {
  ALTER TABLE x1 RENAME COLUMN t TO ttt;
} {1 {error in index x1i: no such column: j}}

do_execsql_test 13.1.6 {
  PRAGMA writable_schema = ON;
  UPDATE sqlite_master SET sql = '' WHERE name='x1i';
  PRAGMA writable_schema = OFF;
} {}

do_catchsql_test 13.1.7 {
  ALTER TABLE x1 RENAME COLUMN t TO ttt;
} {1 {error in index x1i: }}

do_execsql_test 13.1.8 {
  PRAGMA writable_schema = ON;
  DELETE FROM sqlite_master WHERE name = 'x1i';
  PRAGMA writable_schema = OFF;
}

do_execsql_test 13.2.0 {
  CREATE TABLE data(x UNIQUE, y, z);
}
foreach {tn trigger error} {
  1 {
    CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
      UPDATE data SET x=x+1 WHERE zzz=new.i;
    END;
  } {no such column: zzz}

  2 {
    CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
      INSERT INTO data(x, y) VALUES(new.i, new.t, 1) 
        ON CONFLICT (x) DO UPDATE SET z=zz+1;
    END;
  } {no such column: zz}

  3 {
    CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
      INSERT INTO x1(i, t) VALUES(new.i+1, new.t||'1') 
        ON CONFLICT (tttttt) DO UPDATE SET t=i+1;
    END;
  } {no such column: tttttt}

  4 {
    CREATE TRIGGER tr1 AFTER INSERT ON x1 BEGIN
      INSERT INTO nosuchtable VALUES(new.i, new.t);
    END;
  } {no such table: main.nosuchtable}
} {
  do_execsql_test 13.2.$tn.1 "
    DROP TRIGGER IF EXISTS tr1;
    $trigger
  "

  do_catchsql_test 13.2.$tn.2 {
    ALTER TABLE x1 RENAME COLUMN t TO ttt;
  } "1 {error in trigger tr1: $error}"
}

#-------------------------------------------------------------------------
# Passing invalid parameters directly to sqlite_rename_column().
#
sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db
do_execsql_test 14.1 {
  CREATE TABLE ddd(sql, type, object, db, tbl, icol, znew, bquote);
  INSERT INTO ddd VALUES(
      'CREATE TABLE x1(i INTEGER, t TEXT)',
      'table', 'x1', 'main', 'x1', -1, 'zzz', 0
  ), (
      'CREATE TABLE x1(i INTEGER, t TEXT)',
      'table', 'x1', 'main', 'x1', 2, 'zzz', 0
  ), (
      'CREATE TABLE x1(i INTEGER, t TEXT)',
      'table', 'x1', 'main', 'notable', 0, 'zzz', 0
  ), (
      'CREATE TABLE x1(i INTEGER, t TEXT)',
      'table', 'x1', 'main', 'ddd', -1, 'zzz', 0
  );
} {}

do_execsql_test 14.2 {
  SELECT 
  sqlite_rename_column(sql, type, object, db, tbl, icol, znew, bquote, 0)
  FROM ddd;
} {{} {} {} {}}
sqlite3_test_control SQLITE_TESTCTRL_INTERNAL_FUNCTIONS db

# If the INTERNAL_FUNCTIONS test-control is disabled (which is the default)
# then the sqlite_rename_table() SQL function is not accessible to
# ordinary SQL.
#
do_catchsql_test 14.3 {
  SELECT sqlite_rename_column(0,0,0,0,0,0,0,0,0);
} {1 {no such function: sqlite_rename_column}}

#-------------------------------------------------------------------------
#
reset_db
do_execsql_test 15.0 {
  CREATE TABLE xxx(a, b, c);
  SELECT a AS d FROM xxx WHERE d=0;
}

do_execsql_test 15.1 {
  CREATE VIEW vvv AS SELECT a AS d FROM xxx WHERE d=0;
  ALTER TABLE xxx RENAME a TO xyz;
}

do_execsql_test 15.2 {
  SELECT sql FROM sqlite_master WHERE type='view';
} {{CREATE VIEW vvv AS SELECT xyz AS d FROM xxx WHERE d=0}}

#-------------------------------------------------------------------------
#
do_execsql_test 16.1.0 {
  CREATE TABLE t1(a,b,c);
  CREATE TABLE t2(d,e,f);
  INSERT INTO t1 VALUES(1,2,3);
  INSERT INTO t2 VALUES(4,5,6);
  CREATE VIEW v4 AS SELECT a, d FROM t1, t2;
  SELECT * FROM v4;
} {1 4}

do_catchsql_test 16.1.1 {
  ALTER TABLE t2 RENAME d TO a;
} {1 {error in view v4 after rename: ambiguous column name: a}}

do_execsql_test 16.1.2 {
  SELECT * FROM v4;
} {1 4}

do_execsql_test 16.1.3 {
  CREATE UNIQUE INDEX t2d ON t2(d);
  CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
    INSERT INTO t2 VALUES(new.a, new.b, new.c)
      ON CONFLICT(d) DO UPDATE SET f = excluded.f;
  END;
}

do_execsql_test 16.1.4 {
  INSERT INTO t1 VALUES(4, 8, 456);
  SELECT * FROM t2;
} {4 5 456}

do_execsql_test 16.1.5 {
  ALTER TABLE t2 RENAME COLUMN f TO "big f";
  INSERT INTO t1 VALUES(4, 0, 20456);
  SELECT * FROM t2;
} {4 5 20456}

do_execsql_test 16.1.6 {
  ALTER TABLE t1 RENAME COLUMN c TO "big c";
  INSERT INTO t1 VALUES(4, 0, 0);
  SELECT * FROM t2;
} {4 5 0}

do_execsql_test 16.2.1 {
  CREATE VIEW temp.v5 AS SELECT "big c" FROM t1; 
  SELECT * FROM v5;
} {3 456 20456 0}

do_execsql_test 16.2.2 {
  ALTER TABLE t1 RENAME COLUMN "big c" TO reallybigc;
} {}

do_execsql_test 16.2.3 {
  SELECT * FROM v5;
} {3 456 20456 0}

#-------------------------------------------------------------------------
#
do_execsql_test 17.0 {
  CREATE TABLE u7(x, y, z);
  CREATE TRIGGER u7t AFTER INSERT ON u7 BEGIN
    INSERT INTO u8 VALUES(new.x, new.y, new.z);
  END;
} {}
do_catchsql_test 17.1 {
  ALTER TABLE u7 RENAME x TO xxx;
} {1 {error in trigger u7t: no such table: main.u8}}

do_execsql_test 17.2 {
  CREATE TEMP TABLE uu7(x, y, z);
  CREATE TRIGGER uu7t AFTER INSERT ON uu7 BEGIN
    INSERT INTO u8 VALUES(new.x, new.y, new.z);
  END;
} {}
do_catchsql_test 17.3 {
  ALTER TABLE uu7 RENAME x TO xxx;
} {1 {error in trigger uu7t: no such table: u8}}

reset_db
forcedelete test.db2
do_execsql_test 18.0 {
  ATTACH 'test.db2' AS aux;
  CREATE TABLE t1(a);
  CREATE TABLE aux.log(v);
  CREATE TEMP TRIGGER tr1 AFTER INSERT ON t1 BEGIN
    INSERT INTO log VALUES(new.a);
  END;
  INSERT INTO t1 VALUES(111);
  SELECT v FROM log;
} {111}

do_execsql_test 18.1 {
  ALTER TABLE t1 RENAME a TO b;
}

reset_db
do_execsql_test 19.0 {
  CREATE TABLE t1(a, b);
  CREATE TABLE t2(c, d);
  CREATE VIEW v2(e) AS SELECT coalesce(t2.c,t1.a) FROM t1, t2 WHERE t1.b=t2.d;
}

do_execsql_test 19.1 {
  ALTER TABLE t1 RENAME a TO f;
  SELECT sql FROM sqlite_master WHERE name = 'v2';
} {
  {CREATE VIEW v2(e) AS SELECT coalesce(t2.c,t1.f) FROM t1, t2 WHERE t1.b=t2.d}
}

# 2019-01-08: https://www.sqlite.org/src/tktview/bc8d94f0fbd633fd9a051e3
#
# ALTER TABLE RENAME COLUMN does not work for tables that have redundant
# UNIQUE constraints.
#
sqlite3 db :memory:
do_execsql_test 20.100 {
  CREATE TABLE t1(aaa,b,c,UNIQUE(aaA),PRIMARY KEY(aAa),UNIQUE(aAA));
  ALTER TABLE t1 RENAME aaa TO bbb;
  SELECT sql FROM sqlite_master WHERE name='t1';
} {{CREATE TABLE t1(bbb,b,c,UNIQUE(bbb),PRIMARY KEY(bbb),UNIQUE(bbb))}}
do_execsql_test 20.105 {
  DROP TABLE t1;
  CREATE TABLE t1(aaa,b,c,UNIQUE(aaA),PRIMARY KEY(aAa),UNIQUE(aAA))WITHOUT ROWID;
  ALTER TABLE t1 RENAME aaa TO bbb;
  SELECT sql FROM sqlite_master WHERE name='t1';
} {{CREATE TABLE t1(bbb,b,c,UNIQUE(bbb),PRIMARY KEY(bbb),UNIQUE(bbb))WITHOUT ROWID}}
do_execsql_test 20.110 {
  DROP TABLE t1;
  CREATE TABLE t1(aa UNIQUE,bb UNIQUE,cc UNIQUE,UNIQUE(aA),PRIMARY KEY(bB),UNIQUE(cC));
  ALTER TABLE t1 RENAME aa TO xx;
  ALTER TABLE t1 RENAME bb TO yy;
  ALTER TABLE t1 RENAME cc TO zz;
  SELECT sql FROM sqlite_master WHERE name='t1';
} {{CREATE TABLE t1(xx UNIQUE,yy UNIQUE,zz UNIQUE,UNIQUE(xx),PRIMARY KEY(yy),UNIQUE(zz))}}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 21.0 {
  CREATE TABLE t1(a, b, c NOT NULL);
  CREATE TRIGGER tr1 AFTER INSERT ON t1 WHEN new.c IS NOT NULL BEGIN
    SELECT c NOT NULL FROM t1;
  END;
}

do_execsql_test 21.1 {
  ALTER TABLE t1 RENAME c TO d;
}

do_execsql_test 21.2 {
  SELECT sql FROM sqlite_schema WHERE name IS 'tr1'
} {{CREATE TRIGGER tr1 AFTER INSERT ON t1 WHEN new.d IS NOT NULL BEGIN
    SELECT d NOT NULL FROM t1;
  END}
}

#-------------------------------------------------------------------------
#
reset_db
do_execsql_test 22.0 {
  CREATE TABLE t1(a, b);
  CREATE TABLE t2(c, othername, extra AS (c + 1));
  ALTER TABLE t1 RENAME a to othername;
  SELECT sql FROM sqlite_schema;
} {
  {CREATE TABLE t1(othername, b)}
  {CREATE TABLE t2(c, othername, extra AS (c + 1))}
}

#-------------------------------------------------------------------------
#
reset_db
sqlite3_db_config db SQLITE_DBCONFIG_DQS_DDL 1
sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1
do_execsql_test 22.0 {
  CREATE TABLE t1(a, b);
  CREATE INDEX x1 on t1("c"=b);
  INSERT INTO t1 VALUES('a', 'a');
  INSERT INTO t1 VALUES('b', 'b');
  INSERT INTO t1 VALUES('c', 'c');
  ALTER TABLE t1 RENAME COLUMN a TO "c";
  PRAGMA integrity_check;
} {ok}

reset_db
do_execsql_test 23.0 {
  CREATE TABLE t1('a'"b",c);
  CREATE INDEX i1 ON t1('a');
  INSERT INTO t1 VALUES(1,2), (3,4);
  ALTER TABLE t1 RENAME COLUMN a TO x;
  PRAGMA integrity_check;
  SELECT sql FROM sqlite_schema WHERE name='t1';

} {ok {CREATE TABLE t1("x" "b",c)}}

# 2022-02-04
# Do not complain about syntax errors in the schema if
# in PRAGMA writable_schema=ON mode.
#
reset_db
do_execsql_test 23.0 {
  CREATE TABLE t1(a INT, b REAL, c TEXT, d BLOB, e ANY);
  CREATE INDEX t1abx ON t1(a, b, a+b) WHERE c IS NOT NULL;
  CREATE VIEW t2 AS SELECT a+10, b*5.0, xyz FROM t1; -- unknown column "xyz"
  CREATE TABLE schema_copy(name TEXT, sql TEXT);
  INSERT INTO schema_copy(name,sql) SELECT name, sql FROM sqlite_schema WHERE sql IS NOT NULL;
} {}
do_catchsql_test 23.1 {
  ALTER TABLE t1 RENAME COLUMN e TO eeee;
} {1 {error in view t2: no such column: xyz}}
do_execsql_test 23.2 {
  SELECT name, sql FROM sqlite_master
    EXCEPT SELECT name, sql FROM schema_copy;
} {}
do_execsql_test 23.3 {
  BEGIN;
  PRAGMA writable_schema=ON;
  ALTER TABLE t1 RENAME COLUMN e TO eeee;
  PRAGMA writable_schema=OFF;
  SELECT name FROM sqlite_master
     WHERE (name, sql) NOT IN (SELECT name, sql FROM schema_copy);
  ROLLBACK;
} {t1}
do_execsql_test 23.10 {
  DROP VIEW t2;
  CREATE TRIGGER r3 AFTER INSERT ON t1 BEGIN
    INSERT INTO t3(x,y) VALUES(new.a, new.b);
    INSERT INTO t4(p) VALUES(new.c);    -- no such table "t4"
  END;
  DELETE FROM schema_copy;
  INSERT INTO schema_copy(name,sql) SELECT name, sql FROM sqlite_schema WHERE sql IS NOT NULL;
} {}
do_catchsql_test 23.11 {
  ALTER TABLE t1 RENAME COLUMN e TO eeee;
} {1 {error in trigger r3: no such table: main.t3}}
do_execsql_test 23.12 {
  SELECT name, sql FROM sqlite_master
    EXCEPT SELECT name, sql FROM schema_copy;
} {}
do_execsql_test 23.13 {
  BEGIN;
  PRAGMA writable_schema=ON;
  ALTER TABLE t1 RENAME COLUMN e TO eeee;
  PRAGMA writable_schema=OFF;
  SELECT name FROM sqlite_master
     WHERE (name, sql) NOT IN (SELECT name, sql FROM schema_copy);
  ROLLBACK;
} {t1}
do_execsql_test 23.20 {
  CREATE TABLE t4(id INTEGER PRIMARY KEY, c1 INT, c2 INT);
  CREATE VIEW t4v1 AS SELECT id, c1, c99 FROM t4;
  DELETE FROM schema_copy;
  INSERT INTO schema_copy SELECT name, sql FROM sqlite_schema;
  BEGIN;
  PRAGMA writable_schema=ON;
  ALTER TABLE t4 RENAME to t4new;
  SELECT name FROM sqlite_schema WHERE (name,sql) NOT IN (SELECT * FROM schema_copy);
  ROLLBACK;
} {t4new}

finish_test
