P1a 的巡检只产出「谁失效了」的清单,不兑现修复:1,356 个失效账号仍要人工逐个
选集→点重登→过滑块。线上实测 20 个失效账号重登 20/20 成功(2 分 13 秒、零极验),
证实失效是会话级过期而非凭证问题,可救量约 1,350。
后端:
- 迁移 20261002_0038:login_tasks 加 relogin_failure_kind(枚举),供作业决定
「继续退避重试」还是「转人工」;不用 attempt_message 做字符串匹配(文案一改
就失效,页面也要按类型筛选);
- jobs/ck_relogin.py:
- 候选 = 巡检判定 invalid + 允许自动管理 + 无在途尝试 + 退避到期 + 未超每日上限
+ **未转人工**(极验/凭证问题不再自动重登);
- 复用 LoginBatchRunner(mode="relogin"),成功才替换 CK(沿用既有安全语义)。
自动化身份取最早的超管做 created_by,权限给 login:view_all 以免受归属限制;
总时长上限压到 240 秒,避免长时间占住调度线程;
- settle_results() 独立成纯函数便于单测:成功清空退避状态;失败按分型分流 ——
captcha/credential 转人工(不排期)、email_quota 退避到次日零点+随机抖动、
email_code/interrupted/network 走 1h→3h→12h→24h 阶梯;次数用尽也停下等人工;
- 未知文案默认按可重试处理(有每日上限兜底),别把可能只是抖动的账号推给人工;
- 注册第二个作业「CK 自动重登」(batch 10 / 并发 3,**默认关闭**);
- 新增 GET /api/automation/manual-queue:按原因分组列出待人工处理的账号。可重试
类型但已无下次排期的(次数用尽)单独成组 —— 否则会混进「验证码失败」里,客服
看不出这个已经不会再自动试了。
前端:
- /automation 页新增「待人工处理」卡片,按分型分组展示(极验要过滑块、凭证要核对、
次数用尽要人工确认),各带账号与最近失败原因。
测试:新增 26 条(分型顺序陷阱「上限」优先于「验证码」、退避阶梯、配额退避次日、
候选筛选、MySQL 方言编译、结算、待人工队列分组)。
提交前全量 297 通过、ruff 全绿、前端 tsc 通过。
注意:提交时另一路「测试改用 MySQL」的改造正在同一工作区进行,本次刻意只暂存
本功能相关文件,未触碰 tests/conftest.py、tests/support/ 及对方已转换的测试文件。
118 lines
5.1 KiB
Python
118 lines
5.1 KiB
Python
"""Alembic 迁移链冒烟测试(不依赖真实 MySQL)。
|
|
|
|
覆盖三件事:
|
|
1. 链完整性:只有一个头版本、头与最新迁移一致、每个修订的
|
|
down_revision 都能回溯到根(无悬挂引用、无分叉链)。
|
|
2. 全部迁移脚本可编译:防止迁移文件被改坏后部署流程才暴露。
|
|
3. 空库整链执行:迁移脚本自带方言防护(MySQL 专属 DDL 有 _is_mysql()
|
|
之类分支),整条链可在 SQLite 上真实执行 `upgrade head`,随后校验
|
|
表/列/索引与 Base.metadata 模型元数据对齐(模型要求的不允许缺失),
|
|
并确认迁移新增的复合查询索引确实落库。
|
|
"""
|
|
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
from alembic import command
|
|
from alembic.config import Config
|
|
from alembic.script import ScriptDirectory
|
|
from sqlalchemy import create_engine, inspect
|
|
|
|
TESTS_DIR = Path(__file__).resolve().parent
|
|
PROJECT_ROOT = TESTS_DIR.parent
|
|
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
|
|
|
HEAD_REVISION = "20261002_0038"
|
|
|
|
# 迁移新增、但不声明在模型里的复合查询索引。
|
|
EXTRA_INDEXES = (
|
|
"ix_douyu_tasks_handbook_scope_id",
|
|
"ix_douyu_tasks_handbook_scope_task_type_id",
|
|
"ix_douyu_tasks_status_finished_at_id",
|
|
"ix_login_tasks_status_finished_at_id",
|
|
)
|
|
|
|
|
|
def _migration_config() -> Config:
|
|
config = Config(str(PROJECT_ROOT / "alembic.ini"))
|
|
config.set_main_option(
|
|
"script_location", str(PROJECT_ROOT / "web" / "backend" / "migrations")
|
|
)
|
|
return config
|
|
|
|
|
|
class TestMigrationSmoke:
|
|
def test_chain_is_linear_and_head_matches_latest_file(self):
|
|
directory = ScriptDirectory.from_config(_migration_config())
|
|
|
|
heads = directory.get_heads()
|
|
assert len(heads) == 1, f"迁移链出现多个头版本(存在分叉): {heads}"
|
|
assert heads[0] == HEAD_REVISION, f"头版本 {heads[0]} 与最新迁移不一致"
|
|
|
|
version_files = sorted(path.name for path in VERSIONS_DIR.glob("*.py"))
|
|
revisions = list(directory.walk_revisions())
|
|
revision_ids = {revision.revision for revision in revisions}
|
|
assert len(revision_ids) == len(version_files), (
|
|
"迁移文件数量与已加载修订数量不一致(存在无法加载的脚本)"
|
|
)
|
|
for revision in revisions:
|
|
if revision.down_revision is not None:
|
|
assert revision.down_revision in revision_ids, (
|
|
f"{revision.revision} 引用了不存在的父版本 {revision.down_revision}"
|
|
)
|
|
|
|
def test_all_migration_scripts_compile(self):
|
|
for path in sorted(VERSIONS_DIR.glob("*.py")):
|
|
source = path.read_text(encoding="utf-8")
|
|
compile(source, str(path), "exec") # 语法/缩进等编译错误会在此抛出
|
|
|
|
def test_full_chain_applies_on_fresh_sqlite_and_matches_models(self):
|
|
"""空库执行整条迁移链,校验表/列/索引与模型元数据对齐。"""
|
|
import web.backend.database as database_module
|
|
from web.backend.database import Base
|
|
|
|
original_url = database_module.DATABASE_URL
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
db_path = Path(tmp) / "migrate.db"
|
|
try:
|
|
# env.py 会用模块级 DATABASE_URL 覆盖配置里的 sqlalchemy.url。
|
|
database_module.DATABASE_URL = f"sqlite:///{db_path}"
|
|
command.upgrade(_migration_config(), "head")
|
|
|
|
inspector = inspect(create_engine(f"sqlite:///{db_path}"))
|
|
db_tables = set(inspector.get_table_names())
|
|
assert db_tables
|
|
|
|
model_tables = Base.metadata.tables
|
|
missing_tables = set(model_tables) - db_tables
|
|
assert not missing_tables
|
|
|
|
all_db_indexes = set()
|
|
for name, table in model_tables.items():
|
|
db_columns = {
|
|
column["name"] for column in inspector.get_columns(name)
|
|
}
|
|
model_columns = set(table.columns.keys())
|
|
missing_columns = model_columns - db_columns
|
|
assert not missing_columns
|
|
|
|
db_indexes = set()
|
|
for index in inspector.get_indexes(name):
|
|
index_name = index["name"]
|
|
if index_name and not index_name.startswith("sqlite_autoindex"):
|
|
db_indexes.add(index_name)
|
|
all_db_indexes |= db_indexes
|
|
model_indexes = {
|
|
index.name for index in table.indexes if index.name
|
|
}
|
|
missing_indexes = model_indexes - db_indexes
|
|
assert not missing_indexes
|
|
|
|
# 迁移新增的复合查询索引(不入模型,但应存在)。
|
|
for index_name in EXTRA_INDEXES:
|
|
assert index_name in all_db_indexes, (
|
|
f"迁移链未创建索引 {index_name}"
|
|
)
|
|
finally:
|
|
database_module.DATABASE_URL = original_url
|