feat(fulfillment): 支持手动同步斗鱼外部单协作

This commit is contained in:
yml
2026-11-18 21:02:11 +08:00
parent 631862217d
commit c2d47b98ab
15 changed files with 912 additions and 3 deletions
@@ -0,0 +1,137 @@
"""斗鱼外部单协作关联直播履约来源。
手工协作单保持 ``source_type=manual``;从直播履约同步的订单通过唯一
``source_task_id`` 幂等关联,重复点击同步不会生成重复订单。
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision: str = "20261118_0094"
down_revision: str | None = "20261118_0093"
branch_labels: str | None = None
depends_on: str | None = None
TABLE = "douyu_external_orders"
INDEX = "uq_douyu_external_source_task"
FOREIGN_KEY = "fk_douyu_external_orders_source_task"
def _column_exists(name: str) -> bool:
return bool(
op.get_bind()
.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()
)
def _index_exists(name: str) -> bool:
return bool(
op.get_bind()
.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()
)
def _foreign_key_exists(name: str) -> bool:
return any(
item.get("name") == name
for item in sa.inspect(op.get_bind()).get_foreign_keys(TABLE)
)
def upgrade() -> None:
if not _column_exists("source_type"):
op.add_column(
TABLE,
sa.Column(
"source_type",
sa.String(length=24),
nullable=False,
server_default="manual",
),
)
if not _column_exists("source_task_id"):
op.add_column(
TABLE,
sa.Column("source_task_id", sa.BigInteger(), nullable=True),
)
if not _column_exists("source_order_id"):
op.add_column(
TABLE,
sa.Column(
"source_order_id",
sa.String(length=64),
nullable=False,
server_default="",
),
)
if not _column_exists("source_order_item_id"):
op.add_column(
TABLE,
sa.Column(
"source_order_item_id",
sa.String(length=64),
nullable=False,
server_default="",
),
)
if not _column_exists("source_platform_order_id"):
op.add_column(
TABLE,
sa.Column(
"source_platform_order_id",
sa.String(length=128),
nullable=False,
server_default="",
),
)
if not _column_exists("last_synced_at"):
op.add_column(
TABLE,
sa.Column("last_synced_at", sa.DateTime(), nullable=True),
)
if not _index_exists(INDEX):
op.create_index(INDEX, TABLE, ["source_task_id"], unique=True)
if not _foreign_key_exists(FOREIGN_KEY):
op.create_foreign_key(
FOREIGN_KEY,
TABLE,
"platform_fulfillment_tasks",
["source_task_id"],
["id"],
ondelete="SET NULL",
)
def downgrade() -> None:
if _foreign_key_exists(FOREIGN_KEY):
op.drop_constraint(FOREIGN_KEY, TABLE, type_="foreignkey")
if _index_exists(INDEX):
op.drop_index(INDEX, table_name=TABLE)
for column in (
"last_synced_at",
"source_platform_order_id",
"source_order_item_id",
"source_order_id",
"source_task_id",
"source_type",
):
if _column_exists(column):
op.drop_column(TABLE, column)