55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
"""Phase 1 — confirm pgvector activation on memory_associative.
|
|
|
|
Migration 004 created the embedding column as vector(1536) and added the
|
|
IVFFlat index. This migration is the Phase-1 checkpoint:
|
|
1. Ensures the pgvector extension is enabled (idempotent).
|
|
2. Ensures the canonical Phase-1 IVFFlat index exists under the name
|
|
memory_associative_embedding_idx (creates it only if absent).
|
|
|
|
Revision ID: 005
|
|
Revises: 9a1f2d0b6c7e
|
|
Create Date: 2026-04-15
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
|
|
revision: str = "005"
|
|
down_revision: Union[str, None] = "e04100e88ace"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
# Ensure pgvector extension is enabled (also done in 004, idempotent).
|
|
op.execute("CREATE EXTENSION IF NOT EXISTS vector;")
|
|
|
|
# Ensure the canonical Phase-1 IVFFlat index exists.
|
|
# 004 may have created ix_memory_associative_embedding; this adds the
|
|
# Phase-1 name memory_associative_embedding_idx if it is missing.
|
|
op.execute(
|
|
"""
|
|
DO $$
|
|
BEGIN
|
|
IF NOT EXISTS (
|
|
SELECT 1
|
|
FROM pg_indexes
|
|
WHERE tablename = 'memory_associative'
|
|
AND indexname = 'memory_associative_embedding_idx'
|
|
) THEN
|
|
CREATE INDEX memory_associative_embedding_idx
|
|
ON memory_associative
|
|
USING ivfflat (embedding vector_cosine_ops)
|
|
WITH (lists = 100);
|
|
END IF;
|
|
END $$;
|
|
"""
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.execute("DROP INDEX IF EXISTS memory_associative_embedding_idx;")
|