- 新增 TransferDocument 按票执行模型与单据号序列生成(document_id 独立模块,迁移 tt0d/tt8b) - 发票行新增单位字段、matching_id 放宽可空,支持行与票两级执行口径(迁移 tt9c/tt1a) - capability_client 新增 create_transfer 写契约与六态结果裁决,执行链补写前复查门禁 - 抽取/匹配/执行 Job 按 run/item/match 拆分并入 jobs store 队列,transfer 队列单实例 worker 配套本地启动脚本 - 首页新增「已执行调拨单」四统计卡入口与明细列表子视图,重查/重试/关闭/重新触发与筛选分页齐全 - 行评估新鲜度 TTL 从 10 分钟放宽为当日有效(86400s),跨日由文件版本对账门禁兜底 - 同步 chat/errors 文案四语言、测试全家与两份实施计划文档
118 lines
6.1 KiB
Python
118 lines
6.1 KiB
Python
"""transfer_documents 票表与行/尝试票级外键
|
||
|
||
路径:backend/migrations/versions/tt0d1e2f3a4b_add_transfer_documents.py
|
||
用途:新建 ERP 调拨单(票)表 transfer_documents——五元组一组一票,
|
||
doc_no/submit_timestamp 终身复用(D3);部分唯一索引
|
||
uq_transfer_documents_group_live 强制「同组至多一张未完结票」
|
||
(D15 防线,DB 层防双单兜底);transfer_invoice_items 与
|
||
transfer_attempts 增加可空 document_id 外键(D14:票:Attempt=1:N,
|
||
外键在 Attempt 侧;票表不存行集快照,完整行集随写 Attempt 留档)。
|
||
依赖:当前 alembic head tt9c0d1e2f3a;生产数据库为 PostgreSQL;
|
||
部分唯一索引同时声明 postgresql_where / sqlite_where(SQLite 与 PG
|
||
均支持 WHERE 形式,测试建表同语义)。
|
||
逻辑:upgrade 先建表(唯一约束 + 状态 CHECK 兜底 + 部分唯一索引 + 普通索引;
|
||
CHECK 随 create_table 落地、随 downgrade drop_table 一并移除,反向完整),
|
||
再为两张既有表加可空外键列并为 transfer_invoice_items.document_id
|
||
建单列索引(T10 票级执行按票查行);downgrade 精确反向:先删行表
|
||
document_id 索引与两 FK 列,再删票表索引、drop 票表。
|
||
|
||
Revision ID: tt0d1e2f3a4b
|
||
Revises: tt9c0d1e2f3a
|
||
Create Date: 2026-09-24 05:30:00.000000
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import sqlalchemy as sa
|
||
from alembic import op
|
||
|
||
revision = 'tt0d1e2f3a4b'
|
||
down_revision = 'tt9c0d1e2f3a'
|
||
branch_labels = None
|
||
depends_on = None
|
||
|
||
# 组锁 WHERE 子句:必须与 app/models/in_transit_transfer.py 的
|
||
# UNRESOLVED_DOC_STATUSES 完全一致(test_document_model.py 锁定双方言同口径)。
|
||
_UNRESOLVED_DOC_STATUS_WHERE = (
|
||
"status IN ('pending', 'executing', 'result_unknown', 'business_failed', 'technical_failed')"
|
||
)
|
||
|
||
# 状态冻结枚举 CHECK(ck_transfer_documents_status):与模型侧由
|
||
# TRANSFER_DOCUMENT_STATUSES 生成的 CHECK 同口径(八态逐一对应,非法态 DB 层拒绝)。
|
||
_STATUS_CHECK = (
|
||
"status IN ('pending', 'executing', 'success', 'duplicate_success', "
|
||
"'business_failed', 'technical_failed', 'result_unknown', 'cancelled')"
|
||
)
|
||
|
||
|
||
def upgrade() -> None:
|
||
"""建票表(含全部约束/索引)并为行/尝试表加票级外键列。"""
|
||
op.create_table(
|
||
'transfer_documents',
|
||
sa.Column('id', sa.Integer(), primary_key=True),
|
||
sa.Column('run_id', sa.Integer(), sa.ForeignKey('transfer_runs.id'), nullable=True),
|
||
sa.Column('source_code', sa.String(length=64), nullable=False),
|
||
sa.Column('external_org', sa.String(length=64), nullable=False),
|
||
sa.Column('receipt_date', sa.String(length=16), nullable=False),
|
||
sa.Column('from_warehouse', sa.String(length=64), nullable=False),
|
||
sa.Column('to_warehouse', sa.String(length=64), nullable=False),
|
||
sa.Column('doc_no', sa.String(length=64), nullable=False),
|
||
sa.Column('submit_timestamp', sa.String(length=64), nullable=False),
|
||
sa.Column('status', sa.String(length=32), nullable=False, server_default='pending'),
|
||
sa.Column('stage_confirmed_at', sa.DateTime(), nullable=True),
|
||
sa.Column('erp_document_no', sa.String(length=64), nullable=True),
|
||
sa.Column('error_code', sa.String(length=64), nullable=True),
|
||
sa.Column('error_message', sa.Text(), nullable=True),
|
||
sa.Column('closed_reason', sa.Text(), nullable=True),
|
||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||
sa.UniqueConstraint('doc_no', name='uq_transfer_documents_doc_no'),
|
||
sa.UniqueConstraint('submit_timestamp', name='uq_transfer_documents_submit_timestamp'),
|
||
sa.CheckConstraint(_STATUS_CHECK, name='ck_transfer_documents_status'),
|
||
)
|
||
op.create_index('ix_transfer_documents_run_status', 'transfer_documents', ['run_id', 'status'])
|
||
op.create_index('ix_transfer_documents_erp_no', 'transfer_documents', ['erp_document_no'])
|
||
op.create_index(
|
||
'uq_transfer_documents_group_live',
|
||
'transfer_documents',
|
||
['source_code', 'external_org', 'receipt_date', 'from_warehouse', 'to_warehouse'],
|
||
unique=True,
|
||
postgresql_where=sa.text(_UNRESOLVED_DOC_STATUS_WHERE),
|
||
sqlite_where=sa.text(_UNRESOLVED_DOC_STATUS_WHERE),
|
||
)
|
||
|
||
# 行表加票级外键(D18 关票脱离语义由服务层置 NULL,列为可空)。
|
||
with op.batch_alter_table('transfer_invoice_items') as batch_op:
|
||
batch_op.add_column(sa.Column('document_id', sa.Integer(), nullable=True))
|
||
batch_op.create_foreign_key(
|
||
'fk_transfer_invoice_items_document_id', 'transfer_documents',
|
||
['document_id'], ['id'],
|
||
)
|
||
# 票级执行(T10)高频「按票查行」:document_id 单列索引(控制器裁决并入本迁移)。
|
||
op.create_index(
|
||
'ix_transfer_invoice_items_document_id',
|
||
'transfer_invoice_items', ['document_id'],
|
||
)
|
||
|
||
# 尝试表加票级外键(写 Attempt 挂票;重查 Attempt 仍只挂 item)。
|
||
with op.batch_alter_table('transfer_attempts') as batch_op:
|
||
batch_op.add_column(sa.Column('document_id', sa.Integer(), nullable=True))
|
||
batch_op.create_foreign_key(
|
||
'fk_transfer_attempts_document_id', 'transfer_documents',
|
||
['document_id'], ['id'],
|
||
)
|
||
|
||
|
||
def downgrade() -> None:
|
||
"""精确反向:先删行表 document_id 索引与两 FK 列(约束随列移除),再删索引并 drop 票表。"""
|
||
op.drop_index('ix_transfer_invoice_items_document_id', table_name='transfer_invoice_items')
|
||
with op.batch_alter_table('transfer_attempts') as batch_op:
|
||
batch_op.drop_column('document_id')
|
||
with op.batch_alter_table('transfer_invoice_items') as batch_op:
|
||
batch_op.drop_column('document_id')
|
||
|
||
op.drop_index('uq_transfer_documents_group_live', table_name='transfer_documents')
|
||
op.drop_index('ix_transfer_documents_run_status', table_name='transfer_documents')
|
||
op.drop_index('ix_transfer_documents_erp_no', table_name='transfer_documents')
|
||
op.drop_table('transfer_documents')
|