feat(douyu): 落地账号平台同步与登录配额

This commit is contained in:
yml
2026-11-21 21:27:14 +08:00
parent 72bc346c9b
commit 1d262cdfd9
30 changed files with 3087 additions and 85 deletions
+58 -1
View File
@@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = TESTS_DIR.parent
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
HEAD_REVISION = "20261121_0109"
HEAD_REVISION = "20261121_0110"
# 迁移新增、但不声明在模型里的复合查询索引。
EXTRA_INDEXES = (
@@ -58,6 +58,54 @@ EXTRA_INDEXES = (
)
_ACCOUNT_PLATFORM_COLUMNS = {
"ck_expected_expires_at": "DATETIME NULL",
"ck_last_valid_at": "DATETIME NULL",
"ck_invalid_detected_at": "DATETIME NULL",
"ck_observed_lifetime_seconds": "INTEGER NULL",
"ck_expiry_source": "VARCHAR(24) NOT NULL DEFAULT ''",
}
_LATER_ORM_COLUMNS = {
"platform_fulfillment_tasks": {
"sale_mode": "VARCHAR(16) NOT NULL DEFAULT 'unknown'",
"campaign_key": "VARCHAR(128) NOT NULL DEFAULT ''",
"campaign_name": "VARCHAR(255) NOT NULL DEFAULT ''",
"offer_available_from": "DATETIME NULL",
"offer_available_until": "DATETIME NULL",
"classification_source": "VARCHAR(24) NOT NULL DEFAULT 'unknown'",
"exchange_result_image_key": "VARCHAR(512) NOT NULL DEFAULT ''",
"exchange_result_image_saved_at": "DATETIME NULL",
},
"douyu_external_orders": {
"succeeded_at": "DATETIME NULL",
},
}
def _add_account_platform_columns(engine) -> None:
"""让“旧 revision + 当前 ORM”测试夹具兼容已新增的账号列。"""
inspector = inspect(engine)
existing = {item["name"] for item in inspector.get_columns("accounts")}
with engine.begin() as conn:
for name, ddl in _ACCOUNT_PLATFORM_COLUMNS.items():
if name not in existing:
conn.execute(text(f"ALTER TABLE accounts ADD COLUMN `{name}` {ddl}"))
def _add_later_orm_columns(engine, tables: tuple[str, ...]) -> None:
"""旧 revision 上用当前 ORM 写夹具时,补齐后续纯列迁移。"""
inspector = inspect(engine)
with engine.begin() as conn:
for table in tables:
existing = {item["name"] for item in inspector.get_columns(table)}
for name, ddl in _LATER_ORM_COLUMNS[table].items():
if name not in existing:
conn.execute(
text(f"ALTER TABLE `{table}` ADD COLUMN `{name}` {ddl}")
)
def _migration_config() -> Config:
config = Config(str(PROJECT_ROOT / "alembic.ini"))
config.set_main_option(
@@ -458,6 +506,7 @@ class TestMigrationSmoke:
cfg = _migration_config()
cfg.set_main_option("sqlalchemy.url", mig_url)
command.upgrade(cfg, "20261121_0106")
_add_account_platform_columns(mig_engine)
with Session(mig_engine) as session:
user = User(
username="repair-0107-user",
@@ -919,6 +968,7 @@ class TestMigrationSmoke:
cfg = _migration_config()
cfg.set_main_option("sqlalchemy.url", mig_url)
command.upgrade(cfg, "20261118_0089")
_add_account_platform_columns(mig_engine)
with Session(mig_engine) as session:
user = User(
@@ -1059,6 +1109,7 @@ class TestMigrationSmoke:
cfg = _migration_config()
cfg.set_main_option("sqlalchemy.url", mig_url)
command.upgrade(cfg, "20261118_0091")
_add_account_platform_columns(mig_engine)
with Session(mig_engine) as session:
account = Account(
@@ -1121,6 +1172,10 @@ class TestMigrationSmoke:
cfg = _migration_config()
cfg.set_main_option("sqlalchemy.url", mig_url)
command.upgrade(cfg, "20261118_0094")
_add_later_orm_columns(
mig_engine,
("platform_fulfillment_tasks", "douyu_external_orders"),
)
with Session(mig_engine) as session:
repaired_task = PlatformFulfillmentTask(
@@ -1194,6 +1249,8 @@ class TestMigrationSmoke:
cfg = _migration_config()
cfg.set_main_option("sqlalchemy.url", mig_url)
command.upgrade(cfg, "20261119_0097")
_add_account_platform_columns(mig_engine)
_add_later_orm_columns(mig_engine, ("douyu_external_orders",))
with Session(mig_engine) as session:
owner = User(