Skip to content

Commit fd6ef66

Browse files
authored
Merge 'drop triggers if table drops' from Pavan Nambi
seed: 3038621321660758567 Reviewed-by: Jussi Saurio <[email protected]> Closes #4005
2 parents aecafe8 + 8024bb3 commit fd6ef66

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

core/schema.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ impl Schema {
397397
}
398398
Ok(())
399399
}
400+
pub fn remove_triggers_for_table(&mut self, table_name: &str) {
401+
let table_name = normalize_ident(table_name);
402+
self.triggers.remove(&table_name);
403+
}
400404

401405
pub fn get_trigger_for_table(&self, table_name: &str, name: &str) -> Option<Arc<Trigger>> {
402406
let table_name = normalize_ident(table_name);

core/vdbe/execute.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7746,6 +7746,7 @@ pub fn op_drop_table(
77467746
{
77477747
conn.with_schema_mut(|schema| {
77487748
schema.remove_indices_for_table(table_name);
7749+
schema.remove_triggers_for_table(table_name);
77497750
schema.remove_table(table_name);
77507751
});
77517752
}

testing/trigger.test

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,4 +758,19 @@ do_execsql_test_on_specific_db {:memory:} trigger-multiple-before-insert-lifo {
758758
2|3|4|5|t67_second
759759
3|4|6|8|t67_second
760760
4|2|3|4|t67_first
761-
5|1|1|1|jussi}
761+
5|1|1|1|jussi}
762+
763+
# dropping table should drop triggers
764+
do_execsql_test_on_specific_db {:memory:} trigger-drop-make-same-table{
765+
create table a(id int primary key, x int);
766+
create table t(id int primary key, b int, d int, c int);
767+
create table t1(id int, y int); create trigger trg after insert on t begin update a set x = x - new.d + new.c where id = new.b; insert into t1(id, y) values (new.id, new.c - new.d); end; insert into a values (1, 5000);
768+
insert into a values (2, 3000);
769+
drop table t;
770+
create table t(id int primary key, b int, c int, d int);
771+
insert into t values (1, 1, 1000, 50);
772+
insert into t values (2, 2, 500, 100);
773+
select * from a;
774+
select * from t1;
775+
} {1|5000
776+
2|3000}

0 commit comments

Comments
 (0)