From 11a56cf44e8cde1bb6e0c1406beef116fad3fce3 Mon Sep 17 00:00:00 2001 From: DiheChen Date: Fri, 30 Oct 2026 01:56:11 +0800 Subject: [PATCH] refactor: consolidate database migrations into baseline --- db/migrations/00001_schema.sql | 18 +++++++++++++++--- db/migrations/00002_page_url.sql | 5 ----- db/migrations/00003_drop_kuwo_id.sql | 2 -- db/migrations/00004_room_api_keys.sql | 11 ----------- db/migrations/00005_rotation.sql | 6 ------ db/migrations/00006_api_key_hash_only.sql | 4 ---- db/migrations/00007_site.sql | 6 ------ 7 files changed, 15 insertions(+), 37 deletions(-) delete mode 100644 db/migrations/00002_page_url.sql delete mode 100644 db/migrations/00003_drop_kuwo_id.sql delete mode 100644 db/migrations/00004_room_api_keys.sql delete mode 100644 db/migrations/00005_rotation.sql delete mode 100644 db/migrations/00006_api_key_hash_only.sql delete mode 100644 db/migrations/00007_site.sql diff --git a/db/migrations/00001_schema.sql b/db/migrations/00001_schema.sql index 2bbb181..3f3f4a7 100644 --- a/db/migrations/00001_schema.sql +++ b/db/migrations/00001_schema.sql @@ -17,9 +17,12 @@ CREATE TABLE rooms ( password_hash text, -- 接管房间时加 1;落盘以它为条件,因此过期的 owner 无法写入。 term bigint NOT NULL DEFAULT 0, - play_mode text NOT NULL DEFAULT 'sequential' CHECK (play_mode IN ('sequential', 'random')), + play_mode text NOT NULL DEFAULT 'sequential' CHECK (play_mode IN ('sequential', 'random', 'rotation')), entry_seq bigint NOT NULL DEFAULT 0, - created_at timestamptz NOT NULL DEFAULT now() + created_at timestamptz NOT NULL DEFAULT now(), + -- 第一次生成 API Key 时创建,此后重置 Key、修改密码都不换。 + bot_member_id uuid REFERENCES members, + CHECK (bot_member_id IS NULL OR kind = 'persistent') ); CREATE TABLE room_entries ( @@ -42,6 +45,9 @@ CREATE TABLE room_entries ( likes integer NOT NULL, added_at timestamptz NOT NULL, started_at timestamptz CHECK ((slot = 'current') = (started_at IS NOT NULL)), + page_url text NOT NULL, + -- 条目的轮次在入队时定下,轮流播放时点赞数相同的歌按它排列。 + round bigint NOT NULL DEFAULT 0, PRIMARY KEY (room_id, entry_id) ); @@ -68,12 +74,12 @@ CREATE TABLE tracks ( duration_ms bigint NOT NULL, gain double precision NOT NULL, lyric text, - kuwo_id text NOT NULL, media_key text, media_size bigint, prepared_at timestamptz NOT NULL DEFAULT now(), -- 临时房间的播放记录随房间删除,所以不从 plays 计算。 last_played_at timestamptz, + page_url text NOT NULL, PRIMARY KEY (source, track_id) ); @@ -98,3 +104,9 @@ CREATE TABLE credentials ( value text NOT NULL, updated_at timestamptz NOT NULL DEFAULT now() ); + +-- 至多一行;没有这一行时品牌名是默认的 Alisten。 +CREATE TABLE site ( + id boolean PRIMARY KEY DEFAULT true CHECK (id), + brand_name text NOT NULL +); diff --git a/db/migrations/00002_page_url.sql b/db/migrations/00002_page_url.sql deleted file mode 100644 index 0153913..0000000 --- a/db/migrations/00002_page_url.sql +++ /dev/null @@ -1,5 +0,0 @@ --- +goose Up -ALTER TABLE room_entries ADD COLUMN page_url text NOT NULL DEFAULT ''; -ALTER TABLE room_entries ALTER COLUMN page_url DROP DEFAULT; -ALTER TABLE tracks ADD COLUMN page_url text NOT NULL DEFAULT ''; -ALTER TABLE tracks ALTER COLUMN page_url DROP DEFAULT; diff --git a/db/migrations/00003_drop_kuwo_id.sql b/db/migrations/00003_drop_kuwo_id.sql deleted file mode 100644 index 96fd7f7..0000000 --- a/db/migrations/00003_drop_kuwo_id.sql +++ /dev/null @@ -1,2 +0,0 @@ --- +goose Up -ALTER TABLE tracks DROP COLUMN kuwo_id; diff --git a/db/migrations/00004_room_api_keys.sql b/db/migrations/00004_room_api_keys.sql deleted file mode 100644 index 4d868e5..0000000 --- a/db/migrations/00004_room_api_keys.sql +++ /dev/null @@ -1,11 +0,0 @@ --- +goose Up --- 第一次生成 API Key 时创建,此后重置 Key、修改密码都不换。 -ALTER TABLE rooms - ADD COLUMN bot_member_id uuid REFERENCES members, - ADD CHECK (bot_member_id IS NULL OR kind = 'persistent'); - --- 凭证只存哈希的唯一例外:API Key 要能随时给进过房间的成员和管理员查看。Key 就是 bot 成员的票据,票据作废时明文随之删除。 -CREATE TABLE room_api_keys ( - token_hash bytea PRIMARY KEY REFERENCES room_tickets ON DELETE CASCADE, - key text NOT NULL -); diff --git a/db/migrations/00005_rotation.sql b/db/migrations/00005_rotation.sql deleted file mode 100644 index 9382ef2..0000000 --- a/db/migrations/00005_rotation.sql +++ /dev/null @@ -1,6 +0,0 @@ --- +goose Up --- 条目的轮次在入队时定下,轮流播放时点赞数相同的歌按它排列;已有的条目都算第 0 轮。 -ALTER TABLE room_entries ADD COLUMN round bigint NOT NULL DEFAULT 0; -ALTER TABLE rooms - DROP CONSTRAINT rooms_play_mode_check, - ADD CONSTRAINT rooms_play_mode_check CHECK (play_mode IN ('sequential', 'random', 'rotation')); diff --git a/db/migrations/00006_api_key_hash_only.sql b/db/migrations/00006_api_key_hash_only.sql deleted file mode 100644 index 50a396e..0000000 --- a/db/migrations/00006_api_key_hash_only.sql +++ /dev/null @@ -1,4 +0,0 @@ --- +goose Up --- 已有的 Key 都给进过房间的成员看过,一起作废;bot 成员留给重新生成的 Key。 -DROP TABLE room_api_keys; -DELETE FROM room_tickets t USING rooms r WHERE t.room_id = r.id AND t.member_id = r.bot_member_id; diff --git a/db/migrations/00007_site.sql b/db/migrations/00007_site.sql deleted file mode 100644 index 05789da..0000000 --- a/db/migrations/00007_site.sql +++ /dev/null @@ -1,6 +0,0 @@ --- +goose Up --- 至多一行;没有这一行时品牌名是默认的 Alisten。 -CREATE TABLE site ( - id boolean PRIMARY KEY DEFAULT true CHECK (id), - brand_name text NOT NULL -);