fix(douyu): 统一外部协作账号租约

This commit is contained in:
yml
2026-11-19 16:03:17 +08:00
parent acd27bde67
commit dd48175b4a
8 changed files with 1248 additions and 79 deletions
@@ -0,0 +1,243 @@
"""把进行中的斗鱼外部协作单接入统一账号租约。
只给当前没有活动租约的账号补租约。已经被平台履约等业务占用的账号保持原状,
并写一条冲突事件供账号时间线追溯,避免迁移抢占正在执行的链路。
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision: str = "20261119_0098"
down_revision: str | None = "20261119_0097"
branch_labels: str | None = None
depends_on: str | None = None
ACTIVE_COLLAB_STATUSES = (
"incomplete",
"to_recharge",
"recharged",
"scripted",
"points_error",
"failed",
)
def upgrade() -> None:
active_status_sql = ", ".join(f"'{item}'" for item in ACTIVE_COLLAB_STATUSES)
op.execute(
sa.text(
f"""
INSERT INTO account_leases (
account_id,
holder_user_id,
scope,
purpose,
commodity_id,
batch_id,
status,
leased_at,
expires_at,
released_at,
release_reason,
created_at
)
SELECT
candidates.account_id,
candidates.holder_user_id,
'elite',
'douyu_collab',
'',
CONCAT('collab:', candidates.account_id),
'active',
UTC_TIMESTAMP(),
NULL,
NULL,
'migration_backfill',
UTC_TIMESTAMP()
FROM (
SELECT DISTINCT
o.account_id AS account_id,
COALESCE(
(
SELECT o2.owner_id
FROM douyu_external_orders o2
WHERE o2.account_id = o.account_id
AND o2.owner_id IS NOT NULL
AND o2.archived_at IS NULL
AND o2.status IN ({active_status_sql})
ORDER BY o2.updated_at DESC, o2.id DESC
LIMIT 1
),
a.assigned_to,
(
SELECT u.id
FROM users u
WHERE u.is_active = 1
AND u.deleted_at IS NULL
ORDER BY
CASE u.role
WHEN 'super_admin' THEN 0
WHEN 'operation' THEN 1
ELSE 2
END,
u.id ASC
LIMIT 1
)
) AS holder_user_id
FROM douyu_external_orders o
JOIN accounts a ON a.id = o.account_id
WHERE o.account_id IS NOT NULL
AND o.archived_at IS NULL
AND o.status IN ({active_status_sql})
) AS candidates
WHERE candidates.holder_user_id IS NOT NULL
AND NOT EXISTS (
SELECT 1
FROM account_leases active_lease
WHERE active_lease.account_id = candidates.account_id
AND active_lease.status = 'active'
)
"""
)
)
op.execute(
sa.text(
"""
INSERT INTO account_events (
account_id,
event_type,
category,
occurred_at,
title,
amount,
balance_after,
subject_type,
subject_key,
detail,
source,
actor_type,
actor_id,
source_ref,
created_at
)
SELECT
lease.account_id,
'lease_acquired',
'ops',
UTC_TIMESTAMP(),
CONCAT('外部协作账号占用回填:账号 #', lease.account_id),
NULL,
NULL,
'lease',
CONCAT('collab:', lease.account_id),
JSON_OBJECT(
'lease_id', lease.id,
'account_id', lease.account_id,
'purpose', lease.purpose,
'batch_id', lease.batch_id,
'backfill', TRUE
),
'migration',
'system',
NULL,
CONCAT('account_lease_backfill:', lease.id),
UTC_TIMESTAMP()
FROM account_leases lease
WHERE lease.status = 'active'
AND lease.purpose = 'douyu_collab'
AND lease.release_reason = 'migration_backfill'
"""
)
)
op.execute(
sa.text(
f"""
INSERT INTO account_events (
account_id,
event_type,
category,
occurred_at,
title,
amount,
balance_after,
subject_type,
subject_key,
detail,
source,
actor_type,
actor_id,
source_ref,
created_at
)
SELECT DISTINCT
active_order.account_id,
'lease_conflict',
'ops',
UTC_TIMESTAMP(),
CONCAT(
'外部协作账号已被占用:',
active_order.account_id,
' / ',
active_lease.purpose
),
NULL,
NULL,
'lease',
CONCAT('collab:', active_order.account_id),
JSON_OBJECT(
'lease_id', active_lease.id,
'account_id', active_order.account_id,
'existing_purpose', active_lease.purpose,
'existing_batch_id', active_lease.batch_id,
'conflict', TRUE
),
'migration',
'system',
NULL,
CONCAT(
'account_lease_collab_conflict:',
active_order.account_id,
':',
active_lease.id
),
UTC_TIMESTAMP()
FROM douyu_external_orders active_order
JOIN account_leases active_lease
ON active_lease.account_id = active_order.account_id
AND active_lease.status = 'active'
WHERE active_order.account_id IS NOT NULL
AND active_order.archived_at IS NULL
AND active_order.status IN ({active_status_sql})
AND active_lease.purpose <> 'douyu_collab'
"""
)
)
def downgrade() -> None:
op.execute(
sa.text(
"""
DELETE FROM account_events
WHERE source_ref LIKE 'account_lease_backfill:%'
OR source_ref LIKE 'account_lease_collab_conflict:%'
"""
)
)
op.execute(
sa.text(
"""
UPDATE account_leases
SET status = 'released',
released_at = UTC_TIMESTAMP(),
release_reason = 'migration_rollback'
WHERE status = 'active'
AND purpose = 'douyu_collab'
AND release_reason = 'migration_backfill'
"""
)
)