kemono2/db/migrations/20230903_00_CHN7so-remake_fanbox_newsletter.py

42 lines
1.4 KiB
Python
Raw Normal View History

2024-07-04 21:57:05 +02:00
"""
Add table complete_imports
"""
from yoyo import step
__depends__ = {"20230827_00_USDf5-add-index-to-revisions"}
steps = [
step(
"""
CREATE TABLE public.fanbox_newsletters_temp_new (
user_id varchar NOT NULL,
hash varchar NOT NULL,
"content" varchar NOT NULL,
added timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
published timestamp NOT NULL,
CONSTRAINT fanbox_newsletters_pkey PRIMARY KEY (user_id, hash)
);
CREATE EXTENSION IF NOT EXISTS pgcrypto;
INSERT INTO public.fanbox_newsletters_temp_new (user_id, "hash", "content", added, published)
SELECT
user_id,
ENCODE(DIGEST(content, 'sha256'), 'hex'),
content,
MIN(added) AS min_added,
MIN(published) AS min_published
FROM public.fanbox_newsletters
GROUP BY user_id, ENCODE(DIGEST(content, 'sha256'), 'hex'), content
ON CONFLICT (user_id, "hash") DO NOTHING;
CREATE INDEX fanbox_newsletters_user_id_published_idx ON public.fanbox_newsletters_temp_new USING btree (user_id , published);
ALTER TABLE fanbox_newsletters RENAME TO fanbox_newsletters_temp_old;
ALTER TABLE fanbox_newsletters_temp_new RENAME TO fanbox_newsletters;
"""
),
]