feat(shipping-notice,receipt-report): 新增收料报表功能并优化发货通知流程
清理废弃的shipping xfa ocr automark相关代码和job配置 新增收料报表管理的前端页面、路由和侧边栏入口 新增后端收料报表配置接口、数据库模型和RPA脚本 优化发货通知流程的重试策略,将shipping相关job默认不自动重试 移除废弃的库存源确认相关逻辑,更新流程门禁规则 重构订单模板的值适配器模块,新增坐标OCR值适配器 清理前端shipping菜单和action的废弃选项 更新相关文档和测试用例,调整.gitignore忽略规则
This commit is contained in:
1 parent
260465b72d
commit
1b6483a4ea
466 files changed
+32115
-33796
No files matched your search
@@ -0,0 +1,49 @@
|
||||
"""
|
||||
migrations/versions/hf0k1l2m3n4o_add_shipping_eml_preview_snapshots.py
|
||||
|
||||
用途:创建 EML 安全 HTML 预览快照表。
|
||||
依赖:shipping_order_documents、Alembic。
|
||||
逻辑:只缓存已清洗预览,不回填、不迁移原始邮件;删除 Document 时级联删除快照。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'hf0k1l2m3n4o'
|
||||
down_revision = 'he9k1l2m3n4o'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""创建按正文身份和清洗版本唯一的安全预览快照表。"""
|
||||
op.create_table(
|
||||
'shipping_eml_preview_snapshots',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('document_id', sa.Integer(), nullable=False),
|
||||
sa.Column('source_content_hash', sa.String(length=64), nullable=False),
|
||||
sa.Column('sanitizer_version', sa.String(length=32), nullable=False),
|
||||
sa.Column('html', sa.Text(), nullable=False),
|
||||
sa.Column('html_checksum', sa.String(length=64), nullable=False),
|
||||
sa.Column('html_size_bytes', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['document_id'], ['shipping_order_documents.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint(
|
||||
'document_id', 'source_content_hash', 'sanitizer_version',
|
||||
name='uq_shipping_eml_preview_snapshot_identity',
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_eml_preview_snapshot_document', 'shipping_eml_preview_snapshots',
|
||||
['document_id', 'created_at'], unique=False,
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""移除可再生的安全预览缓存,不影响原始 EML 或订单事实。"""
|
||||
op.drop_index('ix_shipping_eml_preview_snapshot_document', table_name='shipping_eml_preview_snapshots')
|
||||
op.drop_table('shipping_eml_preview_snapshots')
|
||||
@@ -0,0 +1,88 @@
|
||||
"""
|
||||
增加库存缺口原因放行决定。
|
||||
|
||||
用途:扩展既有行级履约决定约束,允许短缺行以必填原因形成可审计放行事实。
|
||||
依赖:shipping_order_review_line_decisions、Alembic。
|
||||
逻辑:仅扩展 CHECK 枚举,不迁移或改写历史决定;downgrade 恢复旧约束,
|
||||
因而降级前必须不存在 release_inventory_shortage / released_shortage 记录。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'hg1i2j3k4l5m'
|
||||
down_revision = 'hf0k1l2m3n4o'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""扩展行级决定和状态约束,以保存短缺放行的原因审计。"""
|
||||
op.drop_constraint(
|
||||
'ck_shipping_review_line_decisions_code',
|
||||
'shipping_order_review_line_decisions',
|
||||
type_='check',
|
||||
)
|
||||
op.create_check_constraint(
|
||||
'ck_shipping_review_line_decisions_code',
|
||||
'shipping_order_review_line_decisions',
|
||||
"decision_code IN ("
|
||||
"'wait_inventory', 'use_new_inventory_file', 'change_inventory_source', "
|
||||
"'fulfill_complete_line_now', 'close_unfulfillable', 'reopen', 'release_inventory_shortage'"
|
||||
")",
|
||||
)
|
||||
# 兼容已存在的“有快照、漏文件号”行事实。文件号仅从该行已绑定的快照反查,
|
||||
# 不按库存源或任务级 active link 猜测,迁移可重复执行且不会覆盖已有证据。
|
||||
op.execute(
|
||||
"UPDATE shipping_notice_line_inventory_matches AS line_match "
|
||||
"SET matched_file_no = inventory_file.inventory_file_no "
|
||||
"FROM shipping_inventory_files AS inventory_file "
|
||||
"WHERE line_match.inventory_snapshot_id = inventory_file.id "
|
||||
"AND line_match.matched_file_no IS NULL "
|
||||
"AND inventory_file.inventory_file_no IS NOT NULL"
|
||||
)
|
||||
op.drop_constraint(
|
||||
'ck_shipping_review_line_decisions_status',
|
||||
'shipping_order_review_line_decisions',
|
||||
type_='check',
|
||||
)
|
||||
op.create_check_constraint(
|
||||
'ck_shipping_review_line_decisions_status',
|
||||
'shipping_order_review_line_decisions',
|
||||
"line_status IN ("
|
||||
"'pending_decision', 'waiting_inventory', 'ready_to_fulfill', "
|
||||
"'task_created', 'fulfilled', 'closed_unfulfillable', 'reopened', 'released_shortage'"
|
||||
")",
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""恢复旧约束;含新放行记录时数据库会拒绝降级,避免静默丢失审计语义。"""
|
||||
op.drop_constraint(
|
||||
'ck_shipping_review_line_decisions_status',
|
||||
'shipping_order_review_line_decisions',
|
||||
type_='check',
|
||||
)
|
||||
op.create_check_constraint(
|
||||
'ck_shipping_review_line_decisions_status',
|
||||
'shipping_order_review_line_decisions',
|
||||
"line_status IN ("
|
||||
"'pending_decision', 'waiting_inventory', 'ready_to_fulfill', "
|
||||
"'task_created', 'fulfilled', 'closed_unfulfillable', 'reopened'"
|
||||
")",
|
||||
)
|
||||
op.drop_constraint(
|
||||
'ck_shipping_review_line_decisions_code',
|
||||
'shipping_order_review_line_decisions',
|
||||
type_='check',
|
||||
)
|
||||
op.create_check_constraint(
|
||||
'ck_shipping_review_line_decisions_code',
|
||||
'shipping_order_review_line_decisions',
|
||||
"decision_code IN ("
|
||||
"'wait_inventory', 'use_new_inventory_file', 'change_inventory_source', "
|
||||
"'fulfill_complete_line_now', 'close_unfulfillable', 'reopen'"
|
||||
")",
|
||||
)
|
||||
@@ -0,0 +1,30 @@
|
||||
"""
|
||||
为订单模板盲测运行保存统一 value-adapter evidence。
|
||||
|
||||
用途:将盲测 metrics 从 failure_codes_json 的 XFA 专属哨兵迁移到所有 adapter 共用的证据字段。
|
||||
依赖:shipping_document_profile_test_runs、Alembic。
|
||||
逻辑:新增非空 JSON evidence_json;历史运行默认空对象,失败码语义不变。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'hh2j3k4l5m6n'
|
||||
down_revision = 'hg1i2j3k4l5m'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""为既有盲测运行增加统一、可审计的 adapter evidence 容器。"""
|
||||
with op.batch_alter_table('shipping_document_profile_test_runs', recreate='auto') as batch:
|
||||
batch.add_column(sa.Column('evidence_json', sa.JSON(), nullable=False, server_default=sa.text("'{}'")))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""回滚统一 evidence 容器;历史盲测计数和失败码保持不变。"""
|
||||
with op.batch_alter_table('shipping_document_profile_test_runs', recreate='auto') as batch:
|
||||
batch.drop_column('evidence_json')
|
||||
@@ -0,0 +1,141 @@
|
||||
"""
|
||||
创建独立收料 RPA 的配置、执行、归档和通知投递表。
|
||||
|
||||
用途:为收料定时导出提供独立数据边界,不变更库存快照、库存匹配或旧 Worker 使用的表。
|
||||
依赖:shipping_inventory_sources、schedule_runs、users、Alembic。
|
||||
逻辑:站点一对一配置;每次运行按站点与 Warehouse 形成唯一单元;文件和企微投递分别幂等保存。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'hi3j4k5l6m7n'
|
||||
down_revision = 'hh2j3k4l5m6n'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""创建收料 RPA 独立表及用于幂等、保留清理和运行查询的索引。"""
|
||||
op.create_table(
|
||||
'shipping_receipt_report_configs',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('inventory_source_id', sa.Integer(), nullable=False),
|
||||
sa.Column('enabled', sa.Boolean(), nullable=False, server_default=sa.text('true')),
|
||||
sa.Column('report_path_json', sa.JSON(), nullable=False, server_default=sa.text("'{}'")),
|
||||
sa.Column('query_parameters_json', sa.JSON(), nullable=False, server_default=sa.text("'[]'")),
|
||||
sa.Column('warehouse_ids_json', sa.JSON(), nullable=False, server_default=sa.text("'[]'")),
|
||||
sa.Column('config_version', sa.Integer(), nullable=False, server_default=sa.text('1')),
|
||||
sa.Column('created_by', sa.Integer(), nullable=True),
|
||||
sa.Column('updated_by', sa.Integer(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['inventory_source_id'], ['shipping_inventory_sources.id']),
|
||||
sa.ForeignKeyConstraint(['created_by'], ['users.id']),
|
||||
sa.ForeignKeyConstraint(['updated_by'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('inventory_source_id', name='uq_shipping_receipt_report_configs_source'),
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_receipt_report_configs_inventory_source', 'shipping_receipt_report_configs',
|
||||
['inventory_source_id'], unique=False,
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
'shipping_receipt_rpa_unit_runs',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('schedule_run_id', sa.Integer(), nullable=False),
|
||||
sa.Column('inventory_source_id', sa.Integer(), nullable=False),
|
||||
sa.Column('retry_of_id', sa.Integer(), nullable=True),
|
||||
sa.Column('source_code', sa.String(length=64), nullable=False),
|
||||
sa.Column('connector_type', sa.String(length=32), nullable=False),
|
||||
sa.Column('warehouse_id', sa.String(length=64), nullable=True),
|
||||
sa.Column('callback_id', sa.String(length=128), nullable=False),
|
||||
sa.Column('status', sa.String(length=32), nullable=False, server_default=sa.text("'queued'")),
|
||||
sa.Column('config_snapshot_json', sa.JSON(), nullable=False, server_default=sa.text("'{}'")),
|
||||
sa.Column('trigger_snapshot_json', sa.JSON(), nullable=False, server_default=sa.text("'{}'")),
|
||||
sa.Column('result_json', sa.JSON(), nullable=False, server_default=sa.text("'{}'")),
|
||||
sa.Column('error_code', sa.String(length=64), nullable=True),
|
||||
sa.Column('error_message', sa.Text(), nullable=True),
|
||||
sa.Column('started_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('finished_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.CheckConstraint(
|
||||
"status IN ('queued', 'awaiting_rpa', 'processing', 'success', 'empty', 'failed', 'cancelled')",
|
||||
name='ck_shipping_receipt_rpa_unit_runs_status',
|
||||
),
|
||||
sa.ForeignKeyConstraint(['schedule_run_id'], ['schedule_runs.id']),
|
||||
sa.ForeignKeyConstraint(['inventory_source_id'], ['shipping_inventory_sources.id']),
|
||||
sa.ForeignKeyConstraint(['retry_of_id'], ['shipping_receipt_rpa_unit_runs.id']),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('callback_id', name='uq_shipping_receipt_rpa_unit_runs_callback'),
|
||||
)
|
||||
op.create_index('ix_shipping_receipt_unit_run_schedule_status', 'shipping_receipt_rpa_unit_runs', ['schedule_run_id', 'status'], unique=False)
|
||||
op.create_index('ix_shipping_receipt_unit_run_source_created', 'shipping_receipt_rpa_unit_runs', ['inventory_source_id', 'created_at'], unique=False)
|
||||
|
||||
op.create_table(
|
||||
'shipping_receipt_report_files',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('unit_run_id', sa.Integer(), nullable=False),
|
||||
sa.Column('storage_provider', sa.String(length=32), nullable=False),
|
||||
sa.Column('storage_bucket', sa.String(length=128), nullable=False),
|
||||
sa.Column('storage_key', sa.String(length=1024), nullable=False),
|
||||
sa.Column('original_filename', sa.String(length=255), nullable=False),
|
||||
sa.Column('content_type', sa.String(length=128), nullable=True),
|
||||
sa.Column('file_size', sa.Integer(), nullable=False),
|
||||
sa.Column('sha256', sa.String(length=64), nullable=False),
|
||||
sa.Column('archive_expires_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('archive_deleted_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('archive_delete_result_json', sa.JSON(), nullable=False, server_default=sa.text("'{}'")),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.CheckConstraint('file_size >= 0', name='ck_shipping_receipt_report_files_size'),
|
||||
sa.ForeignKeyConstraint(['unit_run_id'], ['shipping_receipt_rpa_unit_runs.id']),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('unit_run_id', 'sha256', name='uq_shipping_receipt_report_files_unit_sha256'),
|
||||
)
|
||||
op.create_index('ix_shipping_receipt_report_files_unit', 'shipping_receipt_report_files', ['unit_run_id'], unique=False)
|
||||
op.create_index('ix_shipping_receipt_report_files_expires', 'shipping_receipt_report_files', ['archive_expires_at', 'archive_deleted_at'], unique=False)
|
||||
|
||||
op.create_table(
|
||||
'shipping_receipt_notification_deliveries',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('unit_run_id', sa.Integer(), nullable=True),
|
||||
sa.Column('report_file_id', sa.Integer(), nullable=True),
|
||||
sa.Column('recipient_user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('message_type', sa.String(length=16), nullable=False),
|
||||
sa.Column('idempotency_key', sa.String(length=255), nullable=False),
|
||||
sa.Column('status', sa.String(length=16), nullable=False, server_default=sa.text("'pending'")),
|
||||
sa.Column('response_summary_json', sa.JSON(), nullable=False, server_default=sa.text("'{}'")),
|
||||
sa.Column('error_code', sa.String(length=64), nullable=True),
|
||||
sa.Column('error_message', sa.Text(), nullable=True),
|
||||
sa.Column('sent_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.CheckConstraint("message_type IN ('file', 'summary')", name='ck_shipping_receipt_notification_deliveries_type'),
|
||||
sa.CheckConstraint("status IN ('pending', 'sending', 'success', 'failed')", name='ck_shipping_receipt_notification_deliveries_status'),
|
||||
sa.ForeignKeyConstraint(['unit_run_id'], ['shipping_receipt_rpa_unit_runs.id']),
|
||||
sa.ForeignKeyConstraint(['report_file_id'], ['shipping_receipt_report_files.id']),
|
||||
sa.ForeignKeyConstraint(['recipient_user_id'], ['users.id']),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('idempotency_key', name='uq_shipping_receipt_notification_deliveries_idempotency'),
|
||||
)
|
||||
op.create_index('ix_shipping_receipt_delivery_file_recipient', 'shipping_receipt_notification_deliveries', ['report_file_id', 'recipient_user_id'], unique=False)
|
||||
op.create_index('ix_shipping_receipt_delivery_unit_created', 'shipping_receipt_notification_deliveries', ['unit_run_id', 'created_at'], unique=False)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""删除独立收料 RPA 表;调用方必须先确认不需要保留收料运行与归档事实。"""
|
||||
op.drop_index('ix_shipping_receipt_delivery_unit_created', table_name='shipping_receipt_notification_deliveries')
|
||||
op.drop_index('ix_shipping_receipt_delivery_file_recipient', table_name='shipping_receipt_notification_deliveries')
|
||||
op.drop_table('shipping_receipt_notification_deliveries')
|
||||
op.drop_index('ix_shipping_receipt_report_files_expires', table_name='shipping_receipt_report_files')
|
||||
op.drop_index('ix_shipping_receipt_report_files_unit', table_name='shipping_receipt_report_files')
|
||||
op.drop_table('shipping_receipt_report_files')
|
||||
op.drop_index('ix_shipping_receipt_unit_run_source_created', table_name='shipping_receipt_rpa_unit_runs')
|
||||
op.drop_index('ix_shipping_receipt_unit_run_schedule_status', table_name='shipping_receipt_rpa_unit_runs')
|
||||
op.drop_table('shipping_receipt_rpa_unit_runs')
|
||||
op.drop_index('ix_shipping_receipt_report_configs_inventory_source', table_name='shipping_receipt_report_configs')
|
||||
op.drop_table('shipping_receipt_report_configs')
|
||||
@@ -0,0 +1,59 @@
|
||||
"""允许同一 Intake 的不同 PDF 并行重解析。
|
||||
|
||||
用途:移除按 Intake 单列互斥的部分唯一索引;批次重解析的互斥边界由服务层按
|
||||
Intake + 冻结目标 Document 精确裁决,避免同一 EML 的第二份 PDF 被错误拒绝。
|
||||
依赖:shipping_order_processing_run_intakes、Alembic。
|
||||
逻辑:只替换索引,不改写历史 Run/Intake 数据;旧 Run 继续按其既有谱系工作。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'hj4k5l6m7n8o'
|
||||
down_revision = 'hi3j4k5l6m7n'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""将活动占用索引由唯一 Intake 改为普通查询索引。"""
|
||||
op.drop_index(
|
||||
'uq_shipping_order_processing_run_intakes_active_intake',
|
||||
table_name='shipping_order_processing_run_intakes',
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_order_processing_run_intakes_active_intake',
|
||||
'shipping_order_processing_run_intakes',
|
||||
['intake_id'],
|
||||
unique=False,
|
||||
postgresql_where=sa.text('released_at IS NULL'),
|
||||
sqlite_where=sa.text('released_at IS NULL'),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""恢复旧部署的 Intake 级互斥索引;已有并行活动 Run 时失败关闭。"""
|
||||
conflicting_intake = op.get_bind().execute(sa.text(
|
||||
"SELECT intake_id FROM shipping_order_processing_run_intakes "
|
||||
"WHERE released_at IS NULL GROUP BY intake_id HAVING COUNT(*) > 1 LIMIT 1"
|
||||
)).scalar()
|
||||
if conflicting_intake is not None:
|
||||
raise RuntimeError(
|
||||
'Cannot restore Intake-level active Run uniqueness while parallel PDF reparses exist '
|
||||
f'(intake_id={int(conflicting_intake)}). Release or resolve those runs before downgrade.'
|
||||
)
|
||||
op.drop_index(
|
||||
'ix_shipping_order_processing_run_intakes_active_intake',
|
||||
table_name='shipping_order_processing_run_intakes',
|
||||
)
|
||||
op.create_index(
|
||||
'uq_shipping_order_processing_run_intakes_active_intake',
|
||||
'shipping_order_processing_run_intakes',
|
||||
['intake_id'],
|
||||
unique=True,
|
||||
postgresql_where=sa.text('released_at IS NULL'),
|
||||
sqlite_where=sa.text('released_at IS NULL'),
|
||||
)
|
||||
@@ -0,0 +1,119 @@
|
||||
"""新增文件级 ReviewStream 当前版本主线。
|
||||
|
||||
用途:把文件当前复核版本从批次 Run 中独立出来,保留 Run/Case 历史审计。
|
||||
依赖:shipping_order_documents、shipping_order_review_cases、users。
|
||||
逻辑:Stream 保存一个 Document 的 current Case;Case 追加版本链字段,旧字段继续保留。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
|
||||
revision = 'hk5l6m7n8o9p'
|
||||
down_revision = 'hj4k5l6m7n8o'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""创建文件复核主线并扩展 Case 版本关系。"""
|
||||
op.create_table(
|
||||
'shipping_order_review_streams',
|
||||
sa.Column('id', sa.Integer(), primary_key=True),
|
||||
sa.Column('document_id', sa.Integer(), nullable=False),
|
||||
sa.Column('content_identity', sa.String(length=96), nullable=False),
|
||||
sa.Column('owner_user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('current_case_id', sa.Integer(), nullable=True),
|
||||
sa.Column('confirmed_case_id', sa.Integer(), nullable=True),
|
||||
sa.Column('state', sa.String(length=16), nullable=False, server_default='active'),
|
||||
sa.Column('revision', sa.Integer(), nullable=False, server_default='0'),
|
||||
sa.Column('lock_version', sa.Integer(), nullable=False, server_default='1'),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.CheckConstraint(
|
||||
"state IN ('active', 'reparsing', 'confirmed', 'closed', 'conflict')",
|
||||
name='ck_shipping_order_review_streams_state',
|
||||
),
|
||||
sa.ForeignKeyConstraint(['document_id'], ['shipping_order_documents.id']),
|
||||
sa.ForeignKeyConstraint(['owner_user_id'], ['users.id']),
|
||||
)
|
||||
op.create_index(
|
||||
'uq_shipping_order_review_streams_document',
|
||||
'shipping_order_review_streams', ['document_id'], unique=True,
|
||||
)
|
||||
op.create_index(
|
||||
'uq_shipping_order_review_streams_current_case',
|
||||
'shipping_order_review_streams', ['current_case_id'], unique=True,
|
||||
postgresql_where=sa.text('current_case_id IS NOT NULL'),
|
||||
sqlite_where=sa.text('current_case_id IS NOT NULL'),
|
||||
)
|
||||
op.create_index(
|
||||
'uq_shipping_order_review_streams_confirmed_case',
|
||||
'shipping_order_review_streams', ['confirmed_case_id'], unique=True,
|
||||
postgresql_where=sa.text('confirmed_case_id IS NOT NULL'),
|
||||
sqlite_where=sa.text('confirmed_case_id IS NOT NULL'),
|
||||
)
|
||||
op.create_index(
|
||||
'ix_shipping_order_review_streams_owner_state_updated',
|
||||
'shipping_order_review_streams', ['owner_user_id', 'state', 'updated_at'],
|
||||
)
|
||||
|
||||
with op.batch_alter_table('shipping_order_review_cases', recreate='auto') as batch_op:
|
||||
batch_op.add_column(sa.Column('stream_id', sa.Integer(), nullable=True))
|
||||
batch_op.add_column(sa.Column('revision_no', sa.Integer(), nullable=True))
|
||||
batch_op.add_column(sa.Column('supersedes_case_id', sa.Integer(), nullable=True))
|
||||
batch_op.add_column(sa.Column('superseded_by_case_id', sa.Integer(), nullable=True))
|
||||
batch_op.add_column(sa.Column('became_current_at', sa.DateTime(), nullable=True))
|
||||
batch_op.add_column(sa.Column('retired_at', sa.DateTime(), nullable=True))
|
||||
batch_op.create_foreign_key(
|
||||
'fk_shipping_order_review_cases_stream', 'shipping_order_review_streams',
|
||||
['stream_id'], ['id'], ondelete='SET NULL',
|
||||
)
|
||||
batch_op.create_foreign_key(
|
||||
'fk_shipping_order_review_cases_supersedes', 'shipping_order_review_cases',
|
||||
['supersedes_case_id'], ['id'], ondelete='SET NULL',
|
||||
)
|
||||
batch_op.create_foreign_key(
|
||||
'fk_shipping_order_review_cases_superseded_by', 'shipping_order_review_cases',
|
||||
['superseded_by_case_id'], ['id'], ondelete='SET NULL',
|
||||
)
|
||||
batch_op.create_unique_constraint(
|
||||
'uq_shipping_order_review_cases_stream_revision', ['stream_id', 'revision_no'],
|
||||
)
|
||||
batch_op.create_index(
|
||||
'ix_shipping_order_review_cases_stream_revision', ['stream_id', 'revision_no'],
|
||||
)
|
||||
batch_op.create_index('ix_shipping_order_review_cases_supersedes', ['supersedes_case_id'])
|
||||
|
||||
with op.batch_alter_table('shipping_order_review_streams', recreate='auto') as batch_op:
|
||||
batch_op.create_foreign_key(
|
||||
'fk_shipping_order_review_streams_current_case', 'shipping_order_review_cases',
|
||||
['current_case_id'], ['id'], ondelete='SET NULL',
|
||||
)
|
||||
batch_op.create_foreign_key(
|
||||
'fk_shipping_order_review_streams_confirmed_case', 'shipping_order_review_cases',
|
||||
['confirmed_case_id'], ['id'], ondelete='SET NULL',
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""移除 Stream 扩展;仅供未回填的隔离环境回滚。"""
|
||||
with op.batch_alter_table('shipping_order_review_streams', recreate='auto') as batch_op:
|
||||
batch_op.drop_constraint('fk_shipping_order_review_streams_confirmed_case', type_='foreignkey')
|
||||
batch_op.drop_constraint('fk_shipping_order_review_streams_current_case', type_='foreignkey')
|
||||
op.drop_index('ix_shipping_order_review_cases_supersedes', table_name='shipping_order_review_cases')
|
||||
op.drop_index('ix_shipping_order_review_cases_stream_revision', table_name='shipping_order_review_cases')
|
||||
with op.batch_alter_table('shipping_order_review_cases', recreate='auto') as batch_op:
|
||||
batch_op.drop_constraint('uq_shipping_order_review_cases_stream_revision', type_='unique')
|
||||
batch_op.drop_constraint('fk_shipping_order_review_cases_superseded_by', type_='foreignkey')
|
||||
batch_op.drop_constraint('fk_shipping_order_review_cases_supersedes', type_='foreignkey')
|
||||
batch_op.drop_constraint('fk_shipping_order_review_cases_stream', type_='foreignkey')
|
||||
for name in ('retired_at', 'became_current_at', 'superseded_by_case_id', 'supersedes_case_id', 'revision_no', 'stream_id'):
|
||||
batch_op.drop_column(name)
|
||||
op.drop_index('ix_shipping_order_review_streams_owner_state_updated', table_name='shipping_order_review_streams')
|
||||
op.drop_index('uq_shipping_order_review_streams_confirmed_case', table_name='shipping_order_review_streams')
|
||||
op.drop_index('uq_shipping_order_review_streams_current_case', table_name='shipping_order_review_streams')
|
||||
op.drop_index('uq_shipping_order_review_streams_document', table_name='shipping_order_review_streams')
|
||||
op.drop_table('shipping_order_review_streams')
|
||||
Reference in new issue
Block a user