From 4a8e90bd43205d33fdf02dc326e6b0dbed583525 Mon Sep 17 00:00:00 2001 From: Enrico Testori Date: Sun, 5 May 2019 20:11:02 +0200 Subject: [PATCH] db: decrease comment count when a comment is deleted Update commentsDeleteTriggerFunction in order to decrease the counter before deleting nested comments. Closes https://gitlab.com/commento/commento/issues/157 --- db/20190505191006-comment-count-decrease.sql | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 db/20190505191006-comment-count-decrease.sql diff --git a/db/20190505191006-comment-count-decrease.sql b/db/20190505191006-comment-count-decrease.sql new file mode 100644 index 0000000..46e963d --- /dev/null +++ b/db/20190505191006-comment-count-decrease.sql @@ -0,0 +1,13 @@ +-- This trigger is called every time a comment is deleted, so the comment count for the page where the comment belong is updated +CREATE OR REPLACE FUNCTION commentsDeleteTriggerFunction() RETURNS TRIGGER AS $trigger$ +BEGIN + UPDATE pages + SET commentCount = commentCount - 1 + WHERE domain = old.domain AND path = old.path; + + DELETE FROM comments + WHERE parentHex = old.commentHex; + + RETURN NEW; +END; +$trigger$ LANGUAGE plpgsql;