kemono2/db/migrations/20240211_00_yto88-discord-channel-table-and-discord-posts-revisions.py

73 lines
2.0 KiB
Python
Raw Normal View History

2024-07-04 21:57:05 +02:00
from yoyo import step
__depends__ = {'20240210_00_gto81-add-table-for-tracking-forced-reimports'}
steps = [
step("""
CREATE TABLE public.discord_channels (
channel_id text NOT NULL,
server_id text NOT NULL,
name text NOT NULL,
parent_channel_id text NULL,
topic text NULL,
theme_color text NULL,
is_nsfw bool NOT NULL,
position int NOT NULL DEFAULT 0,
icon_emoji text null,
type int NOT NULL DEFAULT 0,
CONSTRAINT discord_channels_pkey PRIMARY KEY (channel_id)
);
CREATE INDEX discord_channels_server_id_idx ON public.discord_channels USING btree (server_id);
CREATE INDEX discord_channels_parent_channel_id_idx ON public.discord_channels USING btree (parent_channel_id);
"""),
step("""
CREATE TABLE public.discord_posts_revisions (
revision_id serial4 NOT NULL,
id varchar(255) NOT NULL,
author jsonb NOT NULL,
"server" varchar(255) NOT NULL,
channel varchar(255) NOT NULL,
"content" text NOT NULL DEFAULT ''::text,
added timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
published timestamp NULL,
edited timestamp NULL,
embeds _jsonb NOT NULL,
mentions _jsonb NOT NULL,
attachments _jsonb NOT NULL,
CONSTRAINT discord_posts_revisions_pkey PRIMARY KEY (revision_id)
);
CREATE INDEX discord_posts_revisions_id_idx ON public.discord_posts_revisions USING btree (id);
"""),
step("""
INSERT INTO public.discord_channels (channel_id, server_id, "name", is_nsfw, "position", "type")
SELECT DISTINCT
channel AS channel_id,
"server",
channel AS "name",
false AS is_nsfw,
0 AS "position",
0 AS "type"
FROM
public.discord_posts
ON CONFLICT DO NOTHING;
UPDATE public.discord_channels AS dc
SET "name" = l."name"
FROM public.lookup AS l
WHERE l.id = dc.channel_id
AND l.service = 'discord-channel';
DELETE FROM public.lookup
WHERE service = 'discord-channel';
UPDATE public.lookup AS l
SET updated = COALESCE((
SELECT MAX(added)
FROM public.discord_posts AS d
WHERE d."server" = l.id
), updated)
WHERE l.service = 'discord';
"""),
]