lemmy/migrations/2024-09-24-110638_only-shared-inbox/up.sql

57 lines
1.2 KiB
MySQL
Raw Normal View History

2024-09-24 12:44:07 +00:00
-- Remove text fields inbox_url, shared_inbox_url from person, community and site.
-- Instead use a foreign key for these urls so they can be deduplicated.
-- New inbox table
-- TODO: add trigger which removes unused inbox items
CREATE TABLE inbox (
id serial PRIMARY KEY,
2024-10-17 12:13:33 +00:00
url varchar(255) NOT NULL UNIQUE
2024-09-24 12:44:07 +00:00
);
-- Move existing inbox values to inbox table, and replace with foreign key
ALTER TABLE person
ADD COLUMN inbox_id int;
WITH inboxes AS (
SELECT
id AS person_id,
coalesce(shared_inbox_url, inbox_url) AS url
FROM
person
),
inserted AS (
INSERT INTO inbox (url)
SELECT
url
FROM
inboxes
ON CONFLICT
DO NOTHING
2024-10-17 10:13:33 +00:00
RETURNING
id,
2024-09-24 12:44:07 +00:00
url)
UPDATE
person
SET
inbox_id = inserted.id
FROM
inboxes,
inserted
WHERE
person.id = inboxes.person_id
AND inserted.url = inboxes.url;
ALTER TABLE person
2024-10-17 10:13:33 +00:00
ADD CONSTRAINT person_inbox_id_fkey FOREIGN KEY (inbox_id) REFERENCES inbox (id) ON UPDATE CASCADE ON DELETE CASCADE;
2024-09-24 12:44:07 +00:00
ALTER TABLE person
ALTER COLUMN inbox_id SET NOT NULL;
-- Drop old columns and rename new one
2024-10-17 10:13:33 +00:00
ALTER TABLE person
DROP COLUMN inbox_url;
ALTER TABLE person
DROP COLUMN shared_inbox_url;
2024-09-24 12:44:07 +00:00
-- TODO: same thing for community and site