chore(backend): list_order迁移补齐+Task14类型修正落链+moments每页日志+MsgTypeParam派生+3处文档修正+Dockerfile打包migrations+DEPLOY补迁移步骤
- 9b0d0c133035: wechat_account.list_order 进迁移链(此前仅模型声明, 干净库 /api/accounts 即 500);sa.inspect 幂等守卫兼容已手动加列的 smoke.db - fe63c6789e94(验证中发现的预存缺口,超出原清单): Task 14 的 5 列模型 类型修正(4×URL String(1024)→Text, db_size Integer→BigInteger)从未落 链——纯 alembic 新库会复活 >1KB 链接截断;对齐后干净库 alembic check 全绿。SQLite message 表 batch 重建会掉 FTS 触发器+rowid 脱节(docstring 记明 ensure_fts+rebuild_fts 修复,smoke.db 已修复验证) - scrape_via_page: _iter_moments_pages 每页一行进度日志(镜像 _fetch_pages) - messages: MsgTypeParam=Literal[tuple(_MSG_TYPE_FILTER)] 派生,杜绝 双处罗列漂移(openapi enum 验证一致,492 绿含 422 行为) - 文档: ChatGroup 复合PK docstring/groups room_id 多行表述/测试名去 单行假设;Dockerfile 补 alembic.ini+migrations;DEPLOY 增「数据库 迁移」节(新库/旧库stamp路径/回填脚本显式DB_URL)+运维表升级行 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
"""wechat_account 补 list_order 列
|
||||
|
||||
Revision ID: 9b0d0c133035
|
||||
Revises: 099ac2097fa2
|
||||
Create Date: 2026-08-25 20:16:46.324537
|
||||
|
||||
内容
|
||||
----
|
||||
补齐模型-迁移 drift:``wechat_account.list_order``(云客 wxUser/list 返回
|
||||
顺序,2026-08-24 加进模型)一直没有对应迁移——干净库(create_all 或纯
|
||||
alembic 链)上模型建表含该列、迁移链建表不含,``/api/accounts`` SELECT
|
||||
即 500(实测两次,Task 11/12 验证时被迫对 smoke.db 手动 ALTER)。
|
||||
|
||||
幂等:smoke.db 已被手动加过该列(其 alembic_version 仍指旧 head),直接
|
||||
add_column 会报 duplicate column——用 ``sa.inspect`` 先查列是否存在,
|
||||
存在则跳过(SQLite/MySQL 通用,不需要裸 PRAGMA/information_schema)。
|
||||
downgrade 对称:列不存在时跳过。
|
||||
"""
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '9b0d0c133035'
|
||||
down_revision: str | Sequence[str] | None = '099ac2097fa2'
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def _has_column(table: str, column: str) -> bool:
|
||||
insp = sa.inspect(op.get_bind())
|
||||
return column in {c["name"] for c in insp.get_columns(table)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
if _has_column("wechat_account", "list_order"):
|
||||
return # 已手动加过(smoke.db),幂等跳过
|
||||
op.add_column("wechat_account", sa.Column("list_order", sa.Integer(), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
if not _has_column("wechat_account", "list_order"):
|
||||
return
|
||||
op.drop_column("wechat_account", "list_order")
|
||||
@@ -0,0 +1,115 @@
|
||||
"""Task 14 模型类型修正落进迁移链(URL Text + db_size BigInteger)
|
||||
|
||||
Revision ID: fe63c6789e94
|
||||
Revises: 9b0d0c133035
|
||||
Create Date: 2026-08-25 20:34:26.646844
|
||||
|
||||
内容
|
||||
----
|
||||
补齐模型-迁移 drift:2026-08-22 部署实战(commit 00ae13b)在**模型**上改了
|
||||
5 列类型,但迁移链从未跟上——
|
||||
|
||||
* message.file_url / file_th_url / file_hd_url、media.original_url:
|
||||
String(1024) → Text(分享链接可超 1KB,MySQL VARCHAR(1024) 会截断);
|
||||
* wechat_account.db_size: Integer → BigInteger(字节数可超 32 位 INT,
|
||||
实测 2.17GB)。
|
||||
|
||||
后果:纯 alembic 链建的干净库(deploy 文档的新库路径)拿到旧类型,静默
|
||||
复活已修掉的截断问题;``alembic check`` 常红。本迁移把链对齐到模型。
|
||||
|
||||
幂等性:ALTER MODIFY 到相同类型是合法 SQL——MySQL 上 Docker 库的表由
|
||||
migrate_sqlite_to_mysql(按当时模型 create_all,已是 Text/BIGINT)创建,
|
||||
重放本迁移为无害 no-op,无需条件守卫。SQLite 走 batch_alter_table 表重建
|
||||
(仅改声明类型,TEXT/INTEGER 亲和性不变,数据原样拷贝)。
|
||||
|
||||
⚠️ SQLite 侧副作用:message 表重建会丢其上的 FTS5 同步触发器、且
|
||||
external-content 索引(message_fts 按 rowid 映射)会与新表 rowid 脱节。
|
||||
MySQL(prod)无 FTS 不受影响;SQLite dev 库跑完本迁移后需执行
|
||||
``ensure_fts``(重建触发器)+ ``rebuild_fts``(按新 rowid 重灌索引,
|
||||
app/db/fts.py 现成函数)修复搜索。
|
||||
"""
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'fe63c6789e94'
|
||||
down_revision: str | Sequence[str] | None = '9b0d0c133035'
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
with op.batch_alter_table("message") as batch_op:
|
||||
batch_op.alter_column(
|
||||
"file_url",
|
||||
existing_type=sa.String(1024),
|
||||
type_=sa.Text(),
|
||||
existing_nullable=True,
|
||||
)
|
||||
batch_op.alter_column(
|
||||
"file_th_url",
|
||||
existing_type=sa.String(1024),
|
||||
type_=sa.Text(),
|
||||
existing_nullable=True,
|
||||
)
|
||||
batch_op.alter_column(
|
||||
"file_hd_url",
|
||||
existing_type=sa.String(1024),
|
||||
type_=sa.Text(),
|
||||
existing_nullable=True,
|
||||
)
|
||||
with op.batch_alter_table("media") as batch_op:
|
||||
batch_op.alter_column(
|
||||
"original_url",
|
||||
existing_type=sa.String(1024),
|
||||
type_=sa.Text(),
|
||||
existing_nullable=True,
|
||||
)
|
||||
with op.batch_alter_table("wechat_account") as batch_op:
|
||||
batch_op.alter_column(
|
||||
"db_size",
|
||||
existing_type=sa.Integer(),
|
||||
type_=sa.BigInteger(),
|
||||
existing_nullable=True,
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
with op.batch_alter_table("wechat_account") as batch_op:
|
||||
batch_op.alter_column(
|
||||
"db_size",
|
||||
existing_type=sa.BigInteger(),
|
||||
type_=sa.Integer(),
|
||||
existing_nullable=True,
|
||||
)
|
||||
with op.batch_alter_table("media") as batch_op:
|
||||
batch_op.alter_column(
|
||||
"original_url",
|
||||
existing_type=sa.Text(),
|
||||
type_=sa.String(1024),
|
||||
existing_nullable=True,
|
||||
)
|
||||
with op.batch_alter_table("message") as batch_op:
|
||||
batch_op.alter_column(
|
||||
"file_hd_url",
|
||||
existing_type=sa.Text(),
|
||||
type_=sa.String(1024),
|
||||
existing_nullable=True,
|
||||
)
|
||||
batch_op.alter_column(
|
||||
"file_th_url",
|
||||
existing_type=sa.Text(),
|
||||
type_=sa.String(1024),
|
||||
existing_nullable=True,
|
||||
)
|
||||
batch_op.alter_column(
|
||||
"file_url",
|
||||
existing_type=sa.Text(),
|
||||
type_=sa.String(1024),
|
||||
existing_nullable=True,
|
||||
)
|
||||
Reference in New Issue
Block a user