diff --git a/migrations/2023-12-25-034523_replaceable-schema/up.sql b/migrations/2023-12-25-034523_replaceable-schema/up.sql index 44bf0b756..faa40c332 100644 --- a/migrations/2023-12-25-034523_replaceable-schema/up.sql +++ b/migrations/2023-12-25-034523_replaceable-schema/up.sql @@ -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; diff --git a/replaceable_schema.sql b/replaceable_schema.sql index 7b3a2f4f1..46cae7eda 100644 --- a/replaceable_schema.sql +++ b/replaceable_schema.sql @@ -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);