xref: /sqlite-3.40.0/test/vtab_alter.test (revision a690ff36)
1182c4ba9Sdanielk1977# 2007 June 26
2182c4ba9Sdanielk1977#
3182c4ba9Sdanielk1977# The author disclaims copyright to this source code.  In place of
4182c4ba9Sdanielk1977# a legal notice, here is a blessing:
5182c4ba9Sdanielk1977#
6182c4ba9Sdanielk1977#    May you do good and not evil.
7182c4ba9Sdanielk1977#    May you find forgiveness for yourself and forgive others.
8182c4ba9Sdanielk1977#    May you share freely, never taking more than you give.
9182c4ba9Sdanielk1977#
10182c4ba9Sdanielk1977#***********************************************************************
11182c4ba9Sdanielk1977# This file implements regression tests for SQLite library.  The
12182c4ba9Sdanielk1977# focus of this file is testing the ALTER TABLE ... RENAME TO
13182c4ba9Sdanielk1977# command on virtual tables.
14182c4ba9Sdanielk1977#
1585b623f2Sdrh# $Id: vtab_alter.test,v 1.3 2007/12/13 21:54:11 drh Exp $
16182c4ba9Sdanielk1977
17182c4ba9Sdanielk1977set testdir [file dirname $argv0]
18182c4ba9Sdanielk1977source $testdir/tester.tcl
19182c4ba9Sdanielk1977
20856ef1a5Sdanifcapable !vtab||!altertable {
21182c4ba9Sdanielk1977  finish_test
22182c4ba9Sdanielk1977  return
23182c4ba9Sdanielk1977}
24182c4ba9Sdanielk1977
25182c4ba9Sdanielk1977# Register the echo module.
26182c4ba9Sdanielk1977#
27182c4ba9Sdanielk1977# This test uses a special feature of the echo module. If the name
28182c4ba9Sdanielk1977# of the virtual table is a prefix of the name of the underlying
29182c4ba9Sdanielk1977# real table (for example if the v-table is "tbl" and the real table
30182c4ba9Sdanielk1977# is "tbl_base"), then the name of the real table is modified
31182c4ba9Sdanielk1977# when an "ALTER TABLE ... RENAME TO" is executed on the v-table.
32182c4ba9Sdanielk1977# For example:
33182c4ba9Sdanielk1977#
34182c4ba9Sdanielk1977#   sqlite> CREATE TABLE t1_base(a, b, c);
35182c4ba9Sdanielk1977#   sqlite> CREATE VIRTUAL TABLE t1 USING(t1_base);
36182c4ba9Sdanielk1977#   sqlite> ALTER TABLE t1 RENAME TO t2;
37182c4ba9Sdanielk1977#   sqlite> SELECT tbl_name FROM sqlite_master;
38182c4ba9Sdanielk1977#   t2_base
39182c4ba9Sdanielk1977#   t2
40182c4ba9Sdanielk1977#
41182c4ba9Sdanielk1977register_echo_module [sqlite3_connection_pointer db]
42182c4ba9Sdanielk1977
43182c4ba9Sdanielk1977
44182c4ba9Sdanielk1977# Try to rename an echo table. Make sure nothing terrible happens.
45182c4ba9Sdanielk1977#
46182c4ba9Sdanielk1977do_test vtab_alter-1.1 {
47182c4ba9Sdanielk1977  execsql { CREATE TABLE t1(a, b VARCHAR, c INTEGER) }
48182c4ba9Sdanielk1977} {}
49182c4ba9Sdanielk1977do_test vtab_alter-1.2 {
50182c4ba9Sdanielk1977  execsql { CREATE VIRTUAL TABLE t1echo USING echo(t1) }
51182c4ba9Sdanielk1977} {}
52182c4ba9Sdanielk1977do_test vtab_alter-1.3 {
53182c4ba9Sdanielk1977  catchsql { SELECT * FROM t1echo }
54182c4ba9Sdanielk1977} {0 {}}
55182c4ba9Sdanielk1977do_test vtab_alter-1.4 {
56182c4ba9Sdanielk1977  execsql { ALTER TABLE t1echo RENAME TO new }
57182c4ba9Sdanielk1977} {}
58182c4ba9Sdanielk1977do_test vtab_alter-1.5 {
59182c4ba9Sdanielk1977  catchsql { SELECT * FROM t1echo }
60182c4ba9Sdanielk1977} {1 {no such table: t1echo}}
61182c4ba9Sdanielk1977do_test vtab_alter-1.6 {
62182c4ba9Sdanielk1977  catchsql { SELECT * FROM new }
63182c4ba9Sdanielk1977} {0 {}}
64182c4ba9Sdanielk1977
6585b623f2Sdrh# Try to rename an echo table that renames its base table. Make
66182c4ba9Sdanielk1977# sure nothing terrible happens.
67182c4ba9Sdanielk1977#
68182c4ba9Sdanielk1977do_test vtab_alter-2.1 {
69182c4ba9Sdanielk1977  execsql {
70182c4ba9Sdanielk1977    DROP TABLE new;
71182c4ba9Sdanielk1977    DROP TABLE t1;
72182c4ba9Sdanielk1977    CREATE TABLE t1_base(a, b, c);
73182c4ba9Sdanielk1977    CREATE VIRTUAL TABLE t1 USING echo('*_base');
74182c4ba9Sdanielk1977  }
75182c4ba9Sdanielk1977} {}
76182c4ba9Sdanielk1977do_test vtab_alter-2.2 {
77182c4ba9Sdanielk1977  execsql {
78182c4ba9Sdanielk1977    INSERT INTO t1_base VALUES(1, 2, 3);
79182c4ba9Sdanielk1977    SELECT * FROM t1;
80182c4ba9Sdanielk1977  }
81182c4ba9Sdanielk1977} {1 2 3}
82182c4ba9Sdanielk1977do_test vtab_alter-2.3 {
83182c4ba9Sdanielk1977  execsql { ALTER TABLE t1 RENAME TO x }
84182c4ba9Sdanielk1977} {}
85182c4ba9Sdanielk1977do_test vtab_alter-2.4 {
86182c4ba9Sdanielk1977  execsql { SELECT * FROM x; }
87182c4ba9Sdanielk1977} {1 2 3}
88182c4ba9Sdanielk1977do_test vtab_alter-2.5 {
89182c4ba9Sdanielk1977  execsql { SELECT * FROM x_base; }
90182c4ba9Sdanielk1977} {1 2 3}
91182c4ba9Sdanielk1977
9285b623f2Sdrh# Cause an error to occur when the echo module renames its
93182c4ba9Sdanielk1977# backing store table.
94182c4ba9Sdanielk1977#
95182c4ba9Sdanielk1977do_test vtab_alter-3.1 {
96182c4ba9Sdanielk1977  execsql  { CREATE TABLE y_base(a, b, c) }
97182c4ba9Sdanielk1977  catchsql { ALTER TABLE x RENAME TO y }
98*a690ff36Sdrh} {1 {SQL logic error}}
99182c4ba9Sdanielk1977do_test vtab_alter-3.2 {
100182c4ba9Sdanielk1977  execsql  { SELECT * FROM x }
101182c4ba9Sdanielk1977} {1 2 3}
102182c4ba9Sdanielk1977
103182c4ba9Sdanielk1977finish_test
104