lemmy/migrations/2023-10-24-030352_change_primary_keys_and_remove_some_id_columns/up.sql
dullbananas 8e2cbc9a0f
Remove id column and use different primary key on some tables (#4093)
* post_saved

* fmt

* remove unique and not null

* put person_id first in primary key and remove index

* use post_saved.find

* change captcha_answer

* remove removal of not null

* comment_aggregates

* comment_like

* comment_saved

* aggregates

* remove "\"

* deduplicate site_aggregates

* person_post_aggregates

* community_moderator

* community_block

* community_person_ban

* custom_emoji_keyword

* federation allow/block list

* federation_queue_state

* instance_block

* local_site_rate_limit, local_user_language, login_token

* person_ban, person_block, person_follower, post_like, post_read, received_activity

* community_follower, community_language, site_language

* fmt

* image_upload

* remove unused newtypes

* remove more indexes

* use .find

* merge

* fix site_aggregates_site function

* fmt

* Primary keys dess (#17)

* Also order reports by oldest first (ref #4123) (#4129)

* Support signed fetch for federation (fixes #868) (#4125)

* Support signed fetch for federation (fixes #868)

* taplo

* add federation queue state to get_federated_instances api (#4104)

* add federation queue state to get_federated_instances api

* feature gate

* move retry sleep function

* move stuff around

* Add UI setting for collapsing bot comments. Fixes #3838 (#4098)

* Add UI setting for collapsing bot comments. Fixes #3838

* Fixing clippy check.

* Only keep sent and received activities for 7 days (fixes #4113, fixes #4110) (#4131)

* Only check auth secure on release mode. (#4127)

* Only check auth secure on release mode.

* Fixing wrong js-client.

* Adding is_debug_mode var.

* Fixing the desktop image on the README. (#4135)

* Delete dupes and add possibly missing unique constraint on person_aggregates.

* Fixing clippy lints.

---------

Co-authored-by: Nutomic <me@nutomic.com>
Co-authored-by: phiresky <phireskyde+git@gmail.com>

* fmt

* Update community_block.rs

* Update instance_block.rs

* Update person_block.rs

* Update person_block.rs

---------

Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
Co-authored-by: Nutomic <me@nutomic.com>
Co-authored-by: phiresky <phireskyde+git@gmail.com>
2023-11-13 08:14:07 -05:00

229 lines
6.2 KiB
PL/PgSQL

ALTER TABLE captcha_answer
DROP COLUMN id,
ADD PRIMARY KEY (uuid),
DROP CONSTRAINT captcha_answer_uuid_key;
ALTER TABLE comment_aggregates
DROP COLUMN id,
ADD PRIMARY KEY (comment_id),
DROP CONSTRAINT comment_aggregates_comment_id_key;
ALTER TABLE comment_like
DROP COLUMN id,
ADD PRIMARY KEY (person_id, comment_id),
DROP CONSTRAINT comment_like_comment_id_person_id_key;
DROP INDEX idx_comment_like_person;
ALTER TABLE comment_saved
DROP COLUMN id,
ADD PRIMARY KEY (person_id, comment_id),
DROP CONSTRAINT comment_saved_comment_id_person_id_key;
DROP INDEX idx_comment_saved_person_id;
ALTER TABLE community_aggregates
DROP COLUMN id,
ADD PRIMARY KEY (community_id),
DROP CONSTRAINT community_aggregates_community_id_key;
ALTER TABLE community_block
DROP COLUMN id,
ADD PRIMARY KEY (person_id, community_id),
DROP CONSTRAINT community_block_person_id_community_id_key;
DROP INDEX idx_community_block_person;
ALTER TABLE community_follower
DROP COLUMN id,
ADD PRIMARY KEY (person_id, community_id),
DROP CONSTRAINT community_follower_community_id_person_id_key;
DROP INDEX idx_community_follower_person;
ALTER TABLE community_language
DROP COLUMN id,
ADD PRIMARY KEY (community_id, language_id),
DROP CONSTRAINT community_language_community_id_language_id_key;
ALTER TABLE community_moderator
DROP COLUMN id,
ADD PRIMARY KEY (person_id, community_id),
DROP CONSTRAINT community_moderator_community_id_person_id_key;
DROP INDEX idx_community_moderator_person;
ALTER TABLE community_person_ban
DROP COLUMN id,
ADD PRIMARY KEY (person_id, community_id),
DROP CONSTRAINT community_person_ban_community_id_person_id_key;
ALTER TABLE custom_emoji_keyword
DROP COLUMN id,
ADD PRIMARY KEY (custom_emoji_id, keyword),
DROP CONSTRAINT custom_emoji_keyword_custom_emoji_id_keyword_key;
ALTER TABLE federation_allowlist
DROP COLUMN id,
ADD PRIMARY KEY (instance_id),
DROP CONSTRAINT federation_allowlist_instance_id_key;
ALTER TABLE federation_blocklist
DROP COLUMN id,
ADD PRIMARY KEY (instance_id),
DROP CONSTRAINT federation_blocklist_instance_id_key;
ALTER TABLE federation_queue_state
DROP COLUMN id,
ADD PRIMARY KEY (instance_id),
DROP CONSTRAINT federation_queue_state_instance_id_key;
ALTER TABLE image_upload
DROP COLUMN id,
ADD PRIMARY KEY (pictrs_alias),
DROP CONSTRAINT image_upload_pictrs_alias_key;
ALTER TABLE instance_block
DROP COLUMN id,
ADD PRIMARY KEY (person_id, instance_id),
DROP CONSTRAINT instance_block_person_id_instance_id_key;
ALTER TABLE local_site_rate_limit
DROP COLUMN id,
ADD PRIMARY KEY (local_site_id),
DROP CONSTRAINT local_site_rate_limit_local_site_id_key;
ALTER TABLE local_user_language
DROP COLUMN id,
ADD PRIMARY KEY (local_user_id, language_id),
DROP CONSTRAINT local_user_language_local_user_id_language_id_key;
ALTER TABLE login_token
DROP COLUMN id,
ADD PRIMARY KEY (token),
DROP CONSTRAINT login_token_token_key;
-- Delete duplicates which can exist because of missing `UNIQUE` constraint
DELETE FROM person_aggregates AS a USING (
SELECT
min(id) AS id,
person_id
FROM
person_aggregates
GROUP BY
person_id
HAVING
count(*) > 1) AS b
WHERE
a.person_id = b.person_id
AND a.id != b.id;
ALTER TABLE person_aggregates
DROP CONSTRAINT IF EXISTS person_aggregates_person_id_key;
ALTER TABLE person_aggregates
ADD UNIQUE (person_id);
ALTER TABLE person_aggregates
DROP COLUMN id,
ADD PRIMARY KEY (person_id),
DROP CONSTRAINT person_aggregates_person_id_key;
ALTER TABLE person_ban
DROP COLUMN id,
ADD PRIMARY KEY (person_id),
DROP CONSTRAINT person_ban_person_id_key;
ALTER TABLE person_block
DROP COLUMN id,
ADD PRIMARY KEY (person_id, target_id),
DROP CONSTRAINT person_block_person_id_target_id_key;
ALTER TABLE person_follower
DROP COLUMN id,
ADD PRIMARY KEY (follower_id, person_id),
DROP CONSTRAINT person_follower_follower_id_person_id_key;
ALTER TABLE person_post_aggregates
DROP COLUMN id,
ADD PRIMARY KEY (person_id, post_id),
DROP CONSTRAINT person_post_aggregates_person_id_post_id_key;
ALTER TABLE post_aggregates
DROP COLUMN id,
ADD PRIMARY KEY (post_id),
DROP CONSTRAINT post_aggregates_post_id_key;
ALTER TABLE post_like
DROP COLUMN id,
ADD PRIMARY KEY (person_id, post_id),
DROP CONSTRAINT post_like_post_id_person_id_key;
DROP INDEX idx_post_like_person;
ALTER TABLE post_read
DROP COLUMN id,
ADD PRIMARY KEY (person_id, post_id),
DROP CONSTRAINT post_read_post_id_person_id_key;
ALTER TABLE post_saved
DROP COLUMN id,
ADD PRIMARY KEY (person_id, post_id),
DROP CONSTRAINT post_saved_post_id_person_id_key;
DROP INDEX idx_post_saved_person_id;
ALTER TABLE received_activity
DROP COLUMN id,
ADD PRIMARY KEY (ap_id),
DROP CONSTRAINT received_activity_ap_id_key;
-- Delete duplicates which can exist because of missing `UNIQUE` constraint
DELETE FROM site_aggregates AS a USING (
SELECT
min(id) AS id,
site_id
FROM
site_aggregates
GROUP BY
site_id
HAVING
count(*) > 1) AS b
WHERE
a.site_id = b.site_id
AND a.id != b.id;
ALTER TABLE site_aggregates
DROP COLUMN id,
ADD PRIMARY KEY (site_id);
ALTER TABLE site_language
DROP COLUMN id,
ADD PRIMARY KEY (site_id, language_id),
DROP CONSTRAINT site_language_site_id_language_id_key;
-- Change functions to not use the removed columns
CREATE OR REPLACE FUNCTION site_aggregates_site ()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
-- we only ever want to have a single value in site_aggregate because the site_aggregate triggers update all rows in that table.
-- a cleaner check would be to insert it for the local_site but that would break assumptions at least in the tests
IF (TG_OP = 'INSERT') AND NOT EXISTS (
SELECT
*
FROM
site_aggregates
LIMIT 1) THEN
INSERT INTO site_aggregates (site_id)
VALUES (NEW.id);
ELSIF (TG_OP = 'DELETE') THEN
DELETE FROM site_aggregates
WHERE site_id = OLD.id;
END IF;
RETURN NULL;
END
$$;