mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-25 13:51:19 +00:00
Update replaceable_schema.sql
This commit is contained in:
parent
1cd6ed14ae
commit
94b745fd69
1 changed files with 38 additions and 26 deletions
|
@ -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,42 +246,54 @@ 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
|
-- Update aggregates for target
|
||||||
%1$s_aggregates AS aggregates
|
target_aggregates_update_result (creator_id, creator_score_change) AS (
|
||||||
SET
|
UPDATE
|
||||||
score = score + added_upvotes - added_downvotes,
|
%1$s_aggregates AS aggregates
|
||||||
upvotes = upvotes + added_upvotes,
|
SET
|
||||||
downvotes = downvotes + added_downvotes,
|
score = score + added_upvotes - added_downvotes,
|
||||||
controversy_rank = controversy_rank (
|
upvotes = upvotes + added_upvotes,
|
||||||
(upvotes + added_upvotes)::numeric,
|
downvotes = downvotes + added_downvotes,
|
||||||
(downvotes + added_downvotes)::numeric
|
controversy_rank = controversy_rank (
|
||||||
)
|
(upvotes + added_upvotes)::numeric,
|
||||||
FROM
|
(downvotes + added_downvotes)::numeric
|
||||||
added_to_target
|
)
|
||||||
WHERE
|
FROM
|
||||||
aggregates.comment_id = added_to_comment.comment_id;
|
vote_group
|
||||||
|
WHERE
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue