perf(fulfillment): 优化本地能力选号查询与索引

This commit is contained in:
yml
2026-11-23 18:53:06 +08:00
parent d5422b0ecd
commit 7e0d27927c
23 changed files with 1624 additions and 95 deletions
@@ -0,0 +1,73 @@
"""add covering index for fulfillment capability candidates.
Revision ID: 20261123_0123
Revises: 20261123_0122
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision: str = "20261123_0123"
down_revision: str | None = "20261123_0122"
branch_labels: str | None = None
depends_on: str | None = None
INDEX_NAME = "ix_account_commodity_candidate"
REPLACED_INDEX_NAME = "ix_account_commodity_eligibility"
def _indexes() -> set[str]:
return {
str(index["name"])
for index in sa.inspect(op.get_bind()).get_indexes(
"account_commodity_states"
)
if index.get("name")
}
def upgrade() -> None:
indexes = _indexes()
if INDEX_NAME not in indexes:
op.create_index(
INDEX_NAME,
"account_commodity_states",
[
"platform",
"scope",
"commodity_id",
"account_eligibility",
"offer_state",
"valid_until",
"account_id",
"last_exchange_at",
"observed_at",
],
)
if REPLACED_INDEX_NAME in indexes:
op.drop_index(
REPLACED_INDEX_NAME,
table_name="account_commodity_states",
)
def downgrade() -> None:
indexes = _indexes()
if REPLACED_INDEX_NAME not in indexes:
op.create_index(
REPLACED_INDEX_NAME,
"account_commodity_states",
[
"platform",
"scope",
"commodity_id",
"account_eligibility",
"offer_state",
"valid_until",
"account_id",
],
)
if INDEX_NAME in indexes:
op.drop_index(INDEX_NAME, table_name="account_commodity_states")
@@ -0,0 +1,104 @@
"""backfill active fulfillment bindings confirmed through the account workbench.
Revision ID: 20261123_0124
Revises: 20261123_0123
"""
from __future__ import annotations
import json
from datetime import UTC, datetime
import sqlalchemy as sa
from alembic import op
revision: str = "20261123_0124"
down_revision: str | None = "20261123_0123"
branch_labels: str | None = None
depends_on: str | None = None
def upgrade() -> None:
bind = op.get_bind()
rows = bind.execute(
sa.text(
"""
SELECT
t.id AS task_id,
a.id AS account_id,
a.username,
a.nickname,
a.game_name,
a.game_channel,
a.role_change_available_at
FROM platform_fulfillment_tasks AS t
JOIN accounts AS a ON a.id = t.account_id
JOIN account_leases AS l ON l.id = t.lease_id
WHERE t.scope = 'elite'
AND t.binding_status IN ('role_ready', 'confirming')
AND t.execution_status NOT IN ('completed', 'cancelled')
AND l.status = 'active'
AND a.role_bind_status = 'bound'
"""
)
).mappings().all()
now = datetime.now(UTC).replace(tzinfo=None)
for row in rows:
task_id = int(row["task_id"])
bind.execute(
sa.text(
"""
UPDATE platform_fulfillment_tasks
SET binding_status = 'bound',
execution_status = 'bound',
issue_code = '',
last_error = '',
updated_at = :now
WHERE id = :task_id
"""
),
{"task_id": task_id, "now": now},
)
detail = {
"accountId": int(row["account_id"]),
"accountName": str(row["nickname"] or row["username"] or ""),
"roleName": str(row["game_name"] or ""),
"gameChannel": str(row["game_channel"] or ""),
"roleChangeAvailableAt": (
row["role_change_available_at"].isoformat()
if row["role_change_available_at"]
else None
),
"sourceTaskId": None,
"backfilled": True,
}
bind.execute(
sa.text(
"""
INSERT INTO platform_fulfillment_events (
task_id,
event_type,
actor_type,
actor_id,
detail_json,
created_at
) VALUES (
:task_id,
'binding_synced_from_account',
'system',
'',
:detail_json,
:created_at
)
"""
),
{
"task_id": task_id,
"detail_json": json.dumps(detail, ensure_ascii=False),
"created_at": now,
},
)
def downgrade() -> None:
"""业务数据回填不回退,避免把已确认的账号重新标成待确认。"""
@@ -0,0 +1,58 @@
"""add manually maintained douyu collab shop aliases and original shop names.
Revision ID: 20261123_0125
Revises: 20261123_0124
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision: str = "20261123_0125"
down_revision: str | None = "20261123_0124"
branch_labels: str | None = None
depends_on: str | None = None
def _columns(table: str) -> set[str]:
return {
str(column["name"])
for column in sa.inspect(op.get_bind()).get_columns(table)
}
def upgrade() -> None:
bind = op.get_bind()
if "aliases" not in _columns("douyu_collab_shops"):
op.add_column(
"douyu_collab_shops",
sa.Column("aliases", sa.JSON(), nullable=True),
)
if "shop_original_name" not in _columns("douyu_external_orders"):
op.add_column(
"douyu_external_orders",
sa.Column(
"shop_original_name",
sa.String(length=128),
nullable=False,
server_default="",
),
)
bind.execute(
sa.text(
"""
UPDATE douyu_external_orders
SET shop_original_name = shop
WHERE shop_original_name = ''
"""
)
)
def downgrade() -> None:
if "shop_original_name" in _columns("douyu_external_orders"):
op.drop_column("douyu_external_orders", "shop_original_name")
if "aliases" in _columns("douyu_collab_shops"):
op.drop_column("douyu_collab_shops", "aliases")