fix(fulfillment): 修复历史账号绑定覆盖当前待确认角色

This commit is contained in:
yml
2026-11-23 19:42:38 +08:00
parent 17ec07b63a
commit 0a32f24fa8
7 changed files with 347 additions and 9 deletions
@@ -501,6 +501,10 @@
"20261123_0125_douyu_collab_shop_aliases.py": {
"revision": "20261123_0125",
"sha256": "ff11ab9c735043d95504c1d52ddfeb8db04068a8027e0e1253ec45330f227bad"
},
"20261123_0126_repair_false_binding_sync.py": {
"revision": "20261123_0126",
"sha256": "86c201d2298845f7f3eb3e733760fe46911638dc1ef046d4518c02172cfaba6f"
}
}
}
@@ -0,0 +1,112 @@
"""repair fulfillment tasks promoted by historical account bindings.
Revision ID: 20261123_0126
Revises: 20261123_0125
"""
from __future__ import annotations
import json
from datetime import UTC, datetime
import sqlalchemy as sa
from alembic import op
revision: str = "20261123_0126"
down_revision: str | None = "20261123_0125"
branch_labels: str | None = None
depends_on: str | None = None
def upgrade() -> None:
bind = op.get_bind()
rows = bind.execute(
sa.text(
"""
SELECT DISTINCT
t.id AS task_id,
a.id AS account_id,
a.username,
a.nickname,
a.game_name AS bound_role_name,
d.id AS bind_task_id,
JSON_UNQUOTE(JSON_EXTRACT(d.result, '$.role_name')) AS pending_role_name,
JSON_UNQUOTE(JSON_EXTRACT(d.result, '$.game_channel')) AS pending_channel
FROM platform_fulfillment_tasks AS t
JOIN accounts AS a ON a.id = t.account_id
JOIN douyu_tasks AS d ON d.id = t.bind_task_id
JOIN platform_fulfillment_events AS e
ON e.task_id = t.id
AND e.event_type = 'binding_synced_from_account'
WHERE t.binding_status = 'bound'
AND t.execution_status = 'bound'
AND d.task_type IN ('get_bind_qr', 'query_game_name')
AND JSON_EXTRACT(d.result, '$.bind_ready_for_confirm') = true
AND COALESCE(JSON_EXTRACT(d.result, '$.bind_confirmed'), false) = false
AND TRIM(COALESCE(a.game_name, '')) <> TRIM(
COALESCE(
JSON_UNQUOTE(JSON_EXTRACT(d.result, '$.role_name')),
''
)
)
"""
)
).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 = 'role_ready',
execution_status = 'role_ready',
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 ""),
"bindTaskId": int(row["bind_task_id"]),
"previousBindingStatus": "bound",
"correctedBindingStatus": "role_ready",
"boundRoleName": str(row["bound_role_name"] or ""),
"pendingRoleName": str(row["pending_role_name"] or ""),
"pendingChannel": str(row["pending_channel"] or ""),
"reason": "historical_account_binding_not_current_order_confirmation",
}
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_false_confirmation_reverted',
'system',
'',
:detail_json,
:created_at
)
"""
),
{
"task_id": task_id,
"detail_json": json.dumps(detail, ensure_ascii=False),
"created_at": now,
},
)
def downgrade() -> None:
"""数据修复不回退,避免再次用历史账号绑定覆盖当前待确认角色。"""