From 7cea7a938c55c7a7f419e92eec4bf2b59e374c9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=81=B5=E5=BA=94?= Date: Tue, 15 Sep 2026 17:19:21 +0800 Subject: [PATCH] fix(#1894): add alembic migration 077 for title fields auto-deploy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 编程Agent 只写了根目录 migrations/009.sql(手动执行),但没有 alembic 迁移文件, staging/生产部署时 Watchtower 不会自动执行根目录 SQL,导致 title_text/title_category/title_config 三个列缺失、脚本报错。补 alembic 077 迁移确保部署时自动建列+数据迁移。 同时给 migrations/009.sql 加上 IF NOT EXISTS 保护和幂等 INSERT 防止重复执行报错。 --- .../077_merge_title_library_into_scripts.py | 66 +++++++++++++++++++ .../009_merge_title_library_into_scripts.sql | 19 ++++-- 2 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 alembic/versions/077_merge_title_library_into_scripts.py diff --git a/alembic/versions/077_merge_title_library_into_scripts.py b/alembic/versions/077_merge_title_library_into_scripts.py new file mode 100644 index 000000000..e5e6f706c --- /dev/null +++ b/alembic/versions/077_merge_title_library_into_scripts.py @@ -0,0 +1,66 @@ +"""#1894: merge title_library into scripts — add title_text/title_category/title_config to scripts + +Revision ID: 077_merge_title_library_into_scripts +Revises: 076_membership_points +Create Date: 2026-09-15 +""" + +import sqlalchemy as sa +from alembic import op + +revision = "077_merge_title_library_into_scripts" +down_revision = "076_membership_points" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # scripts 表新增标题配置三字段 + with op.batch_alter_table("scripts") as batch: + batch.add_column( + sa.Column("title_text", sa.String(500), nullable=False, server_default=sa.text("''")), + ) + batch.add_column( + sa.Column("title_category", sa.String(50), nullable=False, server_default=sa.text("''")), + ) + batch.add_column( + sa.Column("title_config", sa.JSON(), nullable=False, server_default=sa.text("'{}'::jsonb")), + ) + + # 从 title_libraries 迁移活跃数据到 scripts(若 title_libraries 表存在) + conn = op.get_bind() + exists = conn.execute( + sa.text("SELECT to_regclass('public.title_libraries') IS NOT NULL") + ).scalar() + if exists: + conn.execute(sa.text(""" + INSERT INTO scripts (id, user_id, title, content, segments, tags, title_text, title_category, title_config, created_at, updated_at) + SELECT + gen_random_uuid()::TEXT, + tl.user_id, + COALESCE(tl.name, '迁移标题'), + COALESCE(tl.text, ''), + '[]'::JSONB, + COALESCE(tl.tags, '[]'::JSONB), + COALESCE(tl.text, ''), + COALESCE(tl.category, ''), + COALESCE(tl.title_config, '{}'::JSONB), + tl.created_at, + tl.updated_at + FROM title_libraries tl + WHERE tl.is_active = true + AND NOT EXISTS ( + SELECT 1 FROM scripts s + WHERE s.user_id = tl.user_id + AND s.title_text = COALESCE(tl.text, '') + AND s.title_category = COALESCE(tl.category, '') + AND s.created_at = tl.created_at + ) + """)) + + +def downgrade() -> None: + with op.batch_alter_table("scripts") as batch: + batch.drop_column("title_config") + batch.drop_column("title_category") + batch.drop_column("title_text") diff --git a/migrations/009_merge_title_library_into_scripts.sql b/migrations/009_merge_title_library_into_scripts.sql index 8c616b620..26b069dab 100644 --- a/migrations/009_merge_title_library_into_scripts.sql +++ b/migrations/009_merge_title_library_into_scripts.sql @@ -1,14 +1,14 @@ -- 009: 废弃标题库整合到文案库 (Issue #1894) -- 为 scripts 表新增标题相关字段,从 title_libraries 迁移数据 -- 日期: 2026-09-15 +-- 注意:此文件为手动执行 SQL,正式部署通过 alembic 版本 077 自动迁移(含 IF NOT EXISTS 幂等保护) -- 1. scripts 表新增字段 -ALTER TABLE scripts ADD COLUMN title_text VARCHAR(500) NOT NULL DEFAULT ''; -ALTER TABLE scripts ADD COLUMN title_category VARCHAR(50) NOT NULL DEFAULT ''; -ALTER TABLE scripts ADD COLUMN title_config JSONB NOT NULL DEFAULT '{}'; +ALTER TABLE scripts ADD COLUMN IF NOT EXISTS title_text VARCHAR(500) NOT NULL DEFAULT ''; +ALTER TABLE scripts ADD COLUMN IF NOT EXISTS title_category VARCHAR(50) NOT NULL DEFAULT ''; +ALTER TABLE scripts ADD COLUMN IF NOT EXISTS title_config JSONB NOT NULL DEFAULT '{}'; --- 2. 将 title_libraries 中活跃数据迁移到 scripts 表 --- 每个用户的每条 title_library 记录,创建一条对应的 script 记录 +-- 2. 将 title_libraries 中活跃数据迁移到 scripts 表(幂等:已迁移过的跳过) INSERT INTO scripts (id, user_id, title, content, segments, tags, title_text, title_category, title_config, created_at, updated_at) SELECT gen_random_uuid()::TEXT, @@ -23,4 +23,11 @@ SELECT tl.created_at, tl.updated_at FROM title_libraries tl -WHERE tl.is_active = true; +WHERE tl.is_active = true + AND NOT EXISTS ( + SELECT 1 FROM scripts s + WHERE s.user_id = tl.user_id + AND s.title_text = COALESCE(tl.text, '') + AND s.title_category = COALESCE(tl.category, '') + AND s.created_at = tl.created_at + );