Files
live-hub-py/web/backend/migrations/versions/20261117_0086_integration_outbox.py
T
yml bba3d9dac9 feat(fulfillment): 增加签名 Webhook 事务 Outbox
- 新增直播履约状态事务 Outbox 与签名投递 worker
- 增加 order_site 状态回传、重试和低频对账兜底
- 补充 Outbox、签名校验与集成接口测试
2026-11-18 10:59:20 +08:00

111 lines
3.9 KiB
Python

"""事务 Outbox 与入站 Webhook 幂等表"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision: str = "20261117_0086"
down_revision: str | None = "20261117_0085"
branch_labels: str | None = None
depends_on: str | None = None
def _table_exists(table: str) -> bool:
return bool(
op.get_bind()
.execute(
sa.text(
"SELECT COUNT(*) FROM information_schema.tables "
"WHERE table_schema = DATABASE() AND table_name = :table"
),
{"table": table},
)
.scalar()
)
def _index_exists(index: str) -> bool:
return bool(
op.get_bind()
.execute(
sa.text(
"SELECT COUNT(*) FROM information_schema.statistics "
"WHERE table_schema = DATABASE() AND index_name = :index"
),
{"index": index},
)
.scalar()
)
def upgrade() -> None:
if not _table_exists("integration_outbox"):
op.create_table(
"integration_outbox",
sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
sa.Column("event_id", sa.String(length=36), nullable=False),
sa.Column(
"destination",
sa.String(length=32),
nullable=False,
server_default="order_site",
),
sa.Column("event_type", sa.String(length=128), nullable=False),
sa.Column("dedupe_key", sa.String(length=255), nullable=True),
sa.Column("payload_json", sa.JSON(), nullable=False),
sa.Column(
"status",
sa.String(length=16),
nullable=False,
server_default="pending",
),
sa.Column("attempts", sa.Integer(), nullable=False, server_default="0"),
sa.Column("next_attempt_at", sa.DateTime(), nullable=False),
sa.Column("locked_at", sa.DateTime(), nullable=True),
sa.Column("sent_at", sa.DateTime(), nullable=True),
sa.Column("last_error", sa.Text(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("event_id", name="uq_integration_outbox_event_id"),
sa.UniqueConstraint("dedupe_key", name="uq_integration_outbox_dedupe_key"),
)
if not _index_exists("ix_integration_outbox_due"):
op.create_index(
"ix_integration_outbox_due",
"integration_outbox",
["status", "next_attempt_at", "id"],
)
if not _index_exists("ix_integration_outbox_destination"):
op.create_index(
"ix_integration_outbox_destination",
"integration_outbox",
["destination", "status", "id"],
)
if not _table_exists("integration_inbox"):
op.create_table(
"integration_inbox",
sa.Column("event_id", sa.String(length=36), nullable=False),
sa.Column(
"source", sa.String(length=32), nullable=False, server_default=""
),
sa.Column(
"event_type", sa.String(length=128), nullable=False, server_default=""
),
sa.Column("received_at", sa.DateTime(), nullable=False),
sa.Column("processed_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("event_id"),
)
def downgrade() -> None:
if _index_exists("ix_integration_outbox_destination"):
op.drop_index("ix_integration_outbox_destination", table_name="integration_outbox")
if _index_exists("ix_integration_outbox_due"):
op.drop_index("ix_integration_outbox_due", table_name="integration_outbox")
if _table_exists("integration_inbox"):
op.drop_table("integration_inbox")
if _table_exists("integration_outbox"):
op.drop_table("integration_outbox")