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
FROM (
SELECT
community.id AS community_id,
community_id,
count(*) AS comments
FROM
comment,
LATERAL (
SELECT
*
FROM
post
WHERE
post.id = comment.post_id
LIMIT 1) AS post
WHERE
NOT (comment.deleted
OR comment.removed
OR EXISTS (
SELECT
1
FROM
post
WHERE
post.id = comment.post_id
AND (post.deleted
OR post.removed)))
GROUP BY
community.id) AS counted
OR post.deleted
OR post.removed)
GROUP BY
community_id) AS counted
WHERE
community_aggregates.community_id = counted.community_id;

View file

@ -33,14 +33,16 @@ BEGIN
upvotes::float / downvotes::float
END;
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
CREATE FUNCTION r.combine_transition_tables ()
RETURNS SETOF record
LANGUAGE sql
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT
-1 AS count_diff,
*
@ -52,24 +54,22 @@ CREATE FUNCTION r.combine_transition_tables ()
*
FROM
new_table;
END
$$;
-- Define functions
CREATE FUNCTION r.creator_id_from_post_aggregates (agg post_aggregates)
RETURNS int
SELECT
creator_id
FROM
agg;
RETURNS int RETURN agg.creator_id;
CREATE FUNCTION r.creator_id_from_comment_aggregates (agg comment_aggregates)
RETURNS int
SELECT
creator_id
FROM
comment
WHERE
comment.id = agg.comment_id LIMIT 1;
RETURNS int RETURN (
SELECT
creator_id
FROM
comment
WHERE
comment.id = agg.comment_id LIMIT 1
);
-- Create triggers for both post and comments
CREATE PROCEDURE r.post_or_comment (thing_type text)
@ -148,7 +148,7 @@ BEGIN
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
FOR EACH STATEMENT
EXECUTE FUNCTION r.thing_aggregates_from_like;
EXECUTE FUNCTION r.thing_aggregates_from_like ( );
$b$,
'thing',
thing_type);