85 lines
2.9 KiB
Python
85 lines
2.9 KiB
Python
"""新增虎牙充值商品快照表
|
|
|
|
Revision ID: 20260704_0005
|
|
Revises: 20260704_0004
|
|
Create Date: 2026-07-04
|
|
"""
|
|
|
|
from collections.abc import Sequence
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
revision: str = "20260704_0005"
|
|
down_revision: str | None = "20260704_0004"
|
|
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 _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_recharge_goods_snapshot"):
|
|
op.create_table(
|
|
"huya_recharge_goods_snapshot",
|
|
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
|
sa.Column("spu_id", sa.String(length=64), nullable=False),
|
|
sa.Column("sku_id", sa.String(length=64), nullable=True),
|
|
sa.Column("name", sa.String(length=256), nullable=True),
|
|
sa.Column("price", sa.Integer(), nullable=True),
|
|
sa.Column("stock", sa.Integer(), nullable=True),
|
|
sa.Column("buy_limit", sa.Integer(), nullable=True),
|
|
sa.Column("icon", sa.String(length=512), nullable=True),
|
|
sa.Column("description", sa.String(length=512), nullable=True),
|
|
sa.Column("task_id", sa.String(length=64), nullable=True),
|
|
sa.Column("task_name", sa.String(length=256), 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_recharge_goods_snapshot_spu_id",
|
|
"huya_recharge_goods_snapshot",
|
|
["spu_id"],
|
|
)
|
|
_create_index_if_missing(
|
|
bind,
|
|
"ix_huya_recharge_goods_snapshot_sku_id",
|
|
"huya_recharge_goods_snapshot",
|
|
["sku_id"],
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
bind = op.get_bind()
|
|
if _has_table(bind, "huya_recharge_goods_snapshot"):
|
|
indexes = _indexes(bind, "huya_recharge_goods_snapshot")
|
|
if "ix_huya_recharge_goods_snapshot_sku_id" in indexes:
|
|
op.drop_index(
|
|
"ix_huya_recharge_goods_snapshot_sku_id",
|
|
table_name="huya_recharge_goods_snapshot",
|
|
)
|
|
if "ix_huya_recharge_goods_snapshot_spu_id" in indexes:
|
|
op.drop_index(
|
|
"ix_huya_recharge_goods_snapshot_spu_id",
|
|
table_name="huya_recharge_goods_snapshot",
|
|
)
|
|
op.drop_table("huya_recharge_goods_snapshot")
|