feat(fulfillment): 新增直播账号履约工作台
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
"""账号租约 `account_leases` + `accounts.role_change_available_at` + 操作人索引
|
||||
|
||||
配套文档:`docs/设计-账号自动分配与回收.md`(§3.2 表结构、§4 换绑冷却、§3.14 流水)。
|
||||
|
||||
三件事:
|
||||
|
||||
1. **`account_leases`** —— 归属(`assigned_to`,管理员/运营的维护责任)与
|
||||
使用权(租约,客服临时占用)分离。核心是生成列 `active_account_id`
|
||||
加唯一索引 `uq_lease_active_account`:一个账号同时只能有一个**有效**租约,
|
||||
并发抢号由数据库裁决(撞唯一键即换候选),不需要应用层加锁。
|
||||
2. **`accounts.role_change_available_at`** —— `change_role_wait_time` 是接口原文的
|
||||
「剩余秒数」,落库即过期;换绑判定需要的是**绝对时刻**。电竞手册复用既有的
|
||||
`esports_can_change_time`(本来就是绝对时刻),不另开副本。
|
||||
3. **`ix_account_events_actor_occurred`** —— 池化后客服不再持有账号,
|
||||
「某客服这段时间做了什么」只能按 `actor_id` 查;此前没有可用索引。
|
||||
|
||||
**可重跑**:应用启动时会自动跑迁移,所以每个动作都带存在性守卫。
|
||||
|
||||
**回退**:drop 表、drop 列、drop 索引。租约是运行期状态,回退即丢失 ——
|
||||
但账号与任务数据不受影响(`assigned_to`、`douyu_tasks` 一律没动)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261117_0083"
|
||||
down_revision: str | None = "20261113_0082"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
TABLE = "account_leases"
|
||||
ACCOUNT_COLUMN = "role_change_available_at"
|
||||
ACTOR_INDEX = "ix_account_events_actor_occurred"
|
||||
|
||||
#: 「一个账号同时只能有一个有效租约」的数据库级保证。
|
||||
ACTIVE_ACCOUNT_EXPR = "CASE WHEN status = 'active' THEN account_id ELSE NULL END"
|
||||
|
||||
|
||||
def _table_exists(name: str) -> bool:
|
||||
conn = op.get_bind()
|
||||
count = conn.execute(
|
||||
sa.text(
|
||||
"SELECT COUNT(*) FROM information_schema.tables "
|
||||
"WHERE table_schema = DATABASE() AND table_name = :name"
|
||||
),
|
||||
{"name": name},
|
||||
).scalar()
|
||||
return bool(count)
|
||||
|
||||
|
||||
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 _table_exists(TABLE):
|
||||
op.create_table(
|
||||
TABLE,
|
||||
sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
|
||||
sa.Column("account_id", sa.Integer(), nullable=False),
|
||||
sa.Column("holder_user_id", sa.Integer(), nullable=False),
|
||||
sa.Column(
|
||||
"scope", sa.String(length=16), nullable=False, server_default="elite"
|
||||
),
|
||||
sa.Column(
|
||||
"purpose",
|
||||
sa.String(length=24),
|
||||
nullable=False,
|
||||
server_default="exchange",
|
||||
),
|
||||
sa.Column(
|
||||
"commodity_id", sa.String(length=64), nullable=False, server_default=""
|
||||
),
|
||||
sa.Column(
|
||||
"batch_id", sa.String(length=64), nullable=False, server_default=""
|
||||
),
|
||||
sa.Column(
|
||||
"status", sa.String(length=16), nullable=False, server_default="active"
|
||||
),
|
||||
sa.Column("leased_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("expires_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("released_at", sa.DateTime(), nullable=True),
|
||||
sa.Column(
|
||||
"release_reason",
|
||||
sa.String(length=64),
|
||||
nullable=False,
|
||||
server_default="",
|
||||
),
|
||||
# ⚠️ 生成列必须排在 account_id / status 之后:MySQL 只允许生成列
|
||||
# 引用它前面已定义的列。
|
||||
sa.Column(
|
||||
"active_account_id",
|
||||
sa.Integer(),
|
||||
sa.Computed(ACTIVE_ACCOUNT_EXPR, persisted=True),
|
||||
nullable=True,
|
||||
),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.ForeignKeyConstraint(
|
||||
["account_id"], ["accounts.id"], name="fk_lease_account"
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["holder_user_id"], ["users.id"], name="fk_lease_holder"
|
||||
),
|
||||
)
|
||||
|
||||
if not _index_exists(TABLE, "uq_lease_active_account"):
|
||||
op.create_index(
|
||||
"uq_lease_active_account", TABLE, ["active_account_id"], unique=True
|
||||
)
|
||||
if not _index_exists(TABLE, "ix_leases_holder_status"):
|
||||
op.create_index("ix_leases_holder_status", TABLE, ["holder_user_id", "status"])
|
||||
if not _index_exists(TABLE, "ix_leases_status_expires"):
|
||||
op.create_index("ix_leases_status_expires", TABLE, ["status", "expires_at"])
|
||||
if not _index_exists(TABLE, "ix_account_leases_account_id"):
|
||||
op.create_index("ix_account_leases_account_id", TABLE, ["account_id"])
|
||||
|
||||
if not _column_exists("accounts", ACCOUNT_COLUMN):
|
||||
op.add_column(
|
||||
"accounts",
|
||||
sa.Column(
|
||||
ACCOUNT_COLUMN,
|
||||
sa.DateTime(),
|
||||
nullable=True,
|
||||
comment="精英宝典可换绑时刻(绝对时间);换绑冷却只记录不拦截",
|
||||
),
|
||||
)
|
||||
|
||||
if not _index_exists("account_events", ACTOR_INDEX):
|
||||
op.create_index(ACTOR_INDEX, "account_events", ["actor_id", "occurred_at"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _index_exists("account_events", ACTOR_INDEX):
|
||||
op.drop_index(ACTOR_INDEX, table_name="account_events")
|
||||
if _column_exists("accounts", ACCOUNT_COLUMN):
|
||||
op.drop_column("accounts", ACCOUNT_COLUMN)
|
||||
if _table_exists(TABLE):
|
||||
op.drop_table(TABLE)
|
||||
@@ -0,0 +1,252 @@
|
||||
"""直播账号履约:SKU 映射、履约作业与事件
|
||||
|
||||
Revision ID: 20261117_0084
|
||||
Revises: 20261117_0083
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261117_0084"
|
||||
down_revision: str | None = "20261117_0083"
|
||||
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("platform_commodity_mappings"):
|
||||
op.create_table(
|
||||
"platform_commodity_mappings",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column(
|
||||
"target_platform",
|
||||
sa.String(length=16),
|
||||
nullable=False,
|
||||
server_default="douyu",
|
||||
),
|
||||
sa.Column("internal_sku_key", sa.String(length=255), nullable=False),
|
||||
sa.Column("commodity_id", sa.String(length=64), nullable=False),
|
||||
sa.Column(
|
||||
"goods_name", sa.String(length=255), nullable=False, server_default=""
|
||||
),
|
||||
sa.Column("required_points", sa.Integer(), nullable=True),
|
||||
sa.Column(
|
||||
"scope", sa.String(length=16), nullable=False, server_default="elite"
|
||||
),
|
||||
sa.Column(
|
||||
"enabled", sa.Boolean(), nullable=False, server_default=sa.text("1")
|
||||
),
|
||||
sa.Column("created_by", sa.Integer(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["created_by"], ["users.id"]),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint(
|
||||
"target_platform",
|
||||
"internal_sku_key",
|
||||
name="uq_platform_commodity_mapping_sku",
|
||||
),
|
||||
)
|
||||
if not _index_exists("ix_platform_commodity_mapping_enabled"):
|
||||
op.create_index(
|
||||
"ix_platform_commodity_mapping_enabled",
|
||||
"platform_commodity_mappings",
|
||||
["target_platform", "enabled"],
|
||||
)
|
||||
|
||||
if not _table_exists("platform_fulfillment_tasks"):
|
||||
op.create_table(
|
||||
"platform_fulfillment_tasks",
|
||||
sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
|
||||
sa.Column(
|
||||
"source_system",
|
||||
sa.String(length=32),
|
||||
nullable=False,
|
||||
server_default="order_site",
|
||||
),
|
||||
sa.Column("source_order_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("source_order_item_id", sa.String(length=64), nullable=False),
|
||||
sa.Column(
|
||||
"source_fulfillment_id",
|
||||
sa.String(length=64),
|
||||
nullable=False,
|
||||
server_default="",
|
||||
),
|
||||
sa.Column(
|
||||
"platform_order_id",
|
||||
sa.String(length=128),
|
||||
nullable=False,
|
||||
server_default="",
|
||||
),
|
||||
sa.Column(
|
||||
"provider", sa.String(length=32), nullable=False, server_default=""
|
||||
),
|
||||
sa.Column(
|
||||
"shop_id", sa.String(length=64), nullable=False, server_default=""
|
||||
),
|
||||
sa.Column(
|
||||
"shop_name", sa.String(length=255), nullable=False, server_default=""
|
||||
),
|
||||
sa.Column(
|
||||
"sku_code", sa.String(length=255), nullable=False, server_default=""
|
||||
),
|
||||
sa.Column(
|
||||
"sku_name", sa.String(length=512), nullable=False, server_default=""
|
||||
),
|
||||
sa.Column(
|
||||
"internal_sku_key",
|
||||
sa.String(length=255),
|
||||
nullable=False,
|
||||
server_default="",
|
||||
),
|
||||
sa.Column("quantity", sa.Integer(), nullable=False, server_default="1"),
|
||||
sa.Column(
|
||||
"order_amount_fen", sa.Integer(), nullable=False, server_default="0"
|
||||
),
|
||||
sa.Column(
|
||||
"target_platform",
|
||||
sa.String(length=16),
|
||||
nullable=False,
|
||||
server_default="douyu",
|
||||
),
|
||||
sa.Column(
|
||||
"scope", sa.String(length=16), nullable=False, server_default="elite"
|
||||
),
|
||||
sa.Column("commodity_mapping_id", sa.Integer(), nullable=True),
|
||||
sa.Column(
|
||||
"commodity_id", sa.String(length=64), nullable=False, server_default=""
|
||||
),
|
||||
sa.Column("required_points", sa.Integer(), nullable=True),
|
||||
sa.Column("account_id", sa.Integer(), nullable=True),
|
||||
sa.Column("lease_id", sa.BigInteger(), nullable=True),
|
||||
sa.Column("bind_task_id", sa.Integer(), nullable=True),
|
||||
sa.Column("exchange_task_id", sa.Integer(), nullable=True),
|
||||
sa.Column(
|
||||
"binding_status",
|
||||
sa.String(length=24),
|
||||
nullable=False,
|
||||
server_default="not_started",
|
||||
),
|
||||
sa.Column(
|
||||
"execution_status",
|
||||
sa.String(length=24),
|
||||
nullable=False,
|
||||
server_default="pending_mapping",
|
||||
),
|
||||
sa.Column(
|
||||
"exchange_status",
|
||||
sa.String(length=24),
|
||||
nullable=False,
|
||||
server_default="not_started",
|
||||
),
|
||||
sa.Column(
|
||||
"issue_code", sa.String(length=64), nullable=False, server_default=""
|
||||
),
|
||||
sa.Column("last_error", sa.Text(), nullable=False),
|
||||
sa.Column("source_snapshot_json", sa.JSON(), nullable=True),
|
||||
sa.Column("operator_id", sa.Integer(), nullable=True),
|
||||
sa.Column("completed_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(
|
||||
["commodity_mapping_id"],
|
||||
["platform_commodity_mappings.id"],
|
||||
ondelete="SET NULL",
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["account_id"], ["accounts.id"], ondelete="SET NULL"
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["lease_id"], ["account_leases.id"], ondelete="SET NULL"
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["bind_task_id"], ["douyu_tasks.id"], ondelete="SET NULL"
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["exchange_task_id"], ["douyu_tasks.id"], ondelete="SET NULL"
|
||||
),
|
||||
sa.ForeignKeyConstraint(["operator_id"], ["users.id"], ondelete="SET NULL"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint(
|
||||
"source_system",
|
||||
"source_order_item_id",
|
||||
name="uq_platform_fulfillment_source_item",
|
||||
),
|
||||
)
|
||||
for name, columns in (
|
||||
("ix_platform_fulfillment_tasks_internal_sku_key", ["internal_sku_key"]),
|
||||
("ix_platform_fulfillment_tasks_account_id", ["account_id"]),
|
||||
("ix_platform_fulfillment_status_updated", ["execution_status", "updated_at"]),
|
||||
("ix_platform_fulfillment_order", ["source_order_id", "source_order_item_id"]),
|
||||
):
|
||||
if not _index_exists(name):
|
||||
op.create_index(name, "platform_fulfillment_tasks", columns)
|
||||
|
||||
if not _table_exists("platform_fulfillment_events"):
|
||||
op.create_table(
|
||||
"platform_fulfillment_events",
|
||||
sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
|
||||
sa.Column("task_id", sa.BigInteger(), nullable=False),
|
||||
sa.Column("event_type", sa.String(length=64), nullable=False),
|
||||
sa.Column(
|
||||
"actor_type",
|
||||
sa.String(length=16),
|
||||
nullable=False,
|
||||
server_default="system",
|
||||
),
|
||||
sa.Column(
|
||||
"actor_id", sa.String(length=64), nullable=False, server_default=""
|
||||
),
|
||||
sa.Column("detail_json", sa.JSON(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(
|
||||
["task_id"], ["platform_fulfillment_tasks.id"], ondelete="CASCADE"
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
for name, columns in (
|
||||
("ix_platform_fulfillment_events_task_id", ["task_id"]),
|
||||
("ix_platform_fulfillment_events_task_created", ["task_id", "created_at"]),
|
||||
):
|
||||
if not _index_exists(name):
|
||||
op.create_index(name, "platform_fulfillment_events", columns)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
for table in (
|
||||
"platform_fulfillment_events",
|
||||
"platform_fulfillment_tasks",
|
||||
"platform_commodity_mappings",
|
||||
):
|
||||
if _table_exists(table):
|
||||
op.drop_table(table)
|
||||
Reference in New Issue
Block a user