清理废弃的ShippingCase相关代码与依赖,统一使用ShippingNotice直接关联客户数据。调整库存网关依赖规则,支持仅对库存满足或已人工放行的订单行生成发货Excel,实现滚动履约逻辑。
106 lines
4.6 KiB
Python
106 lines
4.6 KiB
Python
"""物理移除已废弃的 ShippingCase 聚合层。
|
|
|
|
用途:在历史 Notice 已自持客户与 business_no 后,删除 shipping_cases 表及 Notice 外键。
|
|
依赖:shipping_notices、shipping_customers、shipping_business_sequences。
|
|
逻辑:先检查残留客户/编号事实,再断开 Notice 旧外键并删除历史聚合表;不影响 Review Case 或收料 RPA。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
|
|
revision = 'hp9q0r1s2t3u'
|
|
down_revision = 'ho8p9q0r1s2t'
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
"""校验 Notice 已完成迁移后删除 Case 表和兼容字段。"""
|
|
bind = op.get_bind()
|
|
inspector = sa.inspect(bind)
|
|
if not inspector.has_table('shipping_cases'):
|
|
return
|
|
|
|
# 历史库可能错误标记为已执行 hn/ho,但物理表仍缺少其应新增的列。
|
|
# 最终删表迁移必须先自愈这些可由旧 Case 无损恢复的事实,不能引用缺失列。
|
|
notice_columns = {item['name'] for item in inspector.get_columns('shipping_notices')}
|
|
missing_columns = [
|
|
('business_no', sa.String(length=32)),
|
|
('business_date', sa.Date()),
|
|
('business_sequence_value', sa.Integer()),
|
|
('business_numbering_rule_version', sa.String(length=32)),
|
|
('customer_id', sa.Integer()),
|
|
('customer_name_snapshot', sa.String(length=200)),
|
|
('customer_short_code_snapshot', sa.String(length=6)),
|
|
]
|
|
missing_notice_fields = {name for name, _ in missing_columns if name not in notice_columns}
|
|
with op.batch_alter_table('shipping_notices') as batch:
|
|
for name, column_type in missing_columns:
|
|
if name in missing_notice_fields:
|
|
batch.add_column(sa.Column(name, column_type, nullable=True))
|
|
|
|
# 重新读取反射信息,确保修复后的 PostgreSQL/SQLite 结构都用于后续约束和校验。
|
|
inspector = sa.inspect(bind)
|
|
notice_columns = {item['name'] for item in inspector.get_columns('shipping_notices')}
|
|
case_columns = {item['name'] for item in inspector.get_columns('shipping_cases')}
|
|
copied_fields = [
|
|
('business_no', 'business_no'),
|
|
('business_date', 'business_date'),
|
|
('business_sequence_value', 'sequence_value'),
|
|
('business_numbering_rule_version', 'numbering_rule_version'),
|
|
('customer_id', 'customer_id'),
|
|
('customer_name_snapshot', 'customer_name_snapshot'),
|
|
('customer_short_code_snapshot', 'customer_short_code_snapshot'),
|
|
]
|
|
assignments = [
|
|
f"{notice_field} = COALESCE(n.{notice_field}, c.{case_field})"
|
|
for notice_field, case_field in copied_fields
|
|
if notice_field in missing_notice_fields and case_field in case_columns
|
|
]
|
|
if assignments:
|
|
bind.execute(sa.text(f"""
|
|
UPDATE shipping_notices AS n
|
|
SET {', '.join(assignments)}
|
|
FROM shipping_cases AS c
|
|
WHERE c.id = n.shipping_case_id
|
|
"""))
|
|
|
|
# Notice 是最终事实;旧 Case 若仍有未迁移编号或客户,拒绝静默丢失。
|
|
residual = bind.execute(sa.text("""
|
|
SELECT COUNT(*)
|
|
FROM shipping_notices AS n
|
|
JOIN shipping_cases AS c ON c.id = n.shipping_case_id
|
|
WHERE (c.business_no IS NOT NULL AND n.business_no IS NULL)
|
|
OR (c.customer_id IS NOT NULL AND n.customer_id IS NULL)
|
|
""")).scalar_one()
|
|
if int(residual or 0):
|
|
raise RuntimeError(
|
|
f'shipping_case migration incomplete: {int(residual)} Notice rows still miss copied facts'
|
|
)
|
|
|
|
notice_indexes = {item['name'] for item in inspector.get_indexes('shipping_notices')}
|
|
notice_fks = inspector.get_foreign_keys('shipping_notices')
|
|
with op.batch_alter_table('shipping_notices') as batch:
|
|
for fk in notice_fks:
|
|
referred = str(fk.get('referred_table') or '')
|
|
columns = set(fk.get('constrained_columns') or [])
|
|
if referred == 'shipping_cases' and 'shipping_case_id' in columns:
|
|
if fk.get('name'):
|
|
batch.drop_constraint(fk['name'], type_='foreignkey')
|
|
if 'ix_shipping_notices_shipping_case_id' in notice_indexes:
|
|
batch.drop_index('ix_shipping_notices_shipping_case_id')
|
|
if 'shipping_case_id' in notice_columns:
|
|
batch.drop_column('shipping_case_id')
|
|
|
|
# PostgreSQL 的 UNIQUE 约束拥有同名底层索引,不能先以 index 操作删除;
|
|
# DROP TABLE 会一并清理 Case 表上的普通索引和约束索引。
|
|
op.drop_table('shipping_cases')
|
|
|
|
|
|
def downgrade() -> None:
|
|
"""不自动恢复已删除的历史 Case 数据。"""
|
|
raise RuntimeError('ShippingCase removal migration is irreversible')
|