48 lines
1.9 KiB
Python
48 lines
1.9 KiB
Python
"""Add performance indexes for growing database
|
|
|
|
Revision ID: 002_add_indexes
|
|
Revises: 001_initial
|
|
Create Date: 2026-04-09
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
|
|
revision: str = "002_add_indexes"
|
|
down_revision: Union[str, None] = "001_initial"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
# cars: ускорение фильтрации по бренду в API и статистике
|
|
op.create_index("ix_cars_brand", "cars", ["brand"])
|
|
|
|
# cars: составной индекс бренд+модель для комбинированных фильтров
|
|
op.create_index("ix_cars_brand_model", "cars", ["brand", "model"])
|
|
|
|
# cars: ускорение фильтрации по году (year_min/year_max)
|
|
op.create_index("ix_cars_year", "cars", ["year"])
|
|
|
|
# cars: ускорение фильтрации по статусу продажи
|
|
op.create_index("ix_cars_is_sold", "cars", ["is_sold"])
|
|
|
|
# cars: ускорение сортировки ORDER BY last_seen_at DESC (пагинация)
|
|
op.create_index("ix_cars_last_seen_at", "cars", ["last_seen_at"])
|
|
|
|
# images: ускорение JOIN/DELETE по car_id (критично при upsert)
|
|
op.create_index("ix_images_car_id", "images", ["car_id"])
|
|
|
|
# sync_runs: ускорение поиска stale runs по статусу
|
|
op.create_index("ix_sync_runs_status", "sync_runs", ["status"])
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_index("ix_sync_runs_status", table_name="sync_runs")
|
|
op.drop_index("ix_images_car_id", table_name="images")
|
|
op.drop_index("ix_cars_last_seen_at", table_name="cars")
|
|
op.drop_index("ix_cars_is_sold", table_name="cars")
|
|
op.drop_index("ix_cars_year", table_name="cars")
|
|
op.drop_index("ix_cars_brand_model", table_name="cars")
|
|
op.drop_index("ix_cars_brand", table_name="cars")
|