"""pg_trgm + composite indexes for memory evidence ILIKE and filters. Revision ID: 0014_memory_evidence_indexes Revises: 0013_playground_judge """ from typing import Sequence, Union from alembic import op revision: str = "0014_memory_evidence_indexes" down_revision: Union[str, None] = "0013_playground_judge" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: op.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm") op.create_index( "ix_memory_facts_user_status", "memory_facts", ["user_id", "status"], unique=False, ) op.execute( "CREATE INDEX IF NOT EXISTS ix_memory_facts_subject_trgm " "ON memory_facts USING gin (subject gin_trgm_ops)" ) op.execute( "CREATE INDEX IF NOT EXISTS ix_memory_facts_predicate_trgm " "ON memory_facts USING gin (predicate gin_trgm_ops)" ) op.execute( "CREATE INDEX IF NOT EXISTS ix_timeline_events_title_trgm " "ON timeline_events USING gin (title gin_trgm_ops)" ) op.execute( "CREATE INDEX IF NOT EXISTS ix_timeline_events_description_trgm " "ON timeline_events USING gin (description gin_trgm_ops)" ) op.execute( "CREATE INDEX IF NOT EXISTS ix_memory_summaries_content_trgm " "ON memory_summaries USING gin (content gin_trgm_ops)" ) op.create_index( "ix_stories_user_status", "stories", ["user_id", "status"], unique=False, ) op.execute( "CREATE INDEX IF NOT EXISTS ix_stories_title_trgm " "ON stories USING gin (title gin_trgm_ops)" ) op.execute( "CREATE INDEX IF NOT EXISTS ix_stories_summary_trgm " "ON stories USING gin (summary gin_trgm_ops)" ) op.execute( "CREATE INDEX IF NOT EXISTS ix_memory_chunks_embedding_hnsw " "ON memory_chunks USING hnsw (embedding vector_cosine_ops) " "WITH (m = 16, ef_construction = 64)" ) def downgrade() -> None: op.execute("DROP INDEX IF EXISTS ix_memory_chunks_embedding_hnsw") op.execute("DROP INDEX IF EXISTS ix_stories_summary_trgm") op.execute("DROP INDEX IF EXISTS ix_stories_title_trgm") op.drop_index("ix_stories_user_status", table_name="stories") op.execute("DROP INDEX IF EXISTS ix_memory_summaries_content_trgm") op.execute("DROP INDEX IF EXISTS ix_timeline_events_description_trgm") op.execute("DROP INDEX IF EXISTS ix_timeline_events_title_trgm") op.execute("DROP INDEX IF EXISTS ix_memory_facts_predicate_trgm") op.execute("DROP INDEX IF EXISTS ix_memory_facts_subject_trgm") op.drop_index("ix_memory_facts_user_status", table_name="memory_facts")