feat(db): C3 langgraph 检查点建表迁移 + alembic 漂移豁免

多章工作流链(docs/design/chain-workflow.md §4/§7)唯一新增 DDL:langgraph
检查点四表(checkpoints/checkpoint_blobs/checkpoint_writes/checkpoint_migrations)。

- 新增迁移 d3e4f5a6b7c8:在迁移内调 langgraph 同步 PostgresSaver.setup() 建表
  (DDL 只在 migrations/CI,绝不 app-runtime)。从 database_url 派生 psycopg
  conn string(去 +asyncpg),与 alembic 的 asyncpg 连接解耦。
- §11 风险落地:setup() 跑 CREATE INDEX CONCURRENTLY,不能在事务块内且会与
  alembic 开着的事务死锁 → 用 op.get_context().autocommit_block() 先提交退出
  事务块再建表。downgrade 幂等 DROP ... CASCADE。
- env.py 加 include_object:langgraph 四表不在 Base.metadata,过滤掉以免
  alembic check 误报漂移。
- jobs 表零迁移(C2 判定:result.awaiting_chapter 表达待裁决,复用现有状态枚举)。

验证:alembic upgrade head 应用、downgrade/re-upgrade 往返、alembic check
无漂移;ruff/format/mypy(packages/db) 全绿。
This commit is contained in:
Yaojia Wang
2026-06-23 17:23:06 +02:00
parent 29349dc7ee
commit 548b7abd6f
2 changed files with 94 additions and 1 deletions

View File

@@ -20,9 +20,27 @@ if config.config_file_name is not None:
config.set_main_option("sqlalchemy.url", get_settings().database_url)
target_metadata = Base.metadata
# langgraph 检查点表由 langgraph 自管 schema迁移 d3e4f5a6b7c8 调其 setup() 建表),
# 不在 Base.metadata。autogenerate/check 须忽略它们,否则会误报"待 drop"漂移。
_LANGGRAPH_TABLES = frozenset(
{"checkpoints", "checkpoint_blobs", "checkpoint_writes", "checkpoint_migrations"}
)
def include_object(
obj: object, name: str | None, type_: str, reflected: bool, compare_to: object
) -> bool:
if type_ == "table" and name in _LANGGRAPH_TABLES:
return False
return True
def do_run_migrations(connection: Connection) -> None:
context.configure(connection=connection, target_metadata=target_metadata)
context.configure(
connection=connection,
target_metadata=target_metadata,
include_object=include_object,
)
with context.begin_transaction():
context.run_migrations()