mirror of
https://github.com/aljazceru/turso.git
synced 2026-01-17 07:04:20 +01:00
fix: prevent DROP TABLE when table is referenced by foreign keys
Add foreign key constraint check in translate_drop_table to reject dropping tables that are referenced by foreign keys when PRAGMA foreign_keys=ON
This commit is contained in:
@@ -62,3 +62,24 @@ do_execsql_test_on_specific_db {:memory:} drop-table-after-ops-1 {
|
||||
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}
|
||||
|
||||
Reference in New Issue
Block a user