Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 4 additions & 41 deletions src/basic_memory/repository/postgres_search_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ async def _ensure_vector_tables(self) -> None:
) from exc

# --- Chunks table (dimension-independent, may already exist via migration) ---
# Trigger: fresh Postgres projects may not have vector chunk tables yet.
# Why: runtime can bootstrap missing tables, but schema evolution must stay
# in Alembic to avoid concurrent ALTER TABLE deadlocks during indexing.
# Outcome: new installs create the current schema; upgrades rely on migration.
await session.execute(
text(
"""
Expand All @@ -318,47 +322,6 @@ async def _ensure_vector_tables(self) -> None:
"""
)
)
await session.execute(
text(
"""
ALTER TABLE search_vector_chunks
ADD COLUMN IF NOT EXISTS entity_fingerprint TEXT
"""
)
)
await session.execute(
text(
"""
ALTER TABLE search_vector_chunks
ADD COLUMN IF NOT EXISTS embedding_model TEXT
"""
)
)
await session.execute(
text(
"""
UPDATE search_vector_chunks
SET entity_fingerprint = COALESCE(entity_fingerprint, ''),
embedding_model = COALESCE(embedding_model, '')
"""
)
)
await session.execute(
text(
"""
ALTER TABLE search_vector_chunks
ALTER COLUMN entity_fingerprint SET NOT NULL
"""
)
)
await session.execute(
text(
"""
ALTER TABLE search_vector_chunks
ALTER COLUMN embedding_model SET NOT NULL
"""
)
)

# --- Embeddings table (dimension-dependent, created at runtime) ---
# Trigger: provider dimensions may differ from what was previously deployed.
Expand Down
38 changes: 38 additions & 0 deletions tests/repository/test_postgres_search_repository_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import asyncio
from contextlib import asynccontextmanager
from unittest.mock import AsyncMock, MagicMock, patch

import pytest
Expand Down Expand Up @@ -169,6 +170,43 @@ async def test_skips_when_already_initialized(self):
assert repo._vector_tables_initialized is True


class TestEnsureVectorTablesSchemaBootstrapping:
"""Guard the runtime setup path against inline schema migration drift."""

@pytest.mark.asyncio
async def test_initialization_never_alters_chunk_table_schema(self, monkeypatch):
repo = _make_repo(
semantic_enabled=True,
embedding_provider=StubEmbeddingProvider(),
)
session = AsyncMock()

@asynccontextmanager
async def fake_scoped_session(session_maker):
yield session

monkeypatch.setattr(
"basic_memory.repository.postgres_search_repository.db.scoped_session",
fake_scoped_session,
)
monkeypatch.setattr(repo, "_get_existing_embedding_dims", AsyncMock(return_value=None))

await repo._ensure_vector_tables()

executed_sql = [str(call.args[0]) for call in session.execute.await_args_list]

assert any(
"CREATE TABLE IF NOT EXISTS search_vector_chunks" in sql for sql in executed_sql
)
assert any(
"CREATE TABLE IF NOT EXISTS search_vector_embeddings" in sql
for sql in executed_sql
)
assert not any("ALTER TABLE search_vector_chunks" in sql for sql in executed_sql)
session.commit.assert_awaited_once()
assert repo._vector_tables_initialized is True


# --- _run_vector_query empty embedding (line 395-396) ----------------------


Expand Down
Loading