Files
live-hub-py/web/backend/migrations/versions/20261124_0132_fulfillment_flow_version.py
T
yml aafac6e536 feat(fulfillment): 增加 Clean Cutover,历史任务隔离到人工入口
- 新增 flow_version,存量任务回填 1,Cutover 后新任务写 2
- 迁移设置 clean_flow_started_at,按订单支付/创建时间判定
- 自动选号只处理 clean 任务
- 工作台新增历史任务 Tab,历史列表不受订单起算时间限制
- 历史任务不参与自动重试和预售台账同步
- 补充流程边界测试和计划文档
2026-11-24 23:11:13 +08:00

92 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""直播履约 clean/legacy 流程分界。
- 存量任务统一标记为 legacy(flow_version=1);
- 迁移执行时设置 clean_flow_started_at,之后的订单进入 clean 流程;
- 已经是 clean 的任务不会再被自动调度逻辑回退。
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision: str = "20261124_0132"
down_revision: str | None = "20261124_0131"
branch_labels: str | None = None
depends_on: str | None = None
TASK_TABLE = "platform_fulfillment_tasks"
SETTINGS_TABLE = "platform_fulfillment_settings"
def _column_exists(table: str, name: str) -> bool:
conn = op.get_bind()
count = conn.execute(
sa.text(
"SELECT COUNT(*) FROM information_schema.columns "
"WHERE table_schema = DATABASE() AND table_name = :table "
"AND column_name = :name"
),
{"table": table, "name": name},
).scalar()
return bool(count)
def _index_exists(table: str, name: str) -> bool:
conn = op.get_bind()
count = conn.execute(
sa.text(
"SELECT COUNT(*) FROM information_schema.statistics "
"WHERE table_schema = DATABASE() AND table_name = :table "
"AND index_name = :name"
),
{"table": table, "name": name},
).scalar()
return bool(count)
def upgrade() -> None:
if not _column_exists(TASK_TABLE, "flow_version"):
op.add_column(
TASK_TABLE,
sa.Column(
"flow_version",
sa.Integer(),
nullable=False,
server_default="1",
),
)
if not _index_exists(TASK_TABLE, "ix_platform_fulfillment_tasks_flow_version"):
op.create_index(
"ix_platform_fulfillment_tasks_flow_version",
TASK_TABLE,
["flow_version"],
)
if not _column_exists(SETTINGS_TABLE, "clean_flow_started_at"):
op.add_column(
SETTINGS_TABLE,
sa.Column("clean_flow_started_at", sa.DateTime(), nullable=True),
)
# 迁移执行时刻就是 Cutover;存量任务已在 flow_version 默认值 1。
op.execute(
sa.text(
"""
UPDATE platform_fulfillment_settings
SET clean_flow_started_at = UTC_TIMESTAMP()
WHERE id = 1 AND clean_flow_started_at IS NULL
"""
)
)
def downgrade() -> None:
if _column_exists(SETTINGS_TABLE, "clean_flow_started_at"):
op.drop_column(SETTINGS_TABLE, "clean_flow_started_at")
if _index_exists(TASK_TABLE, "ix_platform_fulfillment_tasks_flow_version"):
op.drop_index(
"ix_platform_fulfillment_tasks_flow_version",
table_name=TASK_TABLE,
)
if _column_exists(TASK_TABLE, "flow_version"):
op.drop_column(TASK_TABLE, "flow_version")