"""API read-path indexes: attribute-driven listings (release_date, family, openness), importance-ordered change feeds, current benchmark leaderboards, page-view trending, review queue by kind. Revision ID: 0002 """ from __future__ import annotations from alembic import op revision = "0002" down_revision = "0001" branch_labels = None depends_on = None SQL = r""" create index if not exists entities_type_release_idx on entities (entity_type, (attributes->>'release_date') desc nulls last) where merged_into is null; create index if not exists entities_type_family_idx on entities (entity_type, (attributes->>'family')) where merged_into is null; create index if not exists entities_type_openness_idx on entities (entity_type, (attributes->>'openness')) where merged_into is null; create index if not exists entities_type_updated_idx on entities (entity_type, updated_at desc) where merged_into is null; create index if not exists entities_type_first_seen_idx on entities (entity_type, first_seen_at desc) where merged_into is null; create index if not exists entities_slug_id_idx on entities (id) include (slug); create index if not exists change_events_importance_idx on change_events (importance desc, observed_at desc); create index if not exists change_events_type_observed_idx on change_events (observed_at desc) where event_type <> 'DOCUMENT_CHANGED'; create index if not exists benchmark_results_current_idx on benchmark_results (benchmark_id, higher_is_better, score) where valid_to is null; create index if not exists benchmark_results_model_current_idx on benchmark_results (model_id) where valid_to is null; create index if not exists prices_current_model_idx on prices (model_id, provider_id) where valid_to is null; create index if not exists prices_valid_range_idx on prices (valid_from, valid_to); create index if not exists page_views_day_idx on page_views (day desc, views desc); create index if not exists review_queue_kind_idx on review_queue (kind, status, created_at desc); create index if not exists documents_source_idx on documents (source_id, last_fetched_at desc); create index if not exists claims_entity_valid_idx on claims (entity_id, valid_from, valid_to); create index if not exists relations_predicate_idx on relations (predicate, subject_id, object_id) where valid_to is null; """ def upgrade() -> None: for statement in _split(SQL): op.execute(statement) def downgrade() -> None: raise RuntimeError("AI Atlas migrations are forward-only: historical data is never disposable") def _split(sql: str) -> list[str]: out: list[str] = [] buf: list[str] = [] in_dollar = False for line in sql.splitlines(): stripped = line.strip() if stripped.count("$$") % 2 == 1: in_dollar = not in_dollar buf.append(line) if not in_dollar and stripped.endswith(";"): body = [ln for ln in buf if not ln.strip().startswith("--") or in_dollar] stmt = "\n".join(body).strip() if stmt: out.append(stmt) buf = [] tail = "\n".join(ln for ln in buf if not ln.strip().startswith("--")).strip() if tail: out.append(tail) return out