Files
live-hub-py/web/backend/migrations/versions/20260704_0004_huya_foundation.py
T

128 lines
5.9 KiB
Python

"""新增虎牙基础数据表
Revision ID: 20260704_0004
Revises: 20260624_0003
Create Date: 2026-07-04
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = "20260704_0004"
down_revision: Union[str, None] = "20260624_0003"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def _has_table(bind, table_name: str) -> bool:
return sa.inspect(bind).has_table(table_name)
def _indexes(bind, table_name: str) -> set[str]:
if not _has_table(bind, table_name):
return set()
return {index["name"] for index in sa.inspect(bind).get_indexes(table_name)}
def _create_index_if_missing(bind, name: str, table_name: str, columns: list[str], unique: bool = False) -> None:
if name not in _indexes(bind, table_name):
op.create_index(name, table_name, columns, unique=unique)
def upgrade() -> None:
bind = op.get_bind()
if not _has_table(bind, "huya_accounts"):
op.create_table(
"huya_accounts",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("uid", sa.String(length=32), nullable=True),
sa.Column("yyuid", sa.String(length=32), nullable=True),
sa.Column("username", sa.String(length=128), nullable=True),
sa.Column("nickname", sa.String(length=128), nullable=True),
sa.Column("cookie", sa.Text(), nullable=False),
sa.Column("tag", sa.String(length=64), nullable=True),
sa.Column("remark", sa.String(length=256), nullable=True),
sa.Column("status", sa.String(length=32), nullable=True),
sa.Column("points", sa.Integer(), nullable=True),
sa.Column("game_name", sa.String(length=128), nullable=True),
sa.Column("game_channel", sa.String(length=64), nullable=True),
sa.Column("game_phone", sa.String(length=64), nullable=True),
sa.Column("assigned_to", sa.Integer(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("updated_at", sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(["assigned_to"], ["users.id"]),
sa.PrimaryKeyConstraint("id"),
)
_create_index_if_missing(bind, "ix_huya_accounts_uid", "huya_accounts", ["uid"])
_create_index_if_missing(bind, "ix_huya_accounts_yyuid", "huya_accounts", ["yyuid"])
_create_index_if_missing(bind, "ix_huya_accounts_assigned_to", "huya_accounts", ["assigned_to"])
if not _has_table(bind, "huya_tasks"):
op.create_table(
"huya_tasks",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("batch_id", sa.String(length=64), nullable=False),
sa.Column("account_id", sa.Integer(), nullable=False),
sa.Column("task_type", sa.String(length=64), nullable=False),
sa.Column("status", sa.String(length=32), nullable=True),
sa.Column("message", sa.String(length=512), nullable=True),
sa.Column("result", sa.JSON(), nullable=True),
sa.Column("created_by", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("finished_at", sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(["account_id"], ["huya_accounts.id"]),
sa.ForeignKeyConstraint(["created_by"], ["users.id"]),
sa.PrimaryKeyConstraint("id"),
)
_create_index_if_missing(bind, "ix_huya_tasks_batch_id", "huya_tasks", ["batch_id"])
_create_index_if_missing(bind, "ix_huya_tasks_task_type", "huya_tasks", ["task_type"])
if not _has_table(bind, "huya_config"):
op.create_table(
"huya_config",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("room_pid", sa.String(length=64), nullable=True),
sa.Column("sid", sa.String(length=32), nullable=True),
sa.Column("outer_act_id", sa.String(length=32), nullable=True),
sa.Column("bind_act_id", sa.String(length=32), nullable=True),
sa.Column("pay_channel", sa.String(length=16), nullable=True),
sa.Column("updated_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
if not _has_table(bind, "huya_goods_snapshot"):
op.create_table(
"huya_goods_snapshot",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("product_id", sa.String(length=64), nullable=False),
sa.Column("name", sa.String(length=256), nullable=True),
sa.Column("price", sa.Integer(), nullable=True),
sa.Column("remain_text", sa.String(length=64), nullable=True),
sa.Column("raw", sa.JSON(), nullable=True),
sa.Column("updated_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
_create_index_if_missing(bind, "ix_huya_goods_snapshot_product_id", "huya_goods_snapshot", ["product_id"])
def downgrade() -> None:
bind = op.get_bind()
if _has_table(bind, "huya_goods_snapshot"):
op.drop_index("ix_huya_goods_snapshot_product_id", table_name="huya_goods_snapshot")
op.drop_table("huya_goods_snapshot")
if _has_table(bind, "huya_config"):
op.drop_table("huya_config")
if _has_table(bind, "huya_tasks"):
op.drop_index("ix_huya_tasks_task_type", table_name="huya_tasks")
op.drop_index("ix_huya_tasks_batch_id", table_name="huya_tasks")
op.drop_table("huya_tasks")
if _has_table(bind, "huya_accounts"):
op.drop_index("ix_huya_accounts_assigned_to", table_name="huya_accounts")
op.drop_index("ix_huya_accounts_yyuid", table_name="huya_accounts")
op.drop_index("ix_huya_accounts_uid", table_name="huya_accounts")
op.drop_table("huya_accounts")