diff options
author | Ellie Huxtable <ellie@elliehuxtable.com> | 2022-04-21 09:12:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-21 09:12:51 +0100 |
commit | fe05d86bfaf42945ad7fb345693873451f128779 (patch) | |
tree | 205ad242f57855db04133a1e2329a5906af4cf29 | |
parent | 48747e3b7c542c696003f71ef4b4ae457934e57c (diff) |
Fix delete trigger (#317)
I've tested this again by deleting my full history and re-syncing, all
good.
-rw-r--r-- | atuin-server/migrations/20220421073605_fix_count_trigger_delete.sql | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/atuin-server/migrations/20220421073605_fix_count_trigger_delete.sql b/atuin-server/migrations/20220421073605_fix_count_trigger_delete.sql new file mode 100644 index 00000000..6198f300 --- /dev/null +++ b/atuin-server/migrations/20220421073605_fix_count_trigger_delete.sql @@ -0,0 +1,35 @@ +-- the old version of this function used NEW in the delete part when it should +-- use OLD + +create or replace function user_history_count() +returns trigger as +$func$ +begin + if (TG_OP='INSERT') then + update total_history_count_user set total = total + 1 where user_id = new.user_id; + + if not found then + insert into total_history_count_user(user_id, total) + values ( + new.user_id, + (select count(1) from history where user_id = new.user_id) + ); + end if; + + elsif (TG_OP='DELETE') then + update total_history_count_user set total = total - 1 where user_id = old.user_id; + + if not found then + insert into total_history_count_user(user_id, total) + values ( + old.user_id, + (select count(1) from history where user_id = old.user_id) + ); + end if; + end if; + + return NEW; -- this is actually ignored for an after trigger, but oh well +end; +$func$ +language plpgsql volatile -- pldfplplpflh +cost 100; -- default value |