Update replaceable_schema.sql

This commit is contained in:
dullbananas 2023-12-23 14:15:26 -07:00 committed by GitHub
parent 1cd6ed14ae
commit 94b745fd69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -220,7 +220,7 @@ CREATE TRIGGER aggregates
-- These triggers update aggregates in response to votes. -- These triggers update aggregates in response to votes.
CREATE PROCEDURE aggregates_from_like (target_name text) CREATE PROCEDURE aggregates_from_like (target_name text, creator_id_getter text)
LANGUAGE plpgsql LANGUAGE plpgsql
AS $a$ AS $a$
BEGIN BEGIN
@ -231,7 +231,7 @@ BEGIN
AS $$ AS $$
BEGIN BEGIN
WITH WITH
any_like (target_id, score, added) AS ( individual_vote (target_id, score, vote_amount_change) AS (
SELECT SELECT
%1$s_id, %1$s_id,
score, score,
@ -246,16 +246,18 @@ BEGIN
FROM FROM
new_like new_like
), ),
added_to_target (target_id, added_upvotes, added_downvotes) AS ( vote_group (target_id, added_upvotes, added_downvotes) AS (
SELECT SELECT
target_id, individual_vote.target_id,
sum(added) FILTER (WHERE score = 1), sum(vote_amount_change) FILTER (WHERE score = 1),
sum(added) FILTER (WHERE score = -1) sum(vote_amount_change) FILTER (WHERE score <> 1)
FROM FROM
any_like individual_vote
GROUP BY GROUP BY
target_id individual_vote.target_id
) ),
-- Update aggregates for target
target_aggregates_update_result (creator_id, creator_score_change) AS (
UPDATE UPDATE
%1$s_aggregates AS aggregates %1$s_aggregates AS aggregates
SET SET
@ -267,21 +269,31 @@ BEGIN
(downvotes + added_downvotes)::numeric (downvotes + added_downvotes)::numeric
) )
FROM FROM
added_to_target vote_group
WHERE WHERE
aggregates.comment_id = added_to_comment.comment_id; aggregates.comment_id = vote_group.target_id
RETURNING
%2$s,
added_upvotes - added_downvotes;
RETURN NULL; RETURN NULL;
END END
$$; $$;
CREATE TRIGGER aggregates
AFTER INSERT OR DELETE OR UPDATE OF score ON %1$s_like
REFERENCING OLD TABLE AS old_like NEW TABLE AS new_like
FOR EACH STATEMENT
EXECUTE FUNCTION %1$s_aggregates_from_like;
$b$, $b$,
target_name); target_name,
creator_id_getter);
END END
$a$; $a$;
CALL aggregates_from_like ('comment'); CALL aggregates_from_like ('comment', '(SELECT creator_id FROM comment WHERE id = vote_group.target_id)');
CALL aggregates_from_like ('post'); CALL aggregates_from_like ('post', 'creator_id');
COMMIT; COMMIT;