feat(freight-rpa): 批次R解耦编排层并切换 onhand/inbound 契约

将 RPA 编排收编至 freight_rpa,回调迁 /api/freight-rpa/*。
共享目录与 Worker 契约一次切换至 onhand/inbound/upload,
并补 outbound 骨架与 tt16 排程 job key 迁移。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-10-17 23:18:48 +08:00
1 parent bf3154033b
commit ffc0d4a430
147 files changed
+4221 -2678

No files matched your search

@@ -0,0 +1,134 @@
"""tt16c7d8e9f0a1b2 排程 job key 入站化迁移契约测试。
用途:锁定 tt16 迁移两侧契约——schedule_tasks.platform_api_path 三键更名
(shipping-inventory-rpa→freight-rpa-onhand、freight-receipt-rpa→freight-rpa-inbound、
freight-receipt-rpa-retention→freight-rpa-inbound-retention,随代码同窗口发布),
并锁定迁移链挂接当前单 head(tt16a7b8c9d0e1f2,其上游为 tt15b6a7b8c9d0)。
依赖:migrations/versions(_load 手法沿用 tests/services/in_transit_transfer/
test_migration_inbound_rename.py 的 tt14 契约测试)、app_ctx(根 conftest
app fixture 上下文别名)。
逻辑:revision 链断言 + SQLite 真实 UPDATE upgrade/downgrade 往返
(UPDATE 语句 SQLite/PG 通用,迁移语义以 PG 为准)。
Fix round 1:桩表列名由计划骨架误写的 job_key 修正为真实列 platform_api_path
(models/schedule.py:34,排程表无独立 job_key 列),并追加无关 key 对照行与
往返前后命中计数断言,确保 UPDATE 真实生效且 WHERE 谓词不误伤其他 key。
"""
from __future__ import annotations
import importlib.util
from pathlib import Path
import pytest
import sqlalchemy as sa
from alembic.migration import MigrationContext
from alembic.operations import Operations
pytestmark = pytest.mark.pure_unit
MIGRATIONS_DIR = Path(__file__).resolve().parents[1] / "migrations" / "versions"
# 与 tt16._MAPPING 同源的三键对照(升级方向)。
_MAPPING = {
"shipping-inventory-rpa": "freight-rpa-onhand",
"freight-receipt-rpa": "freight-rpa-inbound",
"freight-receipt-rpa-retention": "freight-rpa-inbound-retention",
}
# 无关对照 key:全程不得被 UPDATE 触碰(验证 WHERE 谓词精确性)。
_UNRELATED_KEY = "faq-aggregate-hot"
@pytest.fixture
def app_ctx(app):
"""根 conftest app fixture 的语义别名:app fixture 产出时已处于 app_context 内。"""
return app
def _load(revision_prefix: str):
for path in sorted(MIGRATIONS_DIR.glob(f"{revision_prefix}*.py")):
spec = importlib.util.spec_from_file_location(path.stem, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
raise AssertionError(f"migration {revision_prefix} not found")
def test_revision_chains_from_tt15():
"""tt16 必须单头挂接:直接前驱为 tt16a7b8c9d0e1f2,链上溯两跳即 tt15b6a7b8c9d0。
[断言意图] 实施时 tt15 已被调拨域迁移 tt16a7b8c9d0e1f2 占用为前驱;
本迁移若再直挂 tt15 会产生双 head,破坏 flask db upgrade,故锁两级链。
"""
migration = _load("tt16c7d8e9f0a1b2")
assert migration.down_revision == "tt16a7b8c9d0e1f2"
parent = _load(migration.down_revision)
assert parent.down_revision == "tt15b6a7b8c9d0"
def test_job_key_upgrade_downgrade(app_ctx):
"""SQLite 往返:三旧行 upgrade 后全量换新 key,downgrade 精确还原旧行。
[断言意图] 桩表用真实列名 platform_api_path(迁移 SQL 在其上真实可执行);
_MAPPING 三键在 upgrade 后不复存在、downgrade 后新键清零且旧行 id→key 对应
关系逐行还原(双向 UPDATE 均为全表谓词替换);无关 key 对照行全程不变。
"""
engine = sa.create_engine("sqlite:///:memory:")
with engine.begin() as connection:
context = MigrationContext.configure(connection)
operations = Operations(context)
# tt16 仅 UPDATE schedule_tasks.platform_api_path(真实列名,String(500)),
# 无表结构变更:建最小桩表承接。
sa.Table(
"schedule_tasks", sa.MetaData(),
sa.Column("id", sa.Integer(), primary_key=True),
sa.Column("platform_api_path", sa.String(length=500)),
).create(connection)
old_keys = list(_MAPPING)
rows = [{"id": index + 1, "key": key} for index, key in enumerate(old_keys)]
# id=4 为无关 key 对照行:升级/降级都不得被改写。
rows.append({"id": len(rows) + 1, "key": _UNRELATED_KEY})
connection.execute(
sa.text("INSERT INTO schedule_tasks (id, platform_api_path) VALUES (:id, :key)"),
rows,
)
migration = _load("tt16c7d8e9f0a1b2")
migration.op = operations
def _job_keys() -> dict[int, str]:
result = connection.execute(
sa.text("SELECT id, platform_api_path FROM schedule_tasks ORDER BY id")
)
return {row_id: key for row_id, key in result.fetchall()}
def _count_any_of(keys: list[str]) -> int:
return connection.execute(sa.text(
"SELECT COUNT(*) FROM schedule_tasks WHERE platform_api_path IN "
"('" + "','".join(keys) + "')"
)).scalar()
# [前置基线] 旧三键恰 3 行、新键 0 行、无关 key 恰 1 行。
assert _count_any_of(old_keys) == 3
assert _count_any_of(list(_MAPPING.values())) == 0
assert _count_any_of([_UNRELATED_KEY]) == 1
# [STEP 1] upgrade:旧行逐行映射新键,旧键零残留,无关 key 不受影响。
migration.upgrade()
assert _job_keys() == {
1: _MAPPING[old_keys[0]],
2: _MAPPING[old_keys[1]],
3: _MAPPING[old_keys[2]],
4: _UNRELATED_KEY,
}
assert _count_any_of(old_keys) == 0
assert _count_any_of(list(_MAPPING.values())) == 3
assert _count_any_of([_UNRELATED_KEY]) == 1
# [STEP 2] downgrade:反向映射逐行还原旧键,新键零残留,无关 key 不受影响。
migration.downgrade()
assert _job_keys() == {row["id"]: row["key"] for row in rows}
assert _count_any_of(old_keys) == 3
assert _count_any_of(list(_MAPPING.values())) == 0
assert _count_any_of([_UNRELATED_KEY]) == 1