summaryrefslogtreecommitdiff
path: root/test/schemareset.test
blob: 90bbbdaec07d243d7bb1fda00dc191280854a5a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 2026-10-26
#
# 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 tests for running DDL or similar statements from
# within an application-defined function, such that those statements
# cause a schema reset, which then frees the schema out from under
# prepared statements that are still using it.
#
# See bug report https://sqlite.org/bugs/info/3577bc69c52237a4
# from 2026-10-25.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix schemareset

ifcapable !vtab {
  finish_test
  return
}
load_static_extension db series
load_static_extension db tryeval
sqlite3_db_config db DEFENSIVE 0

do_execsql_test 1.1 {
  CREATE TABLE t1(a,b);
  INSERT INTO t1(a,b) SELECT value,value*10 FROM generate_series(1,5);
  CREATE INDEX t1x ON t1(a);
  UPDATE t1
     SET b=coalesce(tryeval('DROP INDEX t1x','SELECT sum(a) FROM t1'),b+1);
  SELECT * FROM t1 ORDER BY a;
} {1 15 2 15 3 15 4 15 5 15}

do_execsql_test 1.2 {
  DROP TABLE t1;
  CREATE TABLE t1(a,b);
  INSERT INTO t1 VALUES(1,'a'),(2,'b'),(3,'c');
  CREATE INDEX t1x ON t1(a);
  UPDATE t1 
     SET b=coalesce(tryeval('DROP INDEX t1x','SELECT count(*) FROM t1'),'xyz');
  SELECT * FROM t1 ORDER BY a;
} {1 3 2 3 3 3}

do_execsql_test 1.3 {
  DROP TABLE t1;
  CREATE TABLE t1(a INTEGER PRIMARY KEY, l INT, c INT) STRICT;
  INSERT INTO t1 VALUES(1,1,1),(2,2,2);
  UPDATE t1
     SET c = coalesce(tryeval('PRAGMA schema_version=1000',
                              'SELECT max(l) FROM t'), c+1);
  SELECT * FROM t1 ORDER BY a;
} {1 1 2 2 2 3}

do_execsql_test 1.4.1 {
  DROP TABLE t1;
  CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT) STRICT;
  INSERT INTO t1 VALUES(1,1),(2,2);
}
do_catchsql_test 1.4.2 {
  UPDATE t1
     SET b = coalesce(tryeval('ATTACH ''/no/such/dir/x.db'' AS aux'), b+1);
} {1 {database schema has changed}}
do_catchsql_test 1.4.3 {
  SELECT * FROM t1 ORDER BY a;
} {0 {1 1 2 2}}

do_execsql_test 1.5.1 {
  DROP TABLE t1;
  CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT) STRICT;
  INSERT INTO t1 VALUES(1,1),(2,2);
  BEGIN;
  SAVEPOINT sp;
  CREATE TABLE zz(x);
}
do_catchsql_test 1.5.2 {
  UPDATE t1
     SET b = coalesce(tryeval('ROLLBACK TO sp', 'SELECT 5'), b+1);
} {1 {database schema has changed}}
do_execsql_test 1.5.3 {
  SELECT * FROM t1 ORDER BY a;
} {1 1 2 2}
catch {db eval ROLLBACK}

do_execsql_test 1.6.1 {
  DROP TABLE t1;
  CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT) STRICT;
  INSERT INTO t1 VALUES(1,1),(2,2);
}
do_catchsql_test 1.6.2 {
  UPDATE t1 SET b = coalesce(tryeval('PRAGMA writable_schema=RESET'), b+1);
} {1 {database schema has changed}}
do_execsql_test 1.6.3 {
  SELECT * FROM t1 ORDER BY a;
} {1 1 2 2}

do_execsql_test 1.7.1 {
  DROP TABLE t1;
  CREATE TABLE t1(a INTEGER PRIMARY KEY, l INT, c INT) STRICT;
  CREATE TABLE u1(x);
  INSERT INTO t1 VALUES(1,1,1),(2,2,2);
}
do_catchsql_test 1.7.2 {
  UPDATE t1 SET c = coalesce(tryeval('ALTER TABLE u1 ADD COLUMN z'), c+1);
} {1 {database schema has changed}}
do_execsql_test 1.7.3 {
  SELECT sql FROM sqlite_master WHERE name='u1';
} {{CREATE TABLE u1(x)}}
do_execsql_test 1.7.4 {
  SELECT * FROM t1 ORDER BY a;
} {1 1 1 2 2 2}

# Forum post f2c49ee062: an idle EXPLAIN statement must not be re-stepped
# against Table objects freed by a schema reset that was caused by DDL
# run on another connection.  EXPLAIN never runs OP_Transaction, so it
# never notices the stale schema cookie on its own.
#
reset_db
sqlite3 db2 test.db
do_execsql_test 2.0 {
  CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
  INSERT INTO t1 VALUES(1,2);
}
proc explain_p4 {db sql opcode} {
  set res [list]
  $db eval $sql {if {$opcode==$::op} {lappend res $p4}}
  return $res
}
foreach {tn sql op} {
  1 {EXPLAIN DELETE FROM t1 WHERE a>5}  Delete
  2 {EXPLAIN DELETE FROM t1}            Clear
  3 {EXPLAIN INSERT INTO t1 VALUES(5,6)} Insert
  4 {EXPLAIN UPDATE t1 SET b=b+1}       Insert
} {
  set ::op $op
  do_test 2.$tn.1 { explain_p4 db $sql $op } t1
  db2 eval {CREATE TABLE tz(z); DROP TABLE tz;}
  do_execsql_test 2.$tn.2 { SELECT count(*) FROM t1 } 1
  do_test 2.$tn.3 { explain_p4 db $sql $op } t1
}
db2 close

finish_test