kemono2/db/migrations/20231014_00_MU90C3-match-things-to-prod.py
2024-07-04 22:08:17 +02:00

117 lines
6.5 KiB
Python

"""
Mock this migration of changes that are already in production
"""
from yoyo import step
__depends__ = {"20230930_00_TNd34a-add-created-timestamp-to-favs"}
steps = [
step(
"""
DROP INDEX IF EXISTS public.dm_search_idx;
DROP INDEX IF EXISTS public.search_idx;
"""
),
step(
"""
ALTER TABLE public.account_artist_favorite DROP CONSTRAINT IF EXISTS account_artist_favorite_pkey;
ALTER TABLE public.account_artist_favorite ADD CONSTRAINT account_artist_favorite_pkey PRIMARY KEY (service, id);
"""
),
step(
"""
ALTER TABLE public.account_post_favorite DROP CONSTRAINT IF EXISTS account_post_favorite_pkey;
ALTER TABLE public.account_post_favorite ADD CONSTRAINT account_post_favorite_pkey PRIMARY KEY (service, id);
"""
),
]
"""
/* In production we have these partitioned like this */
BEGIN;
ALTER SEQUENCE IF EXISTS account_artist_favorite_id_seq RENAME TO TBD_account_artist_favorite_id_seq;
ALTER SEQUENCE IF EXISTS account_post_favorite_id_seq RENAME TO TBD_account_post_favorite_id;
ALTER TABLE account_artist_favorite RENAME TO TBD_account_artist_favorite;
ALTER TABLE account_post_favorite RENAME TO TBD_account_post_favorite;
ALTER TABLE TBD_account_artist_favorite RENAME CONSTRAINT account_artist_favorite_account_id_fkey TO TBD_account_artist_favorite_account_id_fkey;
ALTER TABLE TBD_account_post_favorite RENAME CONSTRAINT account_post_favorite_account_id_fkey TO TBD_account_post_favorite_account_id_fkey;
ALTER INDEX IF EXISTS account_post_favorite_pkey RENAME TO TBD_account_post_favorite_pkey;
ALTER INDEX IF EXISTS account_post_favorite_account_id_service_artist_id_post_id_key RENAME TO TBD_account_post_favorite_account_id_service_artist_id_post_id_key;
ALTER INDEX IF EXISTS account_post_favorite_service_artist_id_post_id_idx RENAME TO TBD_account_post_favorite_service_artist_id_post_id_idx;
ALTER INDEX IF EXISTS account_artist_favorite_pkey RENAME TO TBD_account_artist_favorite_pkey;
ALTER INDEX IF EXISTS account_artist_favorite_account_id_service_artist_id_key RENAME TO TBD_account_artist_favorite_account_id_service_artist_id_key;
ALTER INDEX IF EXISTS account_artist_favorite_service_artist_id_idx RENAME TO TBD_account_artist_favorite_service_artist_id_idx;
create table account_artist_favorite (
id serial,
account_id int not null references account(id),
service varchar not null,
artist_id varchar not null,
unique (account_id, service, artist_id),
PRIMARY KEY(service, id)
) PARTITION BY LIST (service);
create table account_post_favorite (
id serial,
account_id int not null references account(id),
service varchar not null,
artist_id varchar not null,
post_id varchar not null,
unique (account_id, service, artist_id, post_id),
PRIMARY KEY(service, id)
) PARTITION BY LIST (service);
CREATE INDEX account_artist_favorite_service_artist_id_idx ON account_artist_favorite USING btree (service, artist_id);
CREATE INDEX account_post_favorite_service_artist_id_post_id_idx ON account_post_favorite USING btree (service, artist_id, post_id);
CREATE TABLE account_artist_favorite_DEFAULT PARTITION OF account_artist_favorite DEFAULT;
CREATE TABLE account_artist_favorite_patreon PARTITION OF account_artist_favorite FOR VALUES IN ('patreon');
CREATE TABLE account_artist_favorite_fantia PARTITION OF account_artist_favorite FOR VALUES IN ('fantia');
CREATE TABLE account_artist_favorite_fanbox PARTITION OF account_artist_favorite FOR VALUES IN ('fanbox');
CREATE TABLE account_artist_favorite_gumroad PARTITION OF account_artist_favorite FOR VALUES IN ('gumroad');
CREATE TABLE account_artist_favorite_dlsite PARTITION OF account_artist_favorite FOR VALUES IN ('dlsite');
CREATE TABLE account_artist_favorite_discord PARTITION OF account_artist_favorite FOR VALUES IN ('discord');
CREATE TABLE account_artist_favorite_afdian PARTITION OF account_artist_favorite FOR VALUES IN ('afdian');
CREATE TABLE account_artist_favorite_boosty PARTITION OF account_artist_favorite FOR VALUES IN ('boosty');
CREATE TABLE account_artist_favorite_subscribestar PARTITION OF account_artist_favorite FOR VALUES IN ('subscribestar');
CREATE TABLE account_post_favorite_DEFAULT PARTITION OF account_post_favorite DEFAULT;
CREATE TABLE account_post_favorite_patreon PARTITION OF account_post_favorite FOR VALUES IN ('patreon');
CREATE TABLE account_post_favorite_fantia PARTITION OF account_post_favorite FOR VALUES IN ('fantia');
CREATE TABLE account_post_favorite_fanbox PARTITION OF account_post_favorite FOR VALUES IN ('fanbox');
CREATE TABLE account_post_favorite_gumroad PARTITION OF account_post_favorite FOR VALUES IN ('gumroad');
CREATE TABLE account_post_favorite_dlsite PARTITION OF account_post_favorite FOR VALUES IN ('dlsite');
CREATE TABLE account_post_favorite_discord PARTITION OF account_post_favorite FOR VALUES IN ('discord');
CREATE TABLE account_post_favorite_afdian PARTITION OF account_post_favorite FOR VALUES IN ('afdian');
CREATE TABLE account_post_favorite_boosty PARTITION OF account_post_favorite FOR VALUES IN ('boosty');
CREATE TABLE account_post_favorite_subscribestar PARTITION OF account_post_favorite FOR VALUES IN ('subscribestar');
INSERT INTO account_artist_favorite (account_id, service, artist_id) SELECT account_id, service, artist_id FROM TBD_account_artist_favorite;
INSERT INTO account_post_favorite (account_id, service, artist_id, post_id) SELECT account_id, service, artist_id, post_id FROM TBD_account_post_favorite;
DROP TABLE IF EXISTS TBD_account_artist_favorite;
DROP TABLE IF EXISTS TBD_account_post_favorite;
COMMIT;
ANALYZE account_artist_favorite_DEFAULT, account_artist_favorite_patreon, account_artist_favorite_fantia, account_artist_favorite_fanbox, account_artist_favorite_gumroad, account_artist_favorite_gumroad, account_artist_favorite_dlsite, account_artist_favorite_discord, account_artist_favorite_afdian, account_artist_favorite_boosty, account_artist_favorite_subscribestar;
ANALYZE account_post_favorite_DEFAULT, account_post_favorite_patreon, account_post_favorite_fantia, account_post_favorite_fanbox, account_post_favorite_gumroad, account_post_favorite_gumroad, account_post_favorite_dlsite, account_post_favorite_discord, account_post_favorite_afdian, account_post_favorite_boosty, account_post_favorite_subscribestar;
COPY distributors FROM 'input_file';
SELECT setval('serial', max(id)) FROM distributors;
INSERT INTO account_artist_favorite (account_id, service, artist_id) SELECT account_id, service, artist_id FROM account_artist_favorite_orig;
"""