fix(douyu): 加固高价值三态迁移与状态口径

修复迁移中断重跑、旧快照回填覆盖、降级将 unknown 误写成可兑换,以及迁移前 CK 已失效账号未作废旧结论的问题。统一 CK error 的展示与筛选口径,补齐旧快照迁移和兑换兜底反向用例,并同步高价值结论时间语义。同时纳入 .gitignore 更新。
This commit is contained in:
yml2213
2026-11-06 13:25:45 +08:00
parent c9fbbdb828
commit 578e259eea
13 changed files with 286 additions and 68 deletions
@@ -4,6 +4,10 @@
本迁移增加两套独立状态和检查时间,并从旧快照保守回填:
只有快照里明确保留了官方 ``status=0/1`` 才恢复可兑换/已达上限;
更早的快照没有保存原始 status,统一回填为 unknown,等待一次新查询。
``high_value_checked_at`` 表示“最近一次形成高价值结论的时间”,结论本身可以是
unknown。旧快照虽然没保存原始 status,但确实形成过“当前无法判断”的结论,
因此保留旧查询时间,让这些账号按 unknown 的慢速策略重新确认。
"""
from __future__ import annotations
@@ -49,6 +53,26 @@ def _column_exists(name: str) -> bool:
return bool(count)
def _index_exists(name: str) -> bool:
count = (
op.get_bind()
.execute(
sa.text(
"""
SELECT COUNT(*)
FROM information_schema.statistics
WHERE table_schema = DATABASE()
AND table_name = 'accounts'
AND index_name = :name
"""
),
{"name": name},
)
.scalar()
)
return bool(count)
def _json_dict(value: Any) -> dict[str, Any]:
if isinstance(value, dict):
return value
@@ -83,14 +107,19 @@ def _as_datetime(value: Any) -> datetime | None:
def _backfill_from_snapshot() -> None:
"""只回填尚未形成新结论的旧账号,保证迁移中断后可安全重跑。"""
conn = op.get_bind()
rows = conn.execute(
sa.text(
"SELECT id, overview_snapshot, overview_queried_at "
"FROM accounts WHERE overview_snapshot IS NOT NULL"
"SELECT id, overview_snapshot, overview_queried_at, ck_check_status "
"FROM accounts "
"WHERE overview_snapshot IS NOT NULL "
"AND high_value_bairimeng_status IS NULL "
"AND high_value_mengtu_status IS NULL "
"AND high_value_checked_at IS NULL"
)
).fetchall()
for account_id, raw_snapshot, overview_queried_at in rows:
for account_id, raw_snapshot, overview_queried_at, ck_status in rows:
snapshot = _json_dict(raw_snapshot)
goods = snapshot.get("high_value_goods")
states = {key: STATUS_UNKNOWN for key in NAMES}
@@ -102,6 +131,11 @@ def _backfill_from_snapshot() -> None:
if core in name:
states[key] = _snapshot_state(item)
break
# 迁移前已经判定 CK 失效的账号,缓存结论同样不可继续信任。
# 否则部署后列表会显示待确认,但筛选/统计仍把它们算作可兑换,
# 直到下一次巡检才由 on_ck_verdict 补上作废。
if str(ck_status or "") == "invalid":
states = {key: STATUS_UNKNOWN for key in NAMES}
checked_at = _as_datetime(snapshot.get("queried_at") or overview_queried_at)
conn.execute(
sa.text(
@@ -123,6 +157,7 @@ def _backfill_from_snapshot() -> None:
def upgrade() -> None:
old_column_exists = _column_exists("high_value_owned")
for name in (
"high_value_bairimeng_status",
"high_value_mengtu_status",
@@ -137,7 +172,7 @@ def upgrade() -> None:
name,
sa.DateTime(),
nullable=True,
comment="最近一次成功读取官方 storehome 准入状态的时间",
comment="最近一次形成高价值结论的时间;结论可以是 unknown",
),
)
else:
@@ -150,13 +185,16 @@ def upgrade() -> None:
comment="owned / available / unknown;只认官方 storehome.status",
),
)
# 回填只处理新字段全空的账号,所以“加完列但回填前中断”和
# “回填成功但后续 DDL 失败”两种 MySQL 非事务迁移都可以直接重跑。
_backfill_from_snapshot()
op.create_index(
"ix_accounts_high_value_checked_at",
"accounts",
["high_value_checked_at"],
)
if _column_exists("high_value_owned"):
if not _index_exists("ix_accounts_high_value_checked_at"):
op.create_index(
"ix_accounts_high_value_checked_at",
"accounts",
["high_value_checked_at"],
)
if old_column_exists:
op.drop_column("accounts", "high_value_owned")
@@ -170,24 +208,36 @@ def downgrade() -> None:
sa.text(
"""
UPDATE accounts
SET high_value_owned = COALESCE(
(
SELECT JSON_ARRAYAGG(t.name)
FROM (
SELECT CASE WHEN high_value_bairimeng_status = 'owned'
THEN '套装-兔兔白日梦' END AS name
UNION ALL
SELECT CASE WHEN high_value_mengtu_status = 'owned'
THEN '套装-狂战萌兔' END AS name
) AS t
WHERE t.name IS NOT NULL
),
JSON_ARRAY()
)
SET high_value_owned = CASE
WHEN high_value_bairimeng_status IS NULL
OR high_value_bairimeng_status NOT IN ('owned', 'available')
OR high_value_mengtu_status IS NULL
OR high_value_mengtu_status NOT IN ('owned', 'available')
THEN NULL
ELSE COALESCE(
(
SELECT JSON_ARRAYAGG(t.name)
FROM (
SELECT CASE WHEN high_value_bairimeng_status = 'owned'
THEN '套装-兔兔白日梦' END AS name
UNION ALL
SELECT CASE WHEN high_value_mengtu_status = 'owned'
THEN '套装-狂战萌兔' END AS name
) AS t
WHERE t.name IS NOT NULL
),
JSON_ARRAY()
)
END
"""
)
)
op.drop_index("ix_accounts_high_value_checked_at", table_name="accounts")
op.drop_column("accounts", "high_value_checked_at")
op.drop_column("accounts", "high_value_mengtu_status")
op.drop_column("accounts", "high_value_bairimeng_status")
if _index_exists("ix_accounts_high_value_checked_at"):
op.drop_index("ix_accounts_high_value_checked_at", table_name="accounts")
for name in (
"high_value_checked_at",
"high_value_mengtu_status",
"high_value_bairimeng_status",
):
if _column_exists(name):
op.drop_column("accounts", name)