This commit is contained in:
Dull Bananas 2023-12-27 23:22:53 +00:00
parent 4e40aa4b25
commit 3e2866fe8e
2 changed files with 28 additions and 27 deletions

View file

@ -18,24 +18,25 @@ SET
comments = counted.comments comments = counted.comments
FROM ( FROM (
SELECT SELECT
community.id AS community_id, community_id,
count(*) AS comments count(*) AS comments
FROM FROM
comment, comment,
LATERAL (
SELECT
*
FROM
post
WHERE
post.id = comment.post_id
LIMIT 1) AS post
WHERE WHERE
NOT (comment.deleted NOT (comment.deleted
OR comment.removed OR comment.removed
OR EXISTS ( OR post.deleted
SELECT OR post.removed)
1 GROUP BY
FROM community_id) AS counted
post
WHERE
post.id = comment.post_id
AND (post.deleted
OR post.removed)))
GROUP BY
community.id) AS counted
WHERE WHERE
community_aggregates.community_id = counted.community_id; community_aggregates.community_id = counted.community_id;

View file

@ -33,14 +33,16 @@ BEGIN
upvotes::float / downvotes::float upvotes::float / downvotes::float
END; END;
END IF; END IF;
END; END
$$; $$;
-- Selects both old and new rows in a trigger and allows using `sum(count_diff)` to get the number to add to a count -- Selects both old and new rows in a trigger and allows using `sum(count_diff)` to get the number to add to a count
CREATE FUNCTION r.combine_transition_tables () CREATE FUNCTION r.combine_transition_tables ()
RETURNS SETOF record RETURNS SETOF record
LANGUAGE sql LANGUAGE plpgsql
AS $$ AS $$
BEGIN
RETURN QUERY
SELECT SELECT
-1 AS count_diff, -1 AS count_diff,
* *
@ -52,24 +54,22 @@ CREATE FUNCTION r.combine_transition_tables ()
* *
FROM FROM
new_table; new_table;
END
$$; $$;
-- Define functions -- Define functions
CREATE FUNCTION r.creator_id_from_post_aggregates (agg post_aggregates) CREATE FUNCTION r.creator_id_from_post_aggregates (agg post_aggregates)
RETURNS int RETURNS int RETURN agg.creator_id;
SELECT
creator_id
FROM
agg;
CREATE FUNCTION r.creator_id_from_comment_aggregates (agg comment_aggregates) CREATE FUNCTION r.creator_id_from_comment_aggregates (agg comment_aggregates)
RETURNS int RETURNS int RETURN (
SELECT SELECT
creator_id creator_id
FROM FROM
comment comment
WHERE WHERE
comment.id = agg.comment_id LIMIT 1; comment.id = agg.comment_id LIMIT 1
);
-- Create triggers for both post and comments -- Create triggers for both post and comments
CREATE PROCEDURE r.post_or_comment (thing_type text) CREATE PROCEDURE r.post_or_comment (thing_type text)
@ -148,7 +148,7 @@ BEGIN
CREATE TRIGGER aggregates CREATE TRIGGER aggregates
AFTER INSERT OR DELETE OR UPDATE OF score ON thing_like REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table AFTER INSERT OR DELETE OR UPDATE OF score ON thing_like REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE FUNCTION r.thing_aggregates_from_like; EXECUTE FUNCTION r.thing_aggregates_from_like ( );
$b$, $b$,
'thing', 'thing',
thing_type); thing_type);