Use raw SQL for performance index migration
This commit is contained in:
@@ -6,7 +6,6 @@ Create Date: 2026-07-01 00:01:00.000000
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
from alembic import op
|
from alembic import op
|
||||||
import sqlalchemy as sa
|
|
||||||
|
|
||||||
|
|
||||||
revision = '20260701_0001_add_performance_indexes'
|
revision = '20260701_0001_add_performance_indexes'
|
||||||
@@ -15,58 +14,37 @@ branch_labels = None
|
|||||||
depends_on = 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:
|
def upgrade() -> None:
|
||||||
_create_index_if_missing(
|
op.execute(
|
||||||
"ix_users_registered_activity",
|
"CREATE INDEX IF NOT EXISTS ix_users_registered_activity "
|
||||||
"users",
|
"ON users (is_registered, last_activity)"
|
||||||
["is_registered", "last_activity"],
|
|
||||||
)
|
)
|
||||||
_create_index_if_missing(
|
op.execute(
|
||||||
"ix_participations_lottery_account",
|
"CREATE INDEX IF NOT EXISTS ix_participations_lottery_account "
|
||||||
"participations",
|
"ON participations (lottery_id, account_number)"
|
||||||
["lottery_id", "account_number"],
|
|
||||||
)
|
)
|
||||||
_create_index_if_missing(
|
op.execute(
|
||||||
"ix_participations_user_created",
|
"CREATE INDEX IF NOT EXISTS ix_participations_user_created "
|
||||||
"participations",
|
"ON participations (user_id, created_at)"
|
||||||
["user_id", "created_at"],
|
|
||||||
)
|
)
|
||||||
_create_index_if_missing(
|
op.execute(
|
||||||
"ix_winners_lottery_place",
|
"CREATE INDEX IF NOT EXISTS ix_winners_lottery_place "
|
||||||
"winners",
|
"ON winners (lottery_id, place)"
|
||||||
["lottery_id", "place"],
|
|
||||||
)
|
)
|
||||||
_create_index_if_missing(
|
op.execute(
|
||||||
"ix_winners_user_id",
|
"CREATE INDEX IF NOT EXISTS ix_winners_user_id "
|
||||||
"winners",
|
"ON winners (user_id)"
|
||||||
["user_id"],
|
|
||||||
)
|
)
|
||||||
_create_index_if_missing(
|
op.execute(
|
||||||
"ix_blocked_users_lookup",
|
"CREATE INDEX IF NOT EXISTS ix_blocked_users_lookup "
|
||||||
"blocked_users",
|
"ON blocked_users (telegram_id, error_type, is_active)"
|
||||||
["telegram_id", "error_type", "is_active"],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
_drop_index_if_exists("ix_blocked_users_lookup", "blocked_users")
|
op.execute("DROP INDEX IF EXISTS ix_blocked_users_lookup")
|
||||||
_drop_index_if_exists("ix_winners_user_id", "winners")
|
op.execute("DROP INDEX IF EXISTS ix_winners_user_id")
|
||||||
_drop_index_if_exists("ix_winners_lottery_place", "winners")
|
op.execute("DROP INDEX IF EXISTS ix_winners_lottery_place")
|
||||||
_drop_index_if_exists("ix_participations_user_created", "participations")
|
op.execute("DROP INDEX IF EXISTS ix_participations_user_created")
|
||||||
_drop_index_if_exists("ix_participations_lottery_account", "participations")
|
op.execute("DROP INDEX IF EXISTS ix_participations_lottery_account")
|
||||||
_drop_index_if_exists("ix_users_registered_activity", "users")
|
op.execute("DROP INDEX IF EXISTS ix_users_registered_activity")
|
||||||
|
|||||||
Reference in New Issue
Block a user