183 lines
6.7 KiB
Python
183 lines
6.7 KiB
Python
"""新增斗鱼活动任务表
|
|
|
|
Revision ID: 20260724_0008
|
|
Revises: 20260712_0007
|
|
Create Date: 2026-07-24
|
|
"""
|
|
|
|
from collections.abc import Sequence
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
revision: str = "20260724_0008"
|
|
down_revision: str | None = "20260712_0007"
|
|
branch_labels: str | Sequence[str] | None = None
|
|
depends_on: str | Sequence[str] | None = None
|
|
|
|
|
|
def _has_table(bind, table_name: str) -> bool:
|
|
return sa.inspect(bind).has_table(table_name)
|
|
|
|
|
|
def _columns(bind, table_name: str) -> set[str]:
|
|
if not _has_table(bind, table_name):
|
|
return set()
|
|
return {column["name"] for column in sa.inspect(bind).get_columns(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 _add_column_if_missing(bind, table_name: str, column: sa.Column) -> None:
|
|
if column.name not in _columns(bind, table_name):
|
|
op.add_column(table_name, column)
|
|
|
|
|
|
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()
|
|
|
|
_add_column_if_missing(
|
|
bind, "accounts", sa.Column("uid", sa.String(length=32), nullable=True)
|
|
)
|
|
_add_column_if_missing(
|
|
bind, "accounts", sa.Column("nickname", sa.String(length=128), nullable=True)
|
|
)
|
|
_add_column_if_missing(
|
|
bind, "accounts", sa.Column("points", sa.Integer(), nullable=True)
|
|
)
|
|
_add_column_if_missing(
|
|
bind, "accounts", sa.Column("game_name", sa.String(length=128), nullable=True)
|
|
)
|
|
_add_column_if_missing(
|
|
bind,
|
|
"accounts",
|
|
sa.Column("game_channel", sa.String(length=128), nullable=True),
|
|
)
|
|
_add_column_if_missing(
|
|
bind, "accounts", sa.Column("gold_balance", sa.Integer(), nullable=True)
|
|
)
|
|
_add_column_if_missing(
|
|
bind, "accounts", sa.Column("exchange_balance", sa.Integer(), nullable=True)
|
|
)
|
|
_add_column_if_missing(
|
|
bind, "accounts", sa.Column("bind_status", sa.String(length=32), nullable=True)
|
|
)
|
|
_add_column_if_missing(
|
|
bind,
|
|
"accounts",
|
|
sa.Column("change_role_wait_time", sa.Integer(), nullable=True),
|
|
)
|
|
_add_column_if_missing(
|
|
bind, "accounts", sa.Column("updated_at", sa.DateTime(), nullable=True)
|
|
)
|
|
_create_index_if_missing(bind, "ix_accounts_uid", "accounts", ["uid"])
|
|
|
|
if not _has_table(bind, "douyu_tasks"):
|
|
op.create_table(
|
|
"douyu_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"], ["accounts.id"]),
|
|
sa.ForeignKeyConstraint(["created_by"], ["users.id"]),
|
|
sa.PrimaryKeyConstraint("id"),
|
|
)
|
|
_create_index_if_missing(
|
|
bind, "ix_douyu_tasks_batch_id", "douyu_tasks", ["batch_id"]
|
|
)
|
|
_create_index_if_missing(
|
|
bind, "ix_douyu_tasks_task_type", "douyu_tasks", ["task_type"]
|
|
)
|
|
|
|
if not _has_table(bind, "douyu_config"):
|
|
op.create_table(
|
|
"douyu_config",
|
|
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
|
sa.Column("manual_id", sa.String(length=64), nullable=True),
|
|
sa.Column("rid", sa.String(length=64), nullable=True),
|
|
sa.Column("bind_act_alias", sa.String(length=64), nullable=True),
|
|
sa.Column("confirm_act_alias", sa.String(length=64), nullable=True),
|
|
sa.Column("legacy_act_alias", sa.String(length=64), nullable=True),
|
|
sa.Column("room_id", sa.String(length=64), nullable=True),
|
|
sa.Column("elite_amount", sa.Integer(), nullable=True),
|
|
sa.Column("gold_pay_type", sa.Integer(), nullable=True),
|
|
sa.Column("gift_id", sa.String(length=64), nullable=True),
|
|
sa.Column("skin_id", sa.String(length=64), nullable=True),
|
|
sa.Column("updated_at", sa.DateTime(), nullable=True),
|
|
sa.PrimaryKeyConstraint("id"),
|
|
)
|
|
|
|
if not _has_table(bind, "douyu_goods_snapshot"):
|
|
op.create_table(
|
|
"douyu_goods_snapshot",
|
|
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
|
sa.Column("commodity_id", sa.String(length=64), nullable=False),
|
|
sa.Column("name", sa.String(length=256), nullable=True),
|
|
sa.Column("score", sa.Integer(), nullable=True),
|
|
sa.Column("status", sa.String(length=32), 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_douyu_goods_snapshot_commodity_id",
|
|
"douyu_goods_snapshot",
|
|
["commodity_id"],
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
bind = op.get_bind()
|
|
if _has_table(bind, "douyu_goods_snapshot"):
|
|
indexes = _indexes(bind, "douyu_goods_snapshot")
|
|
if "ix_douyu_goods_snapshot_commodity_id" in indexes:
|
|
op.drop_index(
|
|
"ix_douyu_goods_snapshot_commodity_id",
|
|
table_name="douyu_goods_snapshot",
|
|
)
|
|
op.drop_table("douyu_goods_snapshot")
|
|
if _has_table(bind, "douyu_config"):
|
|
op.drop_table("douyu_config")
|
|
if _has_table(bind, "douyu_tasks"):
|
|
indexes = _indexes(bind, "douyu_tasks")
|
|
if "ix_douyu_tasks_task_type" in indexes:
|
|
op.drop_index("ix_douyu_tasks_task_type", table_name="douyu_tasks")
|
|
if "ix_douyu_tasks_batch_id" in indexes:
|
|
op.drop_index("ix_douyu_tasks_batch_id", table_name="douyu_tasks")
|
|
op.drop_table("douyu_tasks")
|
|
|
|
account_columns = _columns(bind, "accounts")
|
|
for column in [
|
|
"updated_at",
|
|
"change_role_wait_time",
|
|
"bind_status",
|
|
"exchange_balance",
|
|
"gold_balance",
|
|
"game_channel",
|
|
"game_name",
|
|
"points",
|
|
"nickname",
|
|
"uid",
|
|
]:
|
|
if column in account_columns:
|
|
op.drop_column("accounts", column)
|