Update replaceable_schema.sql

This commit is contained in:
dullbananas 2023-12-23 13:31:57 -07:00 committed by GitHub
parent e330194b1c
commit 1cd6ed14ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -220,55 +220,68 @@ CREATE TRIGGER aggregates
-- These triggers update aggregates in response to votes. -- These triggers update aggregates in response to votes.
CREATE FUNCTION comment_aggregates_from_like() CREATE PROCEDURE aggregates_from_like (target_name text)
RETURNS trigger
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $a$
BEGIN BEGIN
WITH EXECUTE format($b$
any_like (comment_id, score, added) AS ( CREATE FUNCTION %1$s_aggregates_from_like ()
SELECT RETURNS trigger
comment_id, LANGUAGE plpgsql
score, AS $$
-1 BEGIN
WITH
any_like (target_id, score, added) AS (
SELECT
%1$s_id,
score,
-1
FROM
old_like
UNION ALL
SELECT
%1$s_id,
score,
1
FROM
new_like
),
added_to_target (target_id, added_upvotes, added_downvotes) AS (
SELECT
target_id,
sum(added) FILTER (WHERE score = 1),
sum(added) FILTER (WHERE score = -1)
FROM
any_like
GROUP BY
target_id
)
UPDATE
%1$s_aggregates AS aggregates
SET
score = score + added_upvotes - added_downvotes,
upvotes = upvotes + added_upvotes,
downvotes = downvotes + added_downvotes,
controversy_rank = controversy_rank (
(upvotes + added_upvotes)::numeric,
(downvotes + added_downvotes)::numeric
)
FROM FROM
old_like added_to_target
UNION ALL WHERE
SELECT aggregates.comment_id = added_to_comment.comment_id;
comment_id,
score,
1
FROM
new_like
),
added_to_comment (comment_id, added_upvotes, added_downvotes) AS (
SELECT
comment_id,
sum(added) FILTER (WHERE score = 1),
sum(added) FILTER (WHERE score = -1)
FROM
any_like
GROUP BY
comment_id
)
UPDATE
comment_aggregates
SET
score = score + added_upvotes - added_downvotes,
upvotes = upvotes + added_upvotes,
downvotes = downvotes + added_downvotes,
controversy_rank = controversy_rank (
(upvotes + added_upvotes)::numeric,
(downvotes + added_downvotes)::numeric
)
FROM
added_to_comment
WHERE
comment_aggregates.comment_id = added_to_comment.comment_id;
RETURN NULL; RETURN NULL;
END
$$;
$b$,
target_name);
END END
$$; $a$;
CALL aggregates_from_like ('comment');
CALL aggregates_from_like ('post');
COMMIT; COMMIT;