Merge branch 'dev' into runny827

This commit is contained in:
runny committed 2026-09-07 08:04:48 +08:00
commit 0ddc0eadd8
12 files changed
+28 -501

No files matched your search

@@ -1,140 +0,0 @@
"""add contract payment warning tables
Revision ID: g0a1b2c3d4e5
Revises: ('a0b1c2d3e4f5', '9a2d6b4e8c1f')
"""
from alembic import op
import sqlalchemy as sa
revision = "g0a1b2c3d4e5"
down_revision = ("a0b1c2d3e4f5", "9a2d6b4e8c1f")
branch_labels = None
depends_on = None
def upgrade():
op.create_table(
"contract_payment_contracts",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("contract_no", sa.String(length=128), nullable=True),
sa.Column("purchase_order_no", sa.String(length=128), nullable=True),
sa.Column("supplier_name", sa.String(length=255), nullable=True),
sa.Column("total_amount", sa.Numeric(precision=18, scale=2), nullable=True),
sa.Column("currency", sa.String(length=16), nullable=True),
sa.Column("effective_date", sa.Date(), nullable=True),
sa.Column("delivery_due_date", sa.Date(), nullable=True),
sa.Column("warranty_period_days", sa.Integer(), nullable=True),
sa.Column("owner_user_id", sa.Integer(), nullable=True),
sa.Column("oa_record_id", sa.String(length=128), nullable=True),
sa.Column("oa_archive_at", sa.DateTime(), nullable=True),
sa.Column("status", sa.String(length=32), nullable=False, server_default="active"),
sa.Column("parse_status", sa.String(length=32), nullable=False, server_default="pending"),
sa.Column("plan_status", sa.String(length=32), nullable=False, server_default="pending"),
sa.Column("source_payload_json", sa.JSON(), nullable=True),
sa.Column("parse_result_json", sa.JSON(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(["owner_user_id"], ["users.id"]),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("contract_no", name="uq_contract_payment_contracts_contract_no"),
)
op.create_index("ix_contract_payment_contracts_purchase_order_no", "contract_payment_contracts", ["purchase_order_no"])
op.create_index("ix_contract_payment_contracts_status", "contract_payment_contracts", ["status"])
op.create_index("ix_contract_payment_contracts_owner_user_id", "contract_payment_contracts", ["owner_user_id"])
op.create_index("ix_contract_payment_contracts_oa_record_id", "contract_payment_contracts", ["oa_record_id"])
op.create_index("ix_contract_payment_contracts_parse_status", "contract_payment_contracts", ["parse_status"])
op.create_index("ix_contract_payment_contracts_plan_status", "contract_payment_contracts", ["plan_status"])
op.create_table(
"contract_payment_attachments",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("contract_id", sa.Integer(), nullable=False),
sa.Column("source", sa.String(length=32), nullable=False),
sa.Column("source_key", sa.String(length=512), nullable=False),
sa.Column("file_name", sa.String(length=255), nullable=False),
sa.Column("mime_type", sa.String(length=128), nullable=True),
sa.Column("file_asset_id", sa.Integer(), nullable=True),
sa.Column("dms_path", sa.String(length=1024), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("metadata_json", sa.JSON(), nullable=True),
sa.Column("status", sa.String(length=32), nullable=False, server_default="discovered"),
sa.Column("created_record_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(["contract_id"], ["contract_payment_contracts.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["file_asset_id"], ["file_assets.id"]),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("contract_id", "source", "source_key", name="uq_contract_payment_attachments_source"),
)
op.create_index("ix_contract_payment_attachments_contract", "contract_payment_attachments", ["contract_id"])
op.create_table(
"contract_payment_nodes",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("contract_id", sa.Integer(), nullable=False),
sa.Column("node_code", sa.String(length=64), nullable=False),
sa.Column("node_type", sa.String(length=32), nullable=False),
sa.Column("amount", sa.Numeric(precision=18, scale=2), nullable=True),
sa.Column("percentage", sa.Numeric(precision=8, scale=4), nullable=True),
sa.Column("anchor_type", sa.String(length=32), nullable=True),
sa.Column("anchor_date", sa.Date(), nullable=True),
sa.Column("offset_days", sa.Integer(), nullable=False, server_default="0"),
sa.Column("due_date", sa.Date(), nullable=True),
sa.Column("reminder_days", sa.Integer(), nullable=False, server_default="14"),
sa.Column("status", sa.String(length=32), nullable=False, server_default="pending"),
sa.Column("evidence_json", sa.JSON(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(["contract_id"], ["contract_payment_contracts.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("contract_id", "node_code", name="uq_contract_payment_nodes_code"),
)
op.create_index("ix_contract_payment_nodes_due_date", "contract_payment_nodes", ["due_date", "status"])
op.create_index("ix_contract_payment_nodes_contract_id", "contract_payment_nodes", ["contract_id"])
op.create_table(
"contract_payment_events",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("contract_id", sa.Integer(), nullable=False),
sa.Column("event_type", sa.String(length=32), nullable=False),
sa.Column("event_date", sa.Date(), nullable=False),
sa.Column("source_event_id", sa.String(length=128), nullable=False),
sa.Column("payload_json", sa.JSON(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(["contract_id"], ["contract_payment_contracts.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("event_type", "source_event_id", name="uq_contract_payment_events_source"),
)
op.create_index("ix_contract_payment_events_contract_type", "contract_payment_events", ["contract_id", "event_type"])
op.create_table(
"contract_payment_alerts",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("contract_id", sa.Integer(), nullable=False),
sa.Column("node_id", sa.Integer(), nullable=False),
sa.Column("trigger_key", sa.String(length=64), nullable=False),
sa.Column("scheduled_at", sa.DateTime(), nullable=True),
sa.Column("sent_at", sa.DateTime(), nullable=True),
sa.Column("recipient_user_id", sa.Integer(), nullable=True),
sa.Column("status", sa.String(length=32), nullable=False, server_default="pending"),
sa.Column("message_json", sa.JSON(), nullable=True),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(["contract_id"], ["contract_payment_contracts.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["node_id"], ["contract_payment_nodes.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["recipient_user_id"], ["users.id"]),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("node_id", "trigger_key", name="uq_contract_payment_alerts_trigger"),
)
op.create_index("ix_contract_payment_alerts_status_scheduled", "contract_payment_alerts", ["status", "scheduled_at"])
op.create_index("ix_contract_payment_alerts_contract_id", "contract_payment_alerts", ["contract_id"])
op.create_index("ix_contract_payment_alerts_node_id", "contract_payment_alerts", ["node_id"])
op.create_index("ix_contract_payment_alerts_recipient_user_id", "contract_payment_alerts", ["recipient_user_id"])
def downgrade():
op.drop_table("contract_payment_alerts")
op.drop_table("contract_payment_events")
op.drop_table("contract_payment_nodes")
op.drop_table("contract_payment_attachments")
op.drop_table("contract_payment_contracts")
@@ -1,51 +0,0 @@
"""add workflow number and allow contract number pending PDF parsing"""
from alembic import op
import sqlalchemy as sa
revision = "h1b2c3d4e5f6"
down_revision = "g0a1b2c3d4e5"
branch_labels = None
depends_on = None
def upgrade():
op.alter_column(
"contract_payment_contracts",
"contract_no",
existing_type=sa.String(length=128),
nullable=True,
)
op.add_column(
"contract_payment_contracts",
sa.Column("workflow_no", sa.String(length=128), nullable=True),
)
op.create_index(
"ix_contract_payment_contracts_workflow_no",
"contract_payment_contracts",
["workflow_no"],
)
# 已同步的首批数据把 OA“流程编号”误写进合同编号;仅迁移明确的 HT 流程号,
# 真正合同号将由已保存的附件在下一次解析时回填。
op.execute(
"UPDATE contract_payment_contracts "
"SET workflow_no = contract_no, contract_no = NULL "
"WHERE contract_no LIKE 'HT-%'"
)
def downgrade():
op.execute(
"UPDATE contract_payment_contracts "
"SET contract_no = COALESCE(workflow_no, oa_record_id, CAST(id AS VARCHAR(128))) "
"WHERE contract_no IS NULL"
)
op.drop_index("ix_contract_payment_contracts_workflow_no", table_name="contract_payment_contracts")
op.drop_column("contract_payment_contracts", "workflow_no")
op.alter_column(
"contract_payment_contracts",
"contract_no",
existing_type=sa.String(length=128),
nullable=False,
)
@@ -1,66 +0,0 @@
"""extend contract payment three round alerts
Revision ID: h2c3d4e5f6a7
Revises: h1b2c3d4e5f6
"""
from alembic import op
import sqlalchemy as sa
revision = "h2c3d4e5f6a7"
down_revision = "h1b2c3d4e5f6"
branch_labels = None
depends_on = None
def upgrade():
"""
增加厂商预付款请款作业事实表,并将预警幂等键扩展到收件人维度。
核心逻辑:同一提醒轮次允许发送给不同岗位收件人;OA 请款记录独立保存,
使预付款、出货款可以按真实请款事实停止后续升级。
"""
op.create_table(
"contract_payment_vendor_requests",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("contract_id", sa.Integer(), nullable=False),
sa.Column("source_request_id", sa.String(length=128), nullable=False),
sa.Column("request_no", sa.String(length=128), nullable=True),
sa.Column("payment_stage", sa.String(length=32), nullable=False),
sa.Column("request_status", sa.String(length=64), nullable=True),
sa.Column("requested_at", sa.DateTime(), nullable=True),
sa.Column("payload_json", sa.JSON(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(["contract_id"], ["contract_payment_contracts.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("source_request_id", name="uq_contract_payment_vendor_requests_source"),
)
op.create_index(
"ix_contract_payment_vendor_requests_contract_stage",
"contract_payment_vendor_requests",
["contract_id", "payment_stage"],
)
op.add_column(
"contract_payment_alerts",
sa.Column("alert_round", sa.Integer(), nullable=False, server_default="1"),
)
op.drop_constraint("uq_contract_payment_alerts_trigger", "contract_payment_alerts", type_="unique")
op.create_unique_constraint(
"uq_contract_payment_alerts_trigger",
"contract_payment_alerts",
["node_id", "trigger_key", "recipient_user_id"],
)
def downgrade():
"""回滚请款事实表和三轮预警收件人幂等字段。"""
op.drop_constraint("uq_contract_payment_alerts_trigger", "contract_payment_alerts", type_="unique")
op.create_unique_constraint(
"uq_contract_payment_alerts_trigger",
"contract_payment_alerts",
["node_id", "trigger_key"],
)
op.drop_column("contract_payment_alerts", "alert_round")
op.drop_table("contract_payment_vendor_requests")
@@ -1,25 +0,0 @@
"""merge contract payment warning migration head
Revision ID: i3d4e5f6a7b8
Revises: 58915a6f02ff, h2c3d4e5f6a7
用途:合并既有数据库演进线与合同付款预警表演进线,确保部署只有一个 Alembic head。
依赖:既有 58915a6f02ff head、合同付款预警 h2c3d4e5f6a7 head。
逻辑:不变更任何表或数据;仅声明两条已完成迁移线在此收敛。
"""
from alembic import op # noqa: F401 - Alembic migration module contract
revision = "i3d4e5f6a7b8"
down_revision = ("58915a6f02ff", "h2c3d4e5f6a7")
branch_labels = None
depends_on = None
def upgrade() -> None:
"""合并历史迁移头与合同付款预警迁移头,不执行数据库结构变更。"""
def downgrade() -> None:
"""回退时由 Alembic 分别沿两条父迁移链继续处理。"""
@@ -4,28 +4,24 @@ Revision ID: j4e5f6a7b8c9
Revises: 58915a6f02ff
用途:一次性创建合同付款预警域的合同、付款节点、OA 请款、逐笔预警和周汇总批次表。
依赖:项目现有主迁移头 58915a6f02ff。
依赖:项目现有主迁移头 58915a6f02ff 与发货通知迁移头 hk5l6m7n8o9p。
逻辑:将此前拆分的基础表、流程编号、三轮收件人和周汇总演进合并为单一迁移,
新部署只执行一次;合同编号允许先为空,OA 流程编号和申请人独立保存。
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect
revision = "j4e5f6a7b8c9"
down_revision = "58915a6f02ff"
down_revision = ("58915a6f02ff", "hk5l6m7n8o9p")
branch_labels = None
depends_on = None
def upgrade() -> None:
"""创建合同付款域全部业务表、索引和约束。"""
# 历史部署曾通过 i3d4e5f6a7b8 及其父迁移创建过同名合同付款表。
# 这些数据库只需要收敛版本图,不能再次执行整套 CREATE TABLE。
if inspect(op.get_bind()).has_table("contract_payment_contracts"):
return
op.execute("SELECT pg_advisory_xact_lock(781246903)")
op.create_table(
"contract_payment_contracts",
sa.Column("id", sa.Integer(), nullable=False),
@@ -186,6 +182,20 @@ def upgrade() -> None:
)
op.create_index("ix_contract_payment_alert_batches_status_created", "contract_payment_alert_batches", ["status", "created_at"])
op.create_table(
"contract_payment_sync_states",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("state_key", sa.String(length=64), nullable=False),
sa.Column("last_successful_sync_at", sa.DateTime(), nullable=True),
sa.Column("last_archive_seen_at", sa.DateTime(), nullable=True),
sa.Column("last_run_at", sa.DateTime(), nullable=True),
sa.Column("synced_contract_count", sa.Integer(), nullable=False, server_default="0"),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("state_key", name="uq_contract_payment_sync_states_state_key"),
)
# 历史首批数据曾将明确的 HT 流程号写入合同编号,合并迁移时一并纠正。
op.execute(
"UPDATE contract_payment_contracts "
@@ -197,6 +207,7 @@ def upgrade() -> None:
def downgrade() -> None:
"""按依赖逆序删除合同付款域全部表。"""
op.drop_table("contract_payment_alert_batches")
op.drop_table("contract_payment_sync_states")
op.drop_table("contract_payment_alerts")
op.drop_table("contract_payment_vendor_requests")
op.drop_table("contract_payment_events")
@@ -1,26 +0,0 @@
"""合并当前 Alembic 多头迁移。
用途:将历史修复、发货复核流和合同付款域三条并行迁移链收敛为单一 head,
使 ``flask db upgrade`` 不再因 ``head`` 歧义而中止。
依赖:``i3d4e5f6a7b8`` 和当前合同付款域迁移 ``j4e5f6a7b8c9``。
逻辑:本迁移不修改业务表,仅建立 Alembic 图上的合并节点;三条父链的升级内容
仍分别由各自迁移执行。
"""
from alembic import op
revision = "k5f6a7b8c9d0"
down_revision = ("i3d4e5f6a7b8", "hk5l6m7n8o9p", "j4e5f6a7b8c9")
branch_labels = None
depends_on = None
def upgrade() -> None:
"""合并迁移分支;不执行额外数据库 DDL。"""
pass
def downgrade() -> None:
"""撤销合并节点;不会回滚三个父分支的业务变更。"""
pass
@@ -1,60 +0,0 @@
"""补齐历史合同付款库缺失的预警汇总批次表。
用途:修复旧版合同付款迁移已创建基础表、但因兼容路径跳过
``contract_payment_alert_batches`` 的数据库。
依赖:当前合并迁移头 ``k5f6a7b8c9d0``。
逻辑:仅在目标表不存在时创建表和索引,保留已有合同付款数据;
页面读取周预警汇总批次时不再触发 PostgreSQL UndefinedTable。
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect
revision = "l6a7b8c9d0e1"
down_revision = "k5f6a7b8c9d0"
branch_labels = None
depends_on = None
def upgrade() -> None:
"""为历史库补建预警汇总批次表及其必要索引。"""
if inspect(op.get_bind()).has_table("contract_payment_alert_batches"):
return
op.create_table(
"contract_payment_alert_batches",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("batch_no", sa.String(length=64), nullable=False),
sa.Column("week_key", sa.String(length=16), nullable=False),
sa.Column("recipient_user_id", sa.Integer(), nullable=False),
sa.Column("status", sa.String(length=32), nullable=False, server_default="pending"),
sa.Column("summary_json", sa.JSON(), nullable=True),
sa.Column("excel_file_name", sa.String(length=255), nullable=True),
sa.Column("excel_file_data", sa.LargeBinary(), nullable=True),
sa.Column("excel_file_size", sa.Integer(), nullable=True),
sa.Column("sent_at", sa.DateTime(), nullable=True),
sa.Column("wecom_trace_json", sa.JSON(), nullable=True),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.ForeignKeyConstraint(["recipient_user_id"], ["users.id"]),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("batch_no"),
sa.UniqueConstraint(
"week_key",
"recipient_user_id",
name="uq_contract_payment_alert_batches_week_recipient",
),
)
op.create_index(
"ix_contract_payment_alert_batches_status_created",
"contract_payment_alert_batches",
["status", "created_at"],
)
def downgrade() -> None:
"""仅删除由本迁移补建的预警汇总批次表。"""
if inspect(op.get_bind()).has_table("contract_payment_alert_batches"):
op.drop_table("contract_payment_alert_batches")
@@ -1,43 +0,0 @@
"""新增合同付款 OA 增量同步断点表。
用途:记录合同付款预警 OA 通知同步上次成功位置,作为时间窗口增量拉取的游标,
避免每次全量重扫待办/已办列表;同一 state_key 只存一行,由唯一约束保证。
依赖:当前合并迁移头 ``l6a7b8c9d0e1``。
逻辑:仅在目标表不存在时创建;业务代码以 state_key 读写断点,供 sync_service 计算增量窗口。
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect
revision = "m7e8f9a0b1c2"
down_revision = "l6a7b8c9d0e1"
branch_labels = None
depends_on = None
def upgrade() -> None:
"""为 OA 增量同步补建断点表,并保证 state_key 唯一。"""
if inspect(op.get_bind()).has_table("contract_payment_sync_states"):
return
op.create_table(
"contract_payment_sync_states",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("state_key", sa.String(length=64), nullable=False),
sa.Column("last_successful_sync_at", sa.DateTime(), nullable=True),
sa.Column("last_archive_seen_at", sa.DateTime(), nullable=True),
sa.Column("last_run_at", sa.DateTime(), nullable=True),
sa.Column("synced_contract_count", sa.Integer(), nullable=False, server_default="0"),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("state_key", name="uq_contract_payment_sync_states_state_key"),
)
def downgrade() -> None:
"""删除 OA 增量同步断点表。"""
if inspect(op.get_bind()).has_table("contract_payment_sync_states"):
op.drop_table("contract_payment_sync_states")
@@ -3,7 +3,7 @@
用途:将 ``dev`` 上的发货通知 head 与 ``contract-payment`` 的增量同步 head 收成一条链,
避免 ``flask db upgrade`` 因 Multiple head revisions 失败。
依赖:``hw6x7y8z9a0b``(shipping_task_events.source 扩宽)、
``m7e8f9a0b1c2``(contract_payment_sync_states)。
``j4e5f6a7b8c9``(合同付款域统一基础迁移)。
逻辑:不修改业务表,只声明两条已完成迁移线在此收敛。
"""
@@ -11,7 +11,7 @@ from alembic import op # noqa: F401 - Alembic migration module contract
revision = "n8f9a0b1c2d3"
down_revision = ("hw6x7y8z9a0b", "m7e8f9a0b1c2")
down_revision = ("j4e5f6a7b8c9", "hw6x7y8z9a0b")
branch_labels = None
depends_on = None