Use raw SQL for performance index migration

This commit is contained in:
Андрей Цой
2026-07-01 21:24:17 +09:00
parent 26c65b7caa
commit c7c60b70c8

View File

@@ -6,7 +6,6 @@ Create Date: 2026-07-01 00:01:00.000000
"""
from alembic import op
import sqlalchemy as sa
revision = '20260701_0001_add_performance_indexes'
@@ -15,58 +14,37 @@ branch_labels = None
depends_on = None
def _index_exists(table_name: str, index_name: str) -> bool:
inspector = sa.inspect(op.get_bind())
return any(index["name"] == index_name for index in inspector.get_indexes(table_name))
def _create_index_if_missing(index_name: str, table_name: str, columns: list[str], unique: bool = False) -> None:
if not _index_exists(table_name, index_name):
op.create_index(index_name, table_name, columns, unique=unique)
def _drop_index_if_exists(index_name: str, table_name: str) -> None:
if _index_exists(table_name, index_name):
op.drop_index(index_name, table_name=table_name)
def upgrade() -> None:
_create_index_if_missing(
"ix_users_registered_activity",
"users",
["is_registered", "last_activity"],
op.execute(
"CREATE INDEX IF NOT EXISTS ix_users_registered_activity "
"ON users (is_registered, last_activity)"
)
_create_index_if_missing(
"ix_participations_lottery_account",
"participations",
["lottery_id", "account_number"],
op.execute(
"CREATE INDEX IF NOT EXISTS ix_participations_lottery_account "
"ON participations (lottery_id, account_number)"
)
_create_index_if_missing(
"ix_participations_user_created",
"participations",
["user_id", "created_at"],
op.execute(
"CREATE INDEX IF NOT EXISTS ix_participations_user_created "
"ON participations (user_id, created_at)"
)
_create_index_if_missing(
"ix_winners_lottery_place",
"winners",
["lottery_id", "place"],
op.execute(
"CREATE INDEX IF NOT EXISTS ix_winners_lottery_place "
"ON winners (lottery_id, place)"
)
_create_index_if_missing(
"ix_winners_user_id",
"winners",
["user_id"],
op.execute(
"CREATE INDEX IF NOT EXISTS ix_winners_user_id "
"ON winners (user_id)"
)
_create_index_if_missing(
"ix_blocked_users_lookup",
"blocked_users",
["telegram_id", "error_type", "is_active"],
op.execute(
"CREATE INDEX IF NOT EXISTS ix_blocked_users_lookup "
"ON blocked_users (telegram_id, error_type, is_active)"
)
def downgrade() -> None:
_drop_index_if_exists("ix_blocked_users_lookup", "blocked_users")
_drop_index_if_exists("ix_winners_user_id", "winners")
_drop_index_if_exists("ix_winners_lottery_place", "winners")
_drop_index_if_exists("ix_participations_user_created", "participations")
_drop_index_if_exists("ix_participations_lottery_account", "participations")
_drop_index_if_exists("ix_users_registered_activity", "users")
op.execute("DROP INDEX IF EXISTS ix_blocked_users_lookup")
op.execute("DROP INDEX IF EXISTS ix_winners_user_id")
op.execute("DROP INDEX IF EXISTS ix_winners_lottery_place")
op.execute("DROP INDEX IF EXISTS ix_participations_user_created")
op.execute("DROP INDEX IF EXISTS ix_participations_lottery_account")
op.execute("DROP INDEX IF EXISTS ix_users_registered_activity")