Compare commits

...
2 changed files with 39 additions and 0 deletions

No files matched your search

@@ -1256,3 +1256,36 @@ class TestIndexPageSize:
assert page_size_of(legacy_page_size_db) == DB_PAGE_SIZE assert page_size_of(legacy_page_size_db) == DB_PAGE_SIZE
assert DocumentChunksTable.count(store.client) == 2 assert DocumentChunksTable.count(store.client) == 2
def test_open_connection_closes_connection_when_setup_fails(
tmp_path: Path,
mocker: MockerFixture,
) -> None:
"""
GIVEN a table setup step that fails after the connection was opened
WHEN the store is created
THEN the setup error propagates
AND the connection that was opened is closed, not leaked
"""
opened: list[sqlite3.Connection] = []
real_connect = sqlite3.connect
def recording_connect(*args, **kwargs) -> sqlite3.Connection:
conn = real_connect(*args, **kwargs)
opened.append(conn)
return conn
mocker.patch("paperless_ai.vector_store.sqlite3.connect", recording_connect)
mocker.patch.object(
DocumentMetaTable,
"create",
side_effect=RuntimeError("setup failed"),
)
with pytest.raises(RuntimeError, match="setup failed"):
PaperlessSqliteVecVectorStore(uri=str(tmp_path))
assert len(opened) == 1
with pytest.raises(sqlite3.ProgrammingError):
opened[0].execute("SELECT 1")
+6
View File
@@ -4,6 +4,7 @@ import sqlite3
import struct import struct
from collections.abc import Iterator from collections.abc import Iterator
from collections.abc import Sequence from collections.abc import Sequence
from contextlib import ExitStack
from contextlib import contextmanager from contextlib import contextmanager
from pathlib import Path from pathlib import Path
from types import TracebackType from types import TracebackType
@@ -212,11 +213,15 @@ class PaperlessSqliteVecVectorStore(BasePydanticVectorStore):
@staticmethod @staticmethod
def _open_connection(db_path: str) -> sqlite3.Connection: def _open_connection(db_path: str) -> sqlite3.Connection:
with ExitStack() as stack:
conn = sqlite3.connect( conn = sqlite3.connect(
db_path, db_path,
timeout=30, timeout=30,
isolation_level=None, # autocommit; explicit transactions below isolation_level=None, # autocommit; explicit transactions below
) )
# Closes the connection (and its WAL/SHM handles) if any setup step
# below raises. pop_all() disarms it once setup has succeeded.
stack.callback(conn.close)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
conn.enable_load_extension(True) # noqa: FBT003 conn.enable_load_extension(True) # noqa: FBT003
sqlite_vec.load(conn) sqlite_vec.load(conn)
@@ -239,6 +244,7 @@ class PaperlessSqliteVecVectorStore(BasePydanticVectorStore):
# per chunk, like document_chunks), since every chunk of a document # per chunk, like document_chunks), since every chunk of a document
# shares the same modified value -- see get_modified_times(). # shares the same modified value -- see get_modified_times().
DocumentMetaTable.create(conn) DocumentMetaTable.create(conn)
stack.pop_all()
return conn return conn
@property @property