虎牙精英宝典
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
"""增加虎牙精英宝典工作台范围与账号集合。"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20260831_0032"
|
||||
down_revision: str | None = "20260830_0031"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def _columns(bind, table: str) -> set[str]:
|
||||
return {item["name"] for item in sa.inspect(bind).get_columns(table)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
if "handbook_scope" not in _columns(bind, "huya_tasks"):
|
||||
op.add_column(
|
||||
"huya_tasks",
|
||||
sa.Column("handbook_scope", sa.String(length=16), nullable=False, server_default="legacy"),
|
||||
)
|
||||
op.create_index("ix_huya_tasks_handbook_scope", "huya_tasks", ["handbook_scope"])
|
||||
|
||||
inspector = sa.inspect(bind)
|
||||
if not inspector.has_table("huya_workbench_accounts"):
|
||||
op.create_table(
|
||||
"huya_workbench_accounts",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=False),
|
||||
sa.Column("handbook_scope", sa.String(length=16), nullable=False),
|
||||
sa.Column("account_id", sa.Integer(), sa.ForeignKey("huya_accounts.id"), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=True),
|
||||
sa.UniqueConstraint("user_id", "handbook_scope", "account_id", name="uq_huya_workbench_account"),
|
||||
)
|
||||
op.create_index("ix_huya_workbench_accounts_user_id", "huya_workbench_accounts", ["user_id"])
|
||||
op.create_index("ix_huya_workbench_accounts_handbook_scope", "huya_workbench_accounts", ["handbook_scope"])
|
||||
op.create_index("ix_huya_workbench_accounts_account_id", "huya_workbench_accounts", ["account_id"])
|
||||
|
||||
if not inspector.has_table("huya_workbenches"):
|
||||
op.create_table(
|
||||
"huya_workbenches",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("user_id", sa.Integer(), sa.ForeignKey("users.id"), nullable=False),
|
||||
sa.Column("handbook_scope", sa.String(length=16), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=True),
|
||||
sa.UniqueConstraint("user_id", "handbook_scope", name="uq_huya_workbench"),
|
||||
)
|
||||
op.create_index("ix_huya_workbenches_user_id", "huya_workbenches", ["user_id"])
|
||||
op.create_index("ix_huya_workbenches_handbook_scope", "huya_workbenches", ["handbook_scope"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
if inspector.has_table("huya_workbenches"):
|
||||
op.drop_table("huya_workbenches")
|
||||
if inspector.has_table("huya_workbench_accounts"):
|
||||
op.drop_table("huya_workbench_accounts")
|
||||
if "handbook_scope" in _columns(bind, "huya_tasks"):
|
||||
op.drop_index("ix_huya_tasks_handbook_scope", table_name="huya_tasks")
|
||||
op.drop_column("huya_tasks", "handbook_scope")
|
||||
Reference in New Issue
Block a user