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")