Optimize migrations (#5301)

* Optimize migrations

* update smoosh migration

---------

Co-authored-by: Dessalines <tyhou13@gmx.com>
This commit is contained in:
dullbananas 2025-01-13 21:10:31 -07:00 committed by GitHub
parent a91a03a536
commit da9582c80a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 140 additions and 86 deletions

View file

@ -1,17 +1,14 @@
UPDATE UPDATE
post_aggregates post_aggregates
SET SET
controversy_rank = CASE WHEN downvotes <= 0 controversy_rank = (upvotes + downvotes) ^ CASE WHEN upvotes > downvotes THEN
OR upvotes <= 0 THEN
0
ELSE
(upvotes + downvotes) ^ CASE WHEN upvotes > downvotes THEN
downvotes::float / upvotes::float downvotes::float / upvotes::float
ELSE ELSE
upvotes::float / downvotes::float upvotes::float / downvotes::float
END END
END
WHERE WHERE
upvotes > 0 upvotes > 0
AND downvotes > 0; AND downvotes > 0
-- a number divided by itself is 1, and `* 1` does the same thing as `^ 1`
AND upvotes != downvotes;

View file

@ -2,32 +2,28 @@
UPDATE UPDATE
person person
SET SET
inbox_url = subquery.inbox_url shared_inbox_url = inbox_url
FROM (
SELECT
id,
coalesce(shared_inbox_url, inbox_url) AS inbox_url
FROM
person) AS subquery
WHERE WHERE
person.id = subquery.id; shared_inbox_url IS NULL;
ALTER TABLE person ALTER TABLE person
DROP COLUMN shared_inbox_url; DROP COLUMN inbox_url,
ALTER COLUMN shared_inbox_url SET NOT NULL,
ALTER COLUMN shared_inbox_url SET DEFAULT generate_unique_changeme ();
ALTER TABLE person RENAME COLUMN shared_inbox_url TO inbox_url;
UPDATE UPDATE
community community
SET SET
inbox_url = subquery.inbox_url shared_inbox_url = inbox_url
FROM (
SELECT
id,
coalesce(shared_inbox_url, inbox_url) AS inbox_url
FROM
community) AS subquery
WHERE WHERE
community.id = subquery.id; shared_inbox_url IS NULL;
ALTER TABLE community ALTER TABLE community
DROP COLUMN shared_inbox_url; DROP COLUMN inbox_url,
ALTER COLUMN shared_inbox_url SET NOT NULL,
ALTER COLUMN shared_inbox_url SET DEFAULT generate_unique_changeme ();
ALTER TABLE community RENAME COLUMN shared_inbox_url TO inbox_url;

View file

@ -14,6 +14,7 @@ ALTER TABLE community_follower
CREATE OR REPLACE FUNCTION convert_follower_state (b bool) CREATE OR REPLACE FUNCTION convert_follower_state (b bool)
RETURNS community_follower_state RETURNS community_follower_state
LANGUAGE sql LANGUAGE sql
IMMUTABLE PARALLEL SAFE
AS $$ AS $$
SELECT SELECT
CASE WHEN b = TRUE THEN CASE WHEN b = TRUE THEN

View file

@ -87,6 +87,57 @@ ON CONFLICT (person_id,
DO UPDATE SET DO UPDATE SET
saved = excluded.saved; saved = excluded.saved;
INSERT INTO person_actions (person_id, target_id, blocked)
SELECT
person_id,
target_id,
published
FROM
person_block
ON CONFLICT (person_id,
target_id)
DO UPDATE SET
blocked = excluded.blocked;
UPDATE
community_actions AS a
SET
blocked = (
SELECT
published
FROM
community_block AS b
WHERE (b.person_id, b.community_id) = (a.person_id, a.community_id)),
became_moderator = (
SELECT
published
FROM
community_moderator AS b
WHERE (b.person_id, b.community_id) = (a.person_id, a.community_id)),
(received_ban,
ban_expires) = (
SELECT
published,
expires
FROM
community_person_ban AS b
WHERE (b.person_id, b.community_id) = (a.person_id, a.community_id));
INSERT INTO community_actions (person_id, community_id, received_ban, ban_expires)
SELECT
person_id,
community_id,
published,
expires
FROM
community_person_ban AS b
WHERE
NOT EXISTS (
SELECT
FROM
community_actions AS a
WHERE (a.person_id, a.community_id) = (b.person_id, b.community_id));
INSERT INTO community_actions (person_id, community_id, blocked) INSERT INTO community_actions (person_id, community_id, blocked)
SELECT SELECT
person_id, person_id,
@ -97,9 +148,9 @@ FROM
ON CONFLICT (person_id, ON CONFLICT (person_id,
community_id) community_id)
DO UPDATE SET DO UPDATE SET
person_id = excluded.person_id, blocked = excluded.blocked
community_id = excluded.community_id, WHERE
blocked = excluded.blocked; community_actions.blocked IS NULL;
INSERT INTO community_actions (person_id, community_id, became_moderator) INSERT INTO community_actions (person_id, community_id, became_moderator)
SELECT SELECT
@ -111,39 +162,56 @@ FROM
ON CONFLICT (person_id, ON CONFLICT (person_id,
community_id) community_id)
DO UPDATE SET DO UPDATE SET
person_id = excluded.person_id, became_moderator = excluded.became_moderator
community_id = excluded.community_id, WHERE
became_moderator = excluded.became_moderator; community_actions.became_moderator IS NULL;
INSERT INTO community_actions (person_id, community_id, received_ban, ban_expires) UPDATE
SELECT post_actions AS a
person_id, SET
community_id, (read_comments,
read_comments_amount) = (
SELECT
published, published,
expires read_comments
FROM FROM
community_person_ban person_post_aggregates AS b
ON CONFLICT (person_id, WHERE (b.person_id, b.post_id) = (a.person_id, a.post_id)),
community_id) hidden = (
DO UPDATE SET SELECT
person_id = excluded.person_id, published
community_id = excluded.community_id, FROM
received_ban = excluded.received_ban, post_hide AS b
ban_expires = excluded.ban_expires; WHERE (b.person_id, b.post_id) = (a.person_id, a.post_id)),
(liked,
like_score) = (
SELECT
published,
score
FROM
post_like AS b
WHERE (b.person_id, b.post_id) = (a.person_id, a.post_id)),
saved = (
SELECT
published
FROM
post_saved AS b
WHERE (b.person_id, b.post_id) = (a.person_id, a.post_id));
INSERT INTO person_actions (person_id, target_id, blocked) INSERT INTO post_actions (person_id, post_id, liked, like_score)
SELECT SELECT
person_id, person_id,
target_id, post_id,
published published,
score
FROM FROM
person_block post_like AS b
ON CONFLICT (person_id, WHERE
target_id) NOT EXISTS (
DO UPDATE SET SELECT
person_id = excluded.person_id, FROM
target_id = excluded.target_id, post_actions AS a
blocked = excluded.blocked; WHERE (a.person_id, a.post_id) = (b.person_id, b.post_id));
INSERT INTO post_actions (person_id, post_id, read_comments, read_comments_amount) INSERT INTO post_actions (person_id, post_id, read_comments, read_comments_amount)
SELECT SELECT
@ -157,33 +225,9 @@ ON CONFLICT (person_id,
post_id) post_id)
DO UPDATE SET DO UPDATE SET
read_comments = excluded.read_comments, read_comments = excluded.read_comments,
read_comments_amount = excluded.read_comments_amount; read_comments_amount = excluded.read_comments_amount
WHERE
INSERT INTO post_actions (person_id, post_id, hidden) post_actions.read_comments IS NULL;
SELECT
person_id,
post_id,
published
FROM
post_hide
ON CONFLICT (person_id,
post_id)
DO UPDATE SET
hidden = excluded.hidden;
INSERT INTO post_actions (person_id, post_id, liked, like_score)
SELECT
person_id,
post_id,
published,
score
FROM
post_like
ON CONFLICT (person_id,
post_id)
DO UPDATE SET
liked = excluded.liked,
like_score = excluded.like_score;
INSERT INTO post_actions (person_id, post_id, saved) INSERT INTO post_actions (person_id, post_id, saved)
SELECT SELECT
@ -195,7 +239,23 @@ FROM
ON CONFLICT (person_id, ON CONFLICT (person_id,
post_id) post_id)
DO UPDATE SET DO UPDATE SET
saved = excluded.saved; saved = excluded.saved
WHERE
post_actions.saved IS NULL;
INSERT INTO post_actions (person_id, post_id, hidden)
SELECT
person_id,
post_id,
published
FROM
post_hide
ON CONFLICT (person_id,
post_id)
DO UPDATE SET
hidden = excluded.hidden
WHERE
post_actions.hidden IS NULL;
-- Drop old tables -- Drop old tables
DROP TABLE comment_saved, community_block, community_moderator, community_person_ban, person_block, person_post_aggregates, post_hide, post_like, post_saved; DROP TABLE comment_saved, community_block, community_moderator, community_person_ban, person_block, person_post_aggregates, post_hide, post_like, post_saved;