From 363c8fabd688ad3ec442a031174cca088fcd7a4d Mon Sep 17 00:00:00 2001 From: CI Bot Date: Sat, 25 Jul 2026 16:56:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(migration):=20=E8=A1=A5=E5=85=85generation?= =?UTF-8?q?=5Ftasks.bgm=5Fconfig=E5=AD=97=E6=AE=B5migration=EF=BC=88#642?= =?UTF-8?q?=E9=81=97=E6=BC=8F=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因:#642 一键生成支持自定义BGM 在 SQLAlchemy 模型中加了 bgm_config 字段, 但遗漏了 alembic migration,导致 staging 环境数据库没有该列, 创建生成任务时报错 500。 新增 052_generation_task_bgm_config.py: - generation_tasks 表新增 bgm_config JSON 列 - 默认值 {} - 幂等升级/降级(先检查列是否存在) --- .../052_generation_task_bgm_config.py | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 alembic/versions/052_generation_task_bgm_config.py diff --git a/alembic/versions/052_generation_task_bgm_config.py b/alembic/versions/052_generation_task_bgm_config.py new file mode 100755 index 000000000..27bc4476e --- /dev/null +++ b/alembic/versions/052_generation_task_bgm_config.py @@ -0,0 +1,64 @@ +"""#642 - 生成任务新增 bgm_config 字段 + +Revision ID: 052_generation_task_bgm_config +Revises: 051_generation_task_resolution +Create Date: 2026-07-25 + +Changes: +1. generation_tasks 表新增 bgm_config 字段(JSON类型),存储用户自定义BGM配置 +2. 为空时使用默认空字典 + +背景: +#642 一键生成支持自定义BGM 功能在 SQLAlchemy 模型中加了 bgm_config 字段, +但遗漏了 alembic migration,导致 staging 环境数据库没有该列, +创建生成任务时直接 500。 +""" + +import sqlalchemy as sa + +from alembic import context, op + +revision = "052_generation_task_bgm_config" +down_revision = "051_generation_task_resolution" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + conn = op.get_bind() + if context.get_context().dialect.name == "postgresql": + # 检查列是否已存在(幂等) + result = conn.execute( + sa.text( + "SELECT column_name FROM information_schema.columns " + "WHERE table_name = 'generation_tasks' AND column_name = 'bgm_config'" + ) + ) + if result.scalar() is not None: + return + + op.add_column( + "generation_tasks", + sa.Column( + "bgm_config", + sa.JSON, + nullable=False, + server_default=sa.text("'{}'::json"), + ), + ) + + +def downgrade() -> None: + conn = op.get_bind() + if context.get_context().dialect.name == "postgresql": + # 检查列是否存在(幂等) + result = conn.execute( + sa.text( + "SELECT column_name FROM information_schema.columns " + "WHERE table_name = 'generation_tasks' AND column_name = 'bgm_config'" + ) + ) + if result.scalar() is None: + return + + op.drop_column("generation_tasks", "bgm_config") -- 2.54.0