From fdfe955a0f55ac20d4a37802a6bd3ae29556c1a2 Mon Sep 17 00:00:00 2001 From: CI Bot Date: Thu, 13 Aug 2026 22:47:59 +0800 Subject: [PATCH 1/2] fix: cover_templates config double-encoded JSON causing 500 Root cause: migration 055 seed data used json.dumps(config) which double-serialized the config dict. SQLAlchemy JSON column already handles serialization, so json.dumps() turned dicts into strings, then JSON column serialized them again into JSON string scalars. Example: {} -> json.dumps -> "{}" -> JSON column -> "\"{}\"" When Pydantic CoverTemplateResponse reads this, it gets a string instead of a dict, causing ValidationError -> 500. Fix: 1. migration 055: remove json.dumps(), pass dict directly 2. migration 056: fix existing data by extracting text from JSON string scalar and casting back to JSON object --- alembic/versions/055_cover_templates.py | 4 +- .../056_fix_cover_templates_config.py | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 alembic/versions/056_fix_cover_templates_config.py 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..ad09665bb --- /dev/null +++ b/alembic/versions/056_fix_cover_templates_config.py @@ -0,0 +1,38 @@ +"""修复 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 -- 2.54.0 From 69edceb2f00655b42e986e0b9c104dbe21faac30 Mon Sep 17 00:00:00 2001 From: CI Bot Date: Thu, 13 Aug 2026 15:36:03 +0000 Subject: [PATCH 2/2] style: auto-format with black + isort + prettier [skip ci-format-check] --- alembic/versions/056_fix_cover_templates_config.py | 1 + 1 file changed, 1 insertion(+) diff --git a/alembic/versions/056_fix_cover_templates_config.py b/alembic/versions/056_fix_cover_templates_config.py index ad09665bb..820dafb9d 100644 --- a/alembic/versions/056_fix_cover_templates_config.py +++ b/alembic/versions/056_fix_cover_templates_config.py @@ -11,6 +11,7 @@ Create Date: 2026-08-13 """ import sqlalchemy as sa + from alembic import op revision = "056_fix_cover_templates_config" -- 2.54.0