mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-17 08:34:19 +01:00
Add foreign key constraint check in translate_drop_table to reject dropping tables that are referenced by foreign keys when PRAGMA foreign_keys=ON
86 lines
3.1 KiB
Tcl
Executable File
86 lines
3.1 KiB
Tcl
Executable File
#!/usr/bin/env tclsh
|
|
|
|
set testdir [file dirname $argv0]
|
|
source $testdir/tester.tcl
|
|
|
|
# Basic DROP TABLE functionality
|
|
do_execsql_test_on_specific_db {:memory:} drop-table-basic-1 {
|
|
CREATE TABLE t1 (x INTEGER PRIMARY KEY);
|
|
INSERT INTO t1 VALUES (1);
|
|
INSERT INTO t1 VALUES (2);
|
|
DROP TABLE t1;
|
|
SELECT count(*) FROM sqlite_schema WHERE type='table' AND name='t1';
|
|
} {0}
|
|
|
|
# The table should be dropped irrespective of the case of the table name.
|
|
do_execsql_test_on_specific_db {:memory:} drop-table-case-insensitive {
|
|
CREATE TABLE test (x INTEGER PRIMARY KEY);
|
|
INSERT INTO test VALUES (1);
|
|
INSERT INTO test VALUES (2);
|
|
DROP TABLE TeSt;
|
|
SELECT count(*) FROM sqlite_schema WHERE type='table' AND name='test';
|
|
} {0}
|
|
|
|
# Test DROP TABLE IF EXISTS on existing table
|
|
do_execsql_test_on_specific_db {:memory:} drop-table-if-exists-1 {
|
|
CREATE TABLE t2 (x INTEGER PRIMARY KEY);
|
|
DROP TABLE IF EXISTS t2;
|
|
SELECT count(*) FROM sqlite_schema WHERE type='table' AND name='t2';
|
|
} {0}
|
|
|
|
# Test DROP TABLE IF EXISTS on non-existent table
|
|
do_execsql_test_on_specific_db {:memory:} drop-table-if-exists-2 {
|
|
DROP TABLE IF EXISTS nonexistent_table;
|
|
SELECT 'success';
|
|
} {success}
|
|
|
|
# Test dropping table with index
|
|
do_execsql_test_on_specific_db {:memory:} drop-table-with-index-1 {
|
|
CREATE TABLE t3 (x INTEGER PRIMARY KEY, y TEXT);
|
|
CREATE INDEX idx_t3_y ON t3(y);
|
|
INSERT INTO t3 VALUES(1, 'one');
|
|
DROP TABLE t3;
|
|
SELECT count(*) FROM sqlite_schema WHERE tbl_name='t3';
|
|
} {0}
|
|
# Test dropping table cleans up related schema entries
|
|
do_execsql_test_on_specific_db {:memory:} drop-table-schema-cleanup-1 {
|
|
CREATE TABLE t4 (x INTEGER PRIMARY KEY, y TEXT);
|
|
CREATE INDEX idx1_t4 ON t4(x);
|
|
CREATE INDEX idx2_t4 ON t4(y);
|
|
INSERT INTO t4 VALUES(1, 'one');
|
|
DROP TABLE t4;
|
|
SELECT count(*) FROM sqlite_schema WHERE tbl_name='t4';
|
|
} {0}
|
|
|
|
# Test dropping table after multiple inserts and deletes
|
|
do_execsql_test_on_specific_db {:memory:} drop-table-after-ops-1 {
|
|
CREATE TABLE t6 (x INTEGER PRIMARY KEY);
|
|
INSERT INTO t6 VALUES (1);
|
|
INSERT INTO t6 VALUES (2);
|
|
DELETE FROM t6 WHERE x = 1;
|
|
INSERT INTO t6 VALUES (3);
|
|
DROP TABLE t6;
|
|
SELECT count(*) FROM sqlite_schema WHERE type='table' AND name='t6';
|
|
} {0}
|
|
|
|
# Test that DROP TABLE fails when table is referenced by foreign keys
|
|
do_execsql_test_in_memory_any_error drop-table-fk-constraint-failed {
|
|
PRAGMA foreign_keys=ON;
|
|
CREATE TABLE parent(a INTEGER PRIMARY KEY);
|
|
CREATE TABLE child(a INTEGER PRIMARY KEY, b INTEGER, FOREIGN KEY(b) REFERENCES parent(a));
|
|
INSERT INTO parent VALUES (1);
|
|
INSERT INTO child VALUES (123, 1);
|
|
DROP TABLE parent;
|
|
}
|
|
|
|
# Test that DROP TABLE succeeds when foreign keys are disabled
|
|
do_execsql_test_on_specific_db {:memory:} drop-table-fk-disabled-ok {
|
|
PRAGMA foreign_keys=OFF;
|
|
CREATE TABLE parent(a INTEGER PRIMARY KEY);
|
|
CREATE TABLE child(a INTEGER PRIMARY KEY, b INTEGER, FOREIGN KEY(b) REFERENCES parent(a));
|
|
INSERT INTO parent VALUES (1);
|
|
INSERT INTO child VALUES (123, 1);
|
|
DROP TABLE parent;
|
|
SELECT count(*) FROM sqlite_schema WHERE type='table' AND name='parent';
|
|
} {0}
|