"""按账号和活动隔离虎牙宝典商品快照。""" from collections.abc import Sequence import sqlalchemy as sa from alembic import op revision: str = "20260901_0033" down_revision: str | None = "20260831_0032" 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() for table in ("huya_goods_snapshot", "huya_recharge_goods_snapshot"): columns = _columns(bind, table) if "account_id" not in columns: op.add_column(table, sa.Column("account_id", sa.Integer(), nullable=True)) op.create_index(f"ix_{table}_account_id", table, ["account_id"]) if "sid" not in columns: op.add_column( table, sa.Column("sid", sa.Integer(), nullable=False, server_default="0"), ) op.create_index(f"ix_{table}_sid", table, ["sid"]) def downgrade() -> None: bind = op.get_bind() for table in ("huya_goods_snapshot", "huya_recharge_goods_snapshot"): columns = _columns(bind, table) if "sid" in columns: op.drop_index(f"ix_{table}_sid", table_name=table) op.drop_column(table, "sid") if "account_id" in columns: op.drop_index(f"ix_{table}_account_id", table_name=table) op.drop_column(table, "account_id")