lemmy/migrations/2023-12-25-034523_replaceable-schema/up.sql

54 lines
2 KiB
MySQL
Raw Normal View History

2023-12-27 01:26:29 +00:00
-- Drop functions and use `CASCADE` to drop the triggers that use them
DROP FUNCTION comment_aggregates_comment, comment_aggregates_score, comment_removed_resolve_reports, community_aggregates_comment_count, community_aggregates_community, community_aggregates_post_count, community_aggregates_post_count_insert, community_aggregates_subscriber_count, person_aggregates_comment_count, person_aggregates_comment_score, person_aggregates_person, person_aggregates_post_count, person_aggregates_post_insert, person_aggregates_post_score, post_aggregates_comment_count, post_aggregates_featured_community, post_aggregates_featured_local, post_aggregates_post, post_aggregates_score, post_removed_resolve_reports, site_aggregates_comment_delete, site_aggregates_comment_insert, site_aggregates_community_insert, site_aggregates_person_delete, site_aggregates_person_insert, site_aggregates_post_delete, site_aggregates_post_insert, site_aggregates_post_update, site_aggregates_site, was_removed_or_deleted, was_restored_or_created CASCADE;
2023-12-27 06:32:36 +00:00
-- Fix values that might be incorrect because of the old triggers
UPDATE
post_aggregates
SET
featured_local = post.featured_local,
featured_community = post.featured_community
FROM
post
WHERE
post_aggregates.post_id = post.id;
UPDATE
community_aggregates
SET
comments = counted.comments
FROM (
SELECT
2023-12-27 23:22:53 +00:00
community_id,
2023-12-27 06:32:36 +00:00
count(*) AS comments
FROM
comment,
2023-12-27 23:22:53 +00:00
LATERAL (
SELECT
*
FROM
post
WHERE
post.id = comment.post_id
LIMIT 1) AS post
2023-12-27 06:32:36 +00:00
WHERE
NOT (comment.deleted
OR comment.removed
2023-12-27 23:22:53 +00:00
OR post.deleted
OR post.removed)
GROUP BY
community_id) AS counted
2023-12-27 06:32:36 +00:00
WHERE
community_aggregates.community_id = counted.community_id;
UPDATE
site_aggregates
SET
communities = (
SELECT
count(*)
FROM
community
WHERE
local);