1 -- Creating a new trigger for when comment.deleted is updated
2 CREATE OR REPLACE FUNCTION post_aggregates_comment_deleted ()
7 IF NEW.deleted = TRUE THEN
11 comments = comments - 1
13 pa.post_id = NEW.post_id;
18 comments = comments + 1
20 pa.post_id = NEW.post_id;
26 CREATE TRIGGER post_aggregates_comment_set_deleted
27 AFTER UPDATE OF deleted ON comment
29 EXECUTE PROCEDURE post_aggregates_comment_deleted ();
31 -- Fix issue with being able to necro-bump your own post
32 CREATE OR REPLACE FUNCTION post_aggregates_comment_count ()
37 IF (TG_OP = 'INSERT') THEN
41 comments = comments + 1,
42 newest_comment_time = NEW.published
44 pa.post_id = NEW.post_id;
45 -- A 2 day necro-bump limit
49 newest_comment_time_necro = NEW.published
54 AND pa.post_id = NEW.post_id
55 -- Fix issue with being able to necro-bump your own post
56 AND NEW.creator_id != p.creator_id
57 AND pa.published > ('now'::timestamp - '2 days'::interval);
58 ELSIF (TG_OP = 'DELETE') THEN
59 -- Join to post because that post may not exist anymore
63 comments = comments - 1
68 AND pa.post_id = OLD.post_id;
69 ELSIF (TG_OP = 'UPDATE') THEN
70 -- Join to post because that post may not exist anymore
74 comments = comments - 1
79 AND pa.post_id = OLD.post_id;