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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
|
# 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}}
do_execsql_test 8.5 {
DROP VIEW zzz;
CREATE TABLE t5(a TEXT, b INT);
INSERT INTO t5(a,b) VALUES('aaa',7),('bbb',3),('ccc',4);
CREATE VIEW vt5(x) AS SELECT group_concat(a ORDER BY b) FROM t5;
SELECT x FROM vt5;
} {bbb,ccc,aaa}
do_execsql_test 8.5.1 {
ALTER TABLE t5 RENAME COLUMN b TO bbb;
SELECT sql FROM sqlite_schema WHERE name='vt5';
} {{CREATE VIEW vt5(x) AS SELECT group_concat(a ORDER BY bbb) FROM t5}}
do_execsql_test 8.5.2 {
SELECT x FROM vt5;
} {bbb,ccc,aaa}
#-------------------------------------------------------------------------
# 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
|