1 create or replace function person_aggregates_comment_count()
2 returns trigger language plpgsql
5 IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
6 update person_aggregates
7 set comment_count = comment_count + 1 where person_id = NEW.creator_id;
8 ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
9 update person_aggregates
10 set comment_count = comment_count - 1 where person_id = OLD.creator_id;
15 create or replace function person_aggregates_post_count()
16 returns trigger language plpgsql
19 IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
20 update person_aggregates
21 set post_count = post_count + 1 where person_id = NEW.creator_id;
23 ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
24 update person_aggregates
25 set post_count = post_count - 1 where person_id = OLD.creator_id;
30 create or replace function community_aggregates_comment_count()
31 returns trigger language plpgsql
34 IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
35 update community_aggregates ca
36 set comments = comments + 1 from post p
37 where p.id = NEW.post_id
38 and ca.community_id = p.community_id;
39 ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
40 update community_aggregates ca
41 set comments = comments - 1 from post p
42 where p.id = OLD.post_id
43 and ca.community_id = p.community_id;