mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-01-15 14:35:54 +00:00
Optimize migrations (#5301)
* Optimize migrations * update smoosh migration --------- Co-authored-by: Dessalines <tyhou13@gmx.com>
This commit is contained in:
parent
a91a03a536
commit
da9582c80a
4 changed files with 140 additions and 86 deletions
|
@ -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
|
downvotes::float / upvotes::float
|
||||||
0
|
|
||||||
ELSE
|
ELSE
|
||||||
(upvotes + downvotes) ^ CASE WHEN upvotes > downvotes THEN
|
upvotes::float / downvotes::float
|
||||||
downvotes::float / upvotes::float
|
|
||||||
ELSE
|
|
||||||
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;
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
post_actions AS a
|
||||||
|
SET
|
||||||
|
(read_comments,
|
||||||
|
read_comments_amount) = (
|
||||||
|
SELECT
|
||||||
|
published,
|
||||||
|
read_comments
|
||||||
|
FROM
|
||||||
|
person_post_aggregates AS b
|
||||||
|
WHERE (b.person_id, b.post_id) = (a.person_id, a.post_id)),
|
||||||
|
hidden = (
|
||||||
|
SELECT
|
||||||
|
published
|
||||||
|
FROM
|
||||||
|
post_hide AS b
|
||||||
|
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 post_actions (person_id, post_id, liked, like_score)
|
||||||
SELECT
|
SELECT
|
||||||
person_id,
|
person_id,
|
||||||
community_id,
|
post_id,
|
||||||
published,
|
published,
|
||||||
expires
|
score
|
||||||
FROM
|
FROM
|
||||||
community_person_ban
|
post_like AS b
|
||||||
ON CONFLICT (person_id,
|
WHERE
|
||||||
community_id)
|
NOT EXISTS (
|
||||||
DO UPDATE SET
|
SELECT
|
||||||
person_id = excluded.person_id,
|
FROM
|
||||||
community_id = excluded.community_id,
|
post_actions AS a
|
||||||
received_ban = excluded.received_ban,
|
WHERE (a.person_id, a.post_id) = (b.person_id, b.post_id));
|
||||||
ban_expires = excluded.ban_expires;
|
|
||||||
|
|
||||||
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
|
|
||||||
person_id = excluded.person_id,
|
|
||||||
target_id = excluded.target_id,
|
|
||||||
blocked = excluded.blocked;
|
|
||||||
|
|
||||||
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;
|
||||||
|
|
Loading…
Reference in a new issue