2023-07-03 18:13:53 +00:00
|
|
|
-- Deleting after removing should not decrement the count twice.
|
2023-08-02 16:44:51 +00:00
|
|
|
CREATE OR REPLACE FUNCTION was_removed_or_deleted (TG_OP text, OLD record, NEW record)
|
|
|
|
RETURNS boolean
|
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
|
|
|
IF (TG_OP = 'INSERT') THEN
|
|
|
|
RETURN FALSE;
|
|
|
|
END IF;
|
|
|
|
IF (TG_OP = 'DELETE' AND OLD.deleted = 'f' AND OLD.removed = 'f') THEN
|
|
|
|
RETURN TRUE;
|
|
|
|
END IF;
|
|
|
|
RETURN TG_OP = 'UPDATE'
|
|
|
|
AND ((OLD.deleted = 'f'
|
|
|
|
AND NEW.deleted = 't')
|
|
|
|
OR (OLD.removed = 'f'
|
|
|
|
AND NEW.removed = 't'));
|
|
|
|
END
|
|
|
|
$$;
|
2023-07-03 18:13:53 +00:00
|
|
|
|
|
|
|
-- Recalculate proper comment count.
|
2023-08-02 16:44:51 +00:00
|
|
|
UPDATE
|
|
|
|
person_aggregates
|
|
|
|
SET
|
|
|
|
comment_count = cnt.count
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
creator_id,
|
|
|
|
count(*) AS count
|
|
|
|
FROM
|
|
|
|
comment
|
|
|
|
WHERE
|
|
|
|
deleted = 'f'
|
|
|
|
AND removed = 'f'
|
|
|
|
GROUP BY
|
|
|
|
creator_id) cnt
|
|
|
|
WHERE
|
|
|
|
person_aggregates.person_id = cnt.creator_id;
|
2023-07-03 18:13:53 +00:00
|
|
|
|
|
|
|
-- Recalculate proper comment score.
|
2023-08-02 16:44:51 +00:00
|
|
|
UPDATE
|
|
|
|
person_aggregates ua
|
|
|
|
SET
|
|
|
|
comment_score = cd.score
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
u.id AS creator_id,
|
|
|
|
coalesce(0, sum(cl.score)) AS score
|
2023-07-03 18:13:53 +00:00
|
|
|
-- User join because comments could be empty
|
2023-08-02 16:44:51 +00:00
|
|
|
FROM
|
|
|
|
person u
|
|
|
|
LEFT JOIN comment c ON u.id = c.creator_id
|
|
|
|
AND c.deleted = 'f'
|
|
|
|
AND c.removed = 'f'
|
|
|
|
LEFT JOIN comment_like cl ON c.id = cl.comment_id
|
|
|
|
GROUP BY
|
|
|
|
u.id) cd
|
|
|
|
WHERE
|
|
|
|
ua.person_id = cd.creator_id;
|
2023-07-03 18:13:53 +00:00
|
|
|
|
|
|
|
-- Recalculate proper post count.
|
2023-08-02 16:44:51 +00:00
|
|
|
UPDATE
|
|
|
|
person_aggregates
|
|
|
|
SET
|
|
|
|
post_count = cnt.count
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
creator_id,
|
|
|
|
count(*) AS count
|
|
|
|
FROM
|
|
|
|
post
|
|
|
|
WHERE
|
|
|
|
deleted = 'f'
|
|
|
|
AND removed = 'f'
|
|
|
|
GROUP BY
|
|
|
|
creator_id) cnt
|
|
|
|
WHERE
|
|
|
|
person_aggregates.person_id = cnt.creator_id;
|
2023-07-03 18:13:53 +00:00
|
|
|
|
|
|
|
-- Recalculate proper post score.
|
2023-08-02 16:44:51 +00:00
|
|
|
UPDATE
|
|
|
|
person_aggregates ua
|
|
|
|
SET
|
|
|
|
post_score = pd.score
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
u.id AS creator_id,
|
|
|
|
coalesce(0, sum(pl.score)) AS score
|
|
|
|
-- User join because posts could be empty
|
|
|
|
FROM
|
|
|
|
person u
|
|
|
|
LEFT JOIN post p ON u.id = p.creator_id
|
|
|
|
AND p.deleted = 'f'
|
|
|
|
AND p.removed = 'f'
|
|
|
|
LEFT JOIN post_like pl ON p.id = pl.post_id
|
|
|
|
GROUP BY
|
|
|
|
u.id) pd
|
|
|
|
WHERE
|
|
|
|
ua.person_id = pd.creator_id;
|
|
|
|
|