diff --git a/src/basic_memory/repository/postgres_search_repository.py b/src/basic_memory/repository/postgres_search_repository.py index f9b425694..74a62c24f 100644 --- a/src/basic_memory/repository/postgres_search_repository.py +++ b/src/basic_memory/repository/postgres_search_repository.py @@ -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( """ @@ -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. diff --git a/tests/repository/test_postgres_search_repository_unit.py b/tests/repository/test_postgres_search_repository_unit.py index 8fff44200..fa801ee02 100644 --- a/tests/repository/test_postgres_search_repository_unit.py +++ b/tests/repository/test_postgres_search_repository_unit.py @@ -6,6 +6,7 @@ """ import asyncio +from contextlib import asynccontextmanager from unittest.mock import AsyncMock, MagicMock, patch import pytest @@ -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) ----------------------