Update replaceable_schema.sql

This commit is contained in:
dullbananas 2023-12-24 14:29:26 -07:00 committed by GitHub
parent e9565f8d51
commit 25afdb2f96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -248,51 +248,50 @@ BEGIN
AS $$ AS $$
BEGIN BEGIN
WITH WITH
vote_group (target_id, added_upvotes, added_downvotes) AS ( -- Update aggregates for target
SELECT target_diff (creator_id, score) AS (
target_id,
sum(count_diff) FILTER (WHERE score = 1),
sum(count_diff) FILTER (WHERE score <> 1)
FROM
r.combine_transition_tables ()
GROUP BY
target_id
),
-- Update aggregates for targe
updated_target (creator_id, score_change) AS (
UPDATE UPDATE
%1$s_aggregates AS target_aggregates %1$s_aggregates AS target_aggregates
SET SET
score = score + added_upvotes - added_downvotes, score = score + diff.upvotes - diff.downvotes,
upvotes = upvotes + added_upvotes, upvotes = upvotes + diff.upvotes,
downvotes = downvotes + added_downvotes, downvotes = downvotes + diff.downvotes,
controversy_rank = controversy_rank ( controversy_rank = controversy_rank (
(upvotes + added_upvotes)::numeric, (upvotes + diff.upvotes)::numeric,
(downvotes + added_downvotes)::numeric (downvotes + diff.downvotes)::numeric
) )
FROM FROM (
vote_group SELECT
target_id,
sum(count_diff) FILTER (WHERE score = 1) AS upvotes,
sum(count_diff) FILTER (WHERE score <> 1) AS downvotes
FROM
r.combine_transition_tables ()
GROUP BY
target_id
) AS diff
WHERE WHERE
target_aggregates.comment_id = vote_group.target_id target_aggregates.comment_id = diff.target_id
RETURNING RETURNING
%2$s, %2$s,
added_upvotes - added_downvotes diff.upvotes - diff.downvotes
) )
-- Update aggregates for target's creator -- Update aggregates for target's creator
UPDATE UPDATE
person_aggregates person_aggregates
SET SET
%1$s_score = %1$s_score + target_group.score_change; %1$s_score = %1$s_score + diff.score;
FROM FROM (
SELECT SELECT
creator_id, creator_id,
sum(score_change) sum(score)
FROM FROM
updated_target target_diff
GROUP BY GROUP BY
creator_id creator_id
) AS diff
WHERE WHERE
person_aggregates.person_id = target_group.creator_id; person_aggregates.person_id = diff.creator_id;
RETURN NULL; RETURN NULL;
END END
@ -309,7 +308,7 @@ BEGIN
END END
$a$; $a$;
CALL r.aggregates_from_like ('comment', '(SELECT creator_id FROM comment WHERE id = vote_group.target_id LIMIT 1)'); CALL r.aggregates_from_like ('comment', '(SELECT creator_id FROM comment WHERE comment.id = target_aggregates.comment_id LIMIT 1)');
CALL r.aggregates_from_like ('post', 'target_aggregates.creator_id'); CALL r.aggregates_from_like ('post', 'target_aggregates.creator_id');