Files
live-hub-py/web/backend/migrations/versions/20261118_0091_account_commodity_states.py
T

168 lines
5.0 KiB
Python

"""账号商品兑换能力索引。
本表把“某账号对某商品当前能不能换”从 ``overview_snapshot`` JSON 中拆出来,
用于履约秒级候选查询。它只保存最后一次官方查询的派生结论;兑换事实仍写
``account_events``。
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision: str = "20261118_0091"
down_revision: str | None = "20261118_0090"
branch_labels: str | None = None
depends_on: str | None = None
TABLE = "account_commodity_states"
def _table_exists(name: str) -> bool:
return bool(
op.get_bind()
.execute(
sa.text(
"SELECT COUNT(*) FROM information_schema.tables "
"WHERE table_schema = DATABASE() AND table_name = :name"
),
{"name": name},
)
.scalar()
)
def _index_exists(name: str) -> bool:
return bool(
op.get_bind()
.execute(
sa.text(
"SELECT COUNT(*) FROM information_schema.statistics "
"WHERE table_schema = DATABASE() "
"AND table_name = :table AND index_name = :name"
),
{"table": TABLE, "name": name},
)
.scalar()
)
def upgrade() -> None:
if not _table_exists(TABLE):
op.create_table(
TABLE,
sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
sa.Column("account_id", sa.Integer(), nullable=False),
sa.Column(
"platform",
sa.String(length=16),
nullable=False,
server_default="douyu",
),
sa.Column(
"scope",
sa.String(length=16),
nullable=False,
server_default="elite",
),
sa.Column("commodity_id", sa.String(length=64), nullable=False),
sa.Column(
"commodity_name",
sa.String(length=256),
nullable=False,
server_default="",
),
sa.Column(
"availability",
sa.String(length=16),
nullable=False,
server_default="unknown",
),
sa.Column(
"blocking_reason",
sa.String(length=255),
nullable=False,
server_default="",
),
sa.Column(
"raw_status",
sa.String(length=16),
nullable=False,
server_default="",
),
sa.Column("observed_at", sa.DateTime(), nullable=True),
sa.Column("valid_until", sa.DateTime(), nullable=True),
sa.Column("next_check_at", sa.DateTime(), nullable=True),
sa.Column("last_exchange_at", sa.DateTime(), nullable=True),
sa.Column(
"exchange_count_365d",
sa.Integer(),
nullable=False,
server_default="0",
),
sa.Column(
"source", sa.String(length=24), nullable=False, server_default=""
),
sa.Column(
"error_code",
sa.String(length=64),
nullable=False,
server_default="",
),
sa.Column(
"created_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
sa.Column(
"updated_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
sa.PrimaryKeyConstraint("id"),
sa.ForeignKeyConstraint(
["account_id"],
["accounts.id"],
name="fk_account_commodity_account",
ondelete="CASCADE",
),
sa.UniqueConstraint(
"account_id",
"platform",
"scope",
"commodity_id",
name="uq_account_commodity_state",
),
)
if not _index_exists("ix_account_commodity_match"):
op.create_index(
"ix_account_commodity_match",
TABLE,
[
"platform",
"scope",
"commodity_id",
"availability",
"valid_until",
"account_id",
],
)
if not _index_exists("ix_account_commodity_account"):
op.create_index(
"ix_account_commodity_account",
TABLE,
["account_id", "platform", "scope"],
)
def downgrade() -> None:
if _index_exists("ix_account_commodity_account"):
op.drop_index("ix_account_commodity_account", table_name=TABLE)
if _index_exists("ix_account_commodity_match"):
op.drop_index("ix_account_commodity_match", table_name=TABLE)
if _table_exists(TABLE):
op.drop_table(TABLE)