diff --git a/alembic/versions/055_cover_templates.py b/alembic/versions/055_cover_templates.py index f126c255a..9c4e8c19c 100644 --- a/alembic/versions/055_cover_templates.py +++ b/alembic/versions/055_cover_templates.py @@ -10,8 +10,6 @@ Changes: 3. config 为 JSON 字段,存储封面配置信息 """ -import json - import sqlalchemy as sa from alembic import context, op @@ -73,7 +71,7 @@ def upgrade() -> None: name=name, thumbnail_url="", is_system=True, - config=json.dumps(config), + config=config, created_at=sa.func.now(), updated_at=sa.func.now(), ) diff --git a/alembic/versions/056_fix_cover_templates_config.py b/alembic/versions/056_fix_cover_templates_config.py new file mode 100644 index 000000000..820dafb9d --- /dev/null +++ b/alembic/versions/056_fix_cover_templates_config.py @@ -0,0 +1,39 @@ +"""修复 cover_templates.config 双重序列化 + +Revision ID: 056_fix_cover_templates_config +Revises: 055_cover_templates +Create Date: 2026-08-13 + +问题: 055 迁移 seed 数据时 json.dumps(config) 导致 config 被双重序列化为 JSON 字符串 +例如 "{}"(字符串)而不是 {}(对象),导致 Pydantic CoverTemplateResponse 校验失败 500。 + +修复: 从 JSON 字符串中提取文本值,再 cast 回 json 对象类型。 +""" + +import sqlalchemy as sa + +from alembic import op + +revision = "056_fix_cover_templates_config" +down_revision = "055_cover_templates" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + conn = op.get_bind() + + # PostgreSQL: 从 JSON string scalar 中提取文本内容,cast 为 json object + # 例如: JSON string "{}" -> text "{}" -> JSON object {} + if conn.dialect.name == "postgresql": + conn.execute( + sa.text( + "UPDATE cover_templates SET config = (config#>>'{}')::json " + "WHERE jsonb_typeof(config::jsonb) = 'string'" + ) + ) + + +def downgrade() -> None: + # No safe rollback — the original data was incorrect + pass