Files
mobile.de/alembic/versions/003_add_composite_indexes.py
2026-05-11 12:45:37 +03:00

37 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Индексы для масштабированной БД (20k+ записей)
from typing import Sequence, Union
from alembic import op
revision: str = "003_add_composite_indexes"
down_revision: Union[str, None] = "002_add_indexes"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Частичный индекс для активных машин MOBILEDE (is_sold = FALSE).
# Ускоряет поиск при операциях mark_sold.
op.execute(
'CREATE INDEX IF NOT EXISTS "ix_MOBILEDE_cars_active_MOBILEDE" '
'ON "MOBILEDE_cars" (origin_url) '
"WHERE is_sold = FALSE AND origin_id LIKE 'mobilede:%'"
)
# Составной индекс для проверки дубликатов изображений.
op.execute(
'CREATE INDEX IF NOT EXISTS "ix_MOBILEDE_images_car_id_fullres" '
'ON "MOBILEDE_images" (car_id, fullres_image)'
)
# Индекс для быстрого поиска существующих машин по origin_url.
op.execute(
'CREATE INDEX IF NOT EXISTS "ix_MOBILEDE_cars_origin_url_id" '
'ON "MOBILEDE_cars" (origin_url, origin_id)'
)
def downgrade() -> None:
# Миграция оставлена только для совместимости с общей production-базой.
pass