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
@@ -437,6 +437,10 @@
"20261121_0109_account_pool_tier_index.py": {
"revision": "20261121_0109",
"sha256": "0aa85febbc0f2c99f5768bba2364f8797a748c4de740780e33a2017311f591b0"
},
"20261121_0110_account_platform_foundation.py": {
"revision": "20261121_0110",
"sha256": "94c0158a061a7670d1c15cfd57172d98eddd271a4d82be0cb7d0ea65013f8712"
}
}
}
@@ -0,0 +1,650 @@
"""斗鱼账号平台基础:CK 周期、同步状态、工作队列、供给目标和登录配额。"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision: str = "20261121_0110"
down_revision: str | None = "20261121_0109"
branch_labels: str | None = None
depends_on: str | None = None
def _table_exists(table: str) -> bool:
return bool(
op.get_bind()
.execute(
sa.text(
"""
SELECT COUNT(*)
FROM information_schema.tables
WHERE table_schema = DATABASE()
AND table_name = :table
"""
),
{"table": table},
)
.scalar()
)
def _column_exists(table: str, column: str) -> bool:
return bool(
op.get_bind()
.execute(
sa.text(
"""
SELECT COUNT(*)
FROM information_schema.columns
WHERE table_schema = DATABASE()
AND table_name = :table
AND column_name = :column
"""
),
{"table": table, "column": column},
)
.scalar()
)
def upgrade() -> None:
for column in (
sa.Column("ck_expected_expires_at", sa.DateTime(), nullable=True),
sa.Column("ck_last_valid_at", sa.DateTime(), nullable=True),
sa.Column("ck_invalid_detected_at", sa.DateTime(), nullable=True),
sa.Column("ck_observed_lifetime_seconds", sa.Integer(), nullable=True),
sa.Column(
"ck_expiry_source",
sa.String(length=24),
nullable=False,
server_default="",
),
):
if not _column_exists("accounts", column.name):
op.add_column("accounts", column)
# 商品级原始字段:已有商品能力表,必须只做加法,不能靠 create_table 兜底。
if not _column_exists("account_commodity_states", "raw_json"):
op.add_column(
"account_commodity_states",
sa.Column("raw_json", sa.JSON(), nullable=True),
)
if not _table_exists("account_sync_states"):
op.create_table(
"account_sync_states",
sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
sa.Column(
"account_id",
sa.Integer(),
sa.ForeignKey("accounts.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"platform",
sa.String(length=16),
nullable=False,
server_default="douyu",
),
sa.Column("domain", sa.String(length=32), nullable=False),
sa.Column("last_attempt_at", sa.DateTime(), nullable=True),
sa.Column("last_success_at", sa.DateTime(), nullable=True),
sa.Column("valid_until", sa.DateTime(), nullable=True),
sa.Column("next_retry_at", sa.DateTime(), nullable=True),
sa.Column(
"status",
sa.String(length=16),
nullable=False,
server_default="never",
),
sa.Column(
"error_code",
sa.String(length=64),
nullable=False,
server_default="",
),
sa.Column("error_message", sa.Text(), nullable=False),
sa.Column(
"source",
sa.String(length=32),
nullable=False,
server_default="",
),
sa.Column(
"payload_hash",
sa.String(length=64),
nullable=False,
server_default="",
),
sa.Column("raw_payload_json", sa.JSON(), nullable=True),
sa.Column(
"state_version",
sa.BigInteger(),
nullable=False,
server_default="0",
),
sa.Column(
"created_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
sa.Column(
"updated_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
sa.UniqueConstraint(
"account_id",
"platform",
"domain",
name="uq_account_sync_state_domain",
),
)
op.create_index(
"ix_account_sync_state_due",
"account_sync_states",
["platform", "domain", "status", "next_retry_at", "account_id"],
)
op.create_index(
"ix_account_sync_state_success",
"account_sync_states",
["platform", "domain", "last_success_at", "account_id"],
)
elif not _column_exists("account_sync_states", "raw_payload_json"):
op.add_column(
"account_sync_states",
sa.Column("raw_payload_json", sa.JSON(), nullable=True),
)
if not _table_exists("account_sync_runs"):
op.create_table(
"account_sync_runs",
sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
sa.Column(
"account_id",
sa.Integer(),
sa.ForeignKey("accounts.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"platform",
sa.String(length=16),
nullable=False,
server_default="douyu",
),
sa.Column("domain", sa.String(length=32), nullable=False),
sa.Column("task_id", sa.BigInteger(), nullable=True),
sa.Column("started_at", sa.DateTime(), nullable=False),
sa.Column("finished_at", sa.DateTime(), nullable=True),
sa.Column(
"status",
sa.String(length=16),
nullable=False,
server_default="running",
),
sa.Column(
"source",
sa.String(length=32),
nullable=False,
server_default="",
),
sa.Column(
"item_count",
sa.Integer(),
nullable=False,
server_default="0",
),
sa.Column(
"changed_count",
sa.Integer(),
nullable=False,
server_default="0",
),
sa.Column(
"duration_ms",
sa.Integer(),
nullable=False,
server_default="0",
),
sa.Column(
"error_code",
sa.String(length=64),
nullable=False,
server_default="",
),
sa.Column("error_message", sa.Text(), nullable=False),
sa.Column("official_observed_at", sa.DateTime(), nullable=True),
sa.Column(
"created_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
)
op.create_index(
"ix_account_sync_runs_account_domain_time",
"account_sync_runs",
["account_id", "domain", "started_at"],
)
op.create_index(
"ix_account_sync_runs_domain_status_time",
"account_sync_runs",
["domain", "status", "started_at"],
)
if not _table_exists("account_work_items"):
op.create_table(
"account_work_items",
sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
sa.Column(
"account_id",
sa.Integer(),
sa.ForeignKey("accounts.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column("work_type", sa.String(length=32), nullable=False),
sa.Column(
"priority",
sa.Integer(),
nullable=False,
server_default="100",
),
sa.Column(
"status",
sa.String(length=16),
nullable=False,
server_default="pending",
),
sa.Column("due_at", sa.DateTime(), nullable=False),
sa.Column("expires_at", sa.DateTime(), nullable=True),
sa.Column(
"attempt_count",
sa.Integer(),
nullable=False,
server_default="0",
),
sa.Column(
"max_attempts",
sa.Integer(),
nullable=False,
server_default="3",
),
sa.Column("dedupe_key", sa.String(length=191), nullable=False),
sa.Column(
"active_dedupe_key",
sa.String(length=191),
sa.Computed(
"CASE WHEN status IN ('pending', 'running') "
"THEN dedupe_key ELSE NULL END",
persisted=False,
),
nullable=True,
),
sa.Column(
"reason",
sa.String(length=255),
nullable=False,
server_default="",
),
sa.Column("payload_json", sa.JSON(), nullable=True),
sa.Column(
"created_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
sa.Column("started_at", sa.DateTime(), nullable=True),
sa.Column("finished_at", sa.DateTime(), nullable=True),
sa.Column("last_error", sa.Text(), nullable=False),
sa.Column(
"updated_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
)
op.create_index(
"uq_account_work_active_dedupe",
"account_work_items",
["active_dedupe_key"],
unique=True,
)
op.create_index(
"ix_account_work_due",
"account_work_items",
["status", "work_type", "due_at", "priority", "id"],
)
op.create_index(
"ix_account_work_account_status",
"account_work_items",
["account_id", "status", "work_type"],
)
if not _table_exists("account_supply_targets"):
op.create_table(
"account_supply_targets",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
sa.Column(
"platform",
sa.String(length=16),
nullable=False,
server_default="douyu",
),
sa.Column(
"scope",
sa.String(length=16),
nullable=False,
server_default="elite",
),
sa.Column(
"commodity_id",
sa.String(length=64),
nullable=False,
server_default="",
),
sa.Column("value_class", sa.String(length=16), nullable=False),
sa.Column(
"target_covered",
sa.Integer(),
nullable=False,
server_default="0",
),
sa.Column(
"target_available",
sa.Integer(),
nullable=False,
server_default="0",
),
sa.Column(
"target_allocatable",
sa.Integer(),
nullable=False,
server_default="0",
),
sa.Column(
"safety_buffer",
sa.Integer(),
nullable=False,
server_default="0",
),
sa.Column(
"enabled",
sa.Boolean(),
nullable=False,
server_default="1",
),
sa.Column(
"updated_by",
sa.Integer(),
sa.ForeignKey("users.id", ondelete="SET NULL"),
nullable=True,
),
sa.Column(
"created_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
sa.Column(
"updated_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
sa.UniqueConstraint(
"platform",
"scope",
"commodity_id",
"value_class",
name="uq_account_supply_target",
),
)
op.create_index(
"ix_account_supply_target_enabled",
"account_supply_targets",
["platform", "enabled", "scope"],
)
if not _table_exists("login_attempts"):
op.create_table(
"login_attempts",
sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
sa.Column(
"account_id",
sa.Integer(),
sa.ForeignKey("accounts.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column(
"batch_id",
sa.String(length=64),
nullable=False,
server_default="",
),
sa.Column("category", sa.String(length=32), nullable=False),
sa.Column(
"trigger_source",
sa.String(length=16),
nullable=False,
server_default="manual",
),
sa.Column(
"created_by",
sa.Integer(),
sa.ForeignKey("users.id", ondelete="SET NULL"),
nullable=True,
),
sa.Column(
"started_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
sa.Column("finished_at", sa.DateTime(), nullable=True),
sa.Column(
"status",
sa.String(length=16),
nullable=False,
server_default="pending",
),
sa.Column(
"failure_kind",
sa.String(length=16),
nullable=False,
server_default="",
),
sa.Column(
"duration_ms",
sa.Integer(),
nullable=False,
server_default="0",
),
sa.Column(
"proxy_key",
sa.String(length=64),
nullable=False,
server_default="",
),
sa.Column(
"email_code_requested",
sa.Boolean(),
nullable=False,
server_default="0",
),
sa.Column("quota_day", sa.Date(), nullable=False),
sa.Column(
"created_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
)
op.create_index(
"ix_login_attempts_account_day",
"login_attempts",
["account_id", "quota_day", "category"],
)
op.create_index(
"ix_login_attempts_day_category_status",
"login_attempts",
["quota_day", "category", "status"],
)
op.create_index(
"ix_login_attempts_batch",
"login_attempts",
["batch_id", "account_id"],
)
if not _table_exists("login_quota_settings"):
op.create_table(
"login_quota_settings",
sa.Column("id", sa.Integer(), primary_key=True),
sa.Column(
"daily_attempt_limit",
sa.Integer(),
nullable=False,
server_default="1500",
),
sa.Column(
"hourly_attempt_limit",
sa.Integer(),
nullable=False,
server_default="100",
),
sa.Column(
"per_account_daily_limit",
sa.Integer(),
nullable=False,
server_default="2",
),
sa.Column(
"onboarding_daily_limit",
sa.Integer(),
nullable=False,
server_default="150",
),
sa.Column(
"relogin_high_value_daily_limit",
sa.Integer(),
nullable=False,
server_default="300",
),
sa.Column(
"relogin_ordinary_daily_limit",
sa.Integer(),
nullable=False,
server_default="500",
),
sa.Column(
"manual_daily_limit",
sa.Integer(),
nullable=False,
server_default="300",
),
sa.Column(
"login_concurrency",
sa.Integer(),
nullable=False,
server_default="3",
),
sa.Column(
"failure_pause_ratio",
sa.Integer(),
nullable=False,
server_default="40",
),
sa.Column(
"enabled",
sa.Boolean(),
nullable=False,
server_default="1",
),
sa.Column(
"updated_by",
sa.Integer(),
sa.ForeignKey("users.id", ondelete="SET NULL"),
nullable=True,
),
sa.Column(
"updated_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
)
op.execute(
sa.text("INSERT IGNORE INTO login_quota_settings (id) VALUES (1)")
)
if not _table_exists("login_quota_usage"):
op.create_table(
"login_quota_usage",
sa.Column("id", sa.BigInteger(), primary_key=True, autoincrement=True),
sa.Column("quota_day", sa.Date(), nullable=False),
sa.Column("quota_hour", sa.Integer(), nullable=False),
sa.Column("category", sa.String(length=32), nullable=False),
sa.Column(
"attempt_count",
sa.Integer(),
nullable=False,
server_default="0",
),
sa.Column(
"success_count",
sa.Integer(),
nullable=False,
server_default="0",
),
sa.Column(
"failure_count",
sa.Integer(),
nullable=False,
server_default="0",
),
sa.Column(
"updated_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
sa.UniqueConstraint(
"quota_day",
"quota_hour",
"category",
name="uq_login_quota_usage_bucket",
),
)
op.create_index(
"ix_login_quota_usage_day_category",
"login_quota_usage",
["quota_day", "category"],
)
def downgrade() -> None:
for table in (
"login_quota_usage",
"login_quota_settings",
"login_attempts",
"account_supply_targets",
"account_work_items",
"account_sync_runs",
"account_sync_states",
):
if _table_exists(table):
op.drop_table(table)
for column in (
"ck_expiry_source",
"ck_observed_lifetime_seconds",
"ck_invalid_detected_at",
"ck_last_valid_at",
"ck_expected_expires_at",
):
if _column_exists("accounts", column):
op.drop_column("accounts", column)
if _column_exists("account_commodity_states", "raw_json"):
op.drop_column("account_commodity_states", "raw_json")