Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

refactor: extract shared SQLCipher connection factory method (#1967)

LearningCircuit67866d99c460
diff --git a/src/local_deep_research/database/encrypted_db.py b/src/local_deep_research/database/encrypted_db.py
index ced7256d3d..bd9f6d0e0b 100644
--- a/src/local_deep_research/database/encrypted_db.py
+++ b/src/local_deep_research/database/encrypted_db.py
@@ -183,6 +183,63 @@ class DatabaseManager:
except Exception as e:
logger.debug(f"Could not apply mmap_size pragma: {e}")
+ @staticmethod
+ def _make_sqlcipher_connection(
+ db_path: Path,
+ password: str,
+ username: Optional[str] = None,
+ isolation_level: Optional[str] = "IMMEDIATE",
+ check_same_thread: bool = False,
+ ) -> Any:
+ """Create a properly initialized SQLCipher connection.
+
+ Follows the canonical SQLCipher initialization order: set key,
+ apply cipher pragmas, verify, then apply performance pragmas.
+ Cipher pragmas (page size, HMAC algorithm, KDF iterations) must
+ be configured before the first query (verification) because that
+ query triggers page decryption with the active cipher settings.
+
+ Args:
+ db_path: Path to the database file
+ password: The database encryption passphrase
+ username: Username passed to performance pragmas (optional)
+ isolation_level: SQLite isolation level (``""`` for deferred
+ transactions, ``None`` for autocommit)
+ check_same_thread: SQLite check_same_thread flag
+
+ Returns:
+ A raw ``sqlcipher3`` connection ready for use.
+
+ Raises:
+ ValueError: If the database key cannot be verified.
+ """
+ sqlcipher3 = get_sqlcipher_module()
+ conn = sqlcipher3.connect(
+ str(db_path),
+ isolation_level=isolation_level,
+ check_same_thread=check_same_thread,
+ )
+ cursor = conn.cursor()
+
+ try:
+ set_sqlcipher_key(cursor, password)
+ apply_sqlcipher_pragmas(cursor, creation_mode=False)
+
+ if not verify_sqlcipher_connection(cursor):
+ raise ValueError("Failed to verify database key")
+
+ apply_performance_pragmas(cursor, username)
+ except Exception:
+ try:
+ cursor.close()
+ except Exception: # noqa: BLE001
+ pass
+ conn.close()
+ raise
+
+ cursor.close()
+ return conn
+
def create_user_database(self, username: str, password: str) -> Engine:
"""Create a new encrypted database for a user."""
@@ -256,35 +313,12 @@ class DatabaseManager:
# Now create SQLAlchemy engine using custom connection creator
# This ensures encryption is properly initialized for every connection
- sqlcipher3 = get_sqlcipher_module()
-
- def create_sqlcipher_connection():
- """Create a properly initialized SQLCipher connection."""
- conn = sqlcipher3.connect(
- str(db_path),
- isolation_level="IMMEDIATE",
- check_same_thread=False,
- )
- cursor = conn.cursor()
-
- # Use centralized SQLCipher setup
- set_sqlcipher_key(cursor, password)
-
- # Verify connection works
- if not verify_sqlcipher_connection(cursor):
- raise ValueError("Failed to verify database key")
-
- # Apply SQLCipher and performance settings
- apply_sqlcipher_pragmas(cursor, creation_mode=False)
- apply_performance_pragmas(cursor, username)
-
- cursor.close()
- return conn
-
# Create engine with custom creator function and optimized cache
engine = create_engine(
"sqlite://",
- creator=create_sqlcipher_connection,
+ creator=lambda: self._make_sqlcipher_connection(
+ db_path, password, username
+ ),
poolclass=self._pool_class,
echo=False,
query_cache_size=1000, # Increased for complex queries with SQLCipher
@@ -360,36 +394,12 @@ class DatabaseManager:
# Create connection string - use regular SQLite when SQLCipher not available
if self.has_encryption:
- # Use the same custom connection creator approach as create_user_database
- sqlcipher3 = get_sqlcipher_module()
-
- def create_sqlcipher_connection():
- """Create a properly initialized SQLCipher connection."""
- conn = sqlcipher3.connect(
- str(db_path),
- isolation_level="IMMEDIATE",
- check_same_thread=False,
- )
- cursor = conn.cursor()
-
- # Use centralized SQLCipher setup
- set_sqlcipher_key(cursor, password)
-
- # Verify connection works
- if not verify_sqlcipher_connection(cursor):
- raise ValueError("Failed to verify database key")
-
- # Apply SQLCipher and performance settings
- apply_sqlcipher_pragmas(cursor, creation_mode=False)
- apply_performance_pragmas(cursor, username)
-
- cursor.close()
- return conn
-
# Create engine with custom creator function and optimized cache
engine = create_engine(
"sqlite://",
- creator=create_sqlcipher_connection,
+ creator=lambda: self._make_sqlcipher_connection(
+ db_path, password, username
+ ),
poolclass=self._pool_class,
echo=False,
query_cache_size=1000, # Increased for complex queries with SQLCipher
@@ -659,35 +669,19 @@ class DatabaseManager:
# Create a thread-local engine
if self.has_encryption:
- sqlcipher3 = get_sqlcipher_module()
def create_thread_connection():
"""Create a SQLCipher connection for this thread."""
try:
- conn = sqlcipher3.connect(
- str(db_path), check_same_thread=False
+ return self._make_sqlcipher_connection(
+ db_path, password, username, isolation_level=""
)
- cursor = conn.cursor()
-
- # Use centralized SQLCipher setup
- set_sqlcipher_key(cursor, password)
- apply_sqlcipher_pragmas(cursor, creation_mode=False)
-
- # Verify connection works
- if not verify_sqlcipher_connection(cursor):
- raise ValueError("Failed to verify database key")
except Exception as e:
logger.exception(
f"Failed to create thread connection for {username}: {e}"
)
raise
- # Apply performance pragmas for metrics writes
- apply_performance_pragmas(cursor, username)
-
- cursor.close()
- return conn
-
engine = create_engine(
"sqlite://",
creator=create_thread_connection,
diff --git a/tests/database/test_sqlcipher_connection_factory.py b/tests/database/test_sqlcipher_connection_factory.py
new file mode 100644
index 0000000000..1635773fd1
--- /dev/null
+++ b/tests/database/test_sqlcipher_connection_factory.py
@@ -0,0 +1,403 @@
+"""Tests for _make_sqlcipher_connection() factory method.
+
+Verifies:
+- Factory method creates connections with correct parameters
+- Correct call order: key → pragmas → verify → performance
+- Parameters are passed through correctly
+- Failed verification raises ValueError
+- Metrics path preserves error logging
+"""
+
+from pathlib import Path
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from local_deep_research.database.encrypted_db import DatabaseManager
+
+# Module path prefix for patching
+_EDB = "local_deep_research.database.encrypted_db"
+
+
+def manager():
+ """Create a DatabaseManager with encryption disabled for testing."""
+ with patch(f"{_EDB}.get_data_directory") as mock_dir:
+ mock_dir.return_value = Path("/tmp/test")
+ with patch.object(
+ DatabaseManager, "_check_encryption_available", return_value=False
+ ):
+ with patch.dict("os.environ", {"LDR_ALLOW_UNENCRYPTED": "true"}):
+ return DatabaseManager()
+
+
+def mock_sqlcipher():
+ """Mock all SQLCipher dependencies for _make_sqlcipher_connection."""
+ mock_conn = MagicMock()
+ mock_cursor = MagicMock()
+ mock_conn.cursor.return_value = mock_cursor
+
+ mock_module = MagicMock()
+ mock_module.connect.return_value = mock_conn
+
+ with (
+ patch(
+ f"{_EDB}.get_sqlcipher_module", return_value=mock_module
+ ) as mock_get,
+ patch(f"{_EDB}.set_sqlcipher_key") as mock_set_key,
+ patch(
+ f"{_EDB}.verify_sqlcipher_connection", return_value=True
+ ) as mock_verify,
+ patch(f"{_EDB}.apply_sqlcipher_pragmas") as mock_pragmas,
+ patch(f"{_EDB}.apply_performance_pragmas") as mock_perf,
+ ):
+ yield {
+ "module": mock_module,
+ "conn": mock_conn,
+ "cursor": mock_cursor,
+ "get_module": mock_get,
+ "set_key": mock_set_key,
+ "verify": mock_verify,
+ "pragmas": mock_pragmas,
+ "performance": mock_perf,
+ }
+
+
+class TestMakeSqlcipherConnection:
+ """Verify _make_sqlcipher_connection creates connections correctly."""
+
+ def test_returns_connection(self, manager, mock_sqlcipher):
+ """Factory should return the connection object from sqlcipher3.connect."""
+ result = manager._make_sqlcipher_connection(
+ Path("/tmp/test.db"), "secret"
+ )
+ assert result is mock_sqlcipher["conn"]
+
+ def test_connect_with_default_params(self, manager, mock_sqlcipher):
+ """Default params: isolation_level=IMMEDIATE, check_same_thread=False."""
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ mock_sqlcipher["module"].connect.assert_called_once_with(
+ "/tmp/test.db",
+ isolation_level="IMMEDIATE",
+ check_same_thread=False,
+ )
+
+ def test_connect_with_custom_isolation_level(self, manager, mock_sqlcipher):
+ """Metrics path passes isolation_level='' for deferred transactions."""
+ manager._make_sqlcipher_connection(
+ Path("/tmp/test.db"), "secret", isolation_level=""
+ )
+
+ mock_sqlcipher["module"].connect.assert_called_once_with(
+ "/tmp/test.db",
+ isolation_level="",
+ check_same_thread=False,
+ )
+
+ def test_connect_with_check_same_thread(self, manager, mock_sqlcipher):
+ """check_same_thread should be forwarded to sqlcipher3.connect."""
+ manager._make_sqlcipher_connection(
+ Path("/tmp/test.db"), "secret", check_same_thread=True
+ )
+
+ mock_sqlcipher["module"].connect.assert_called_once_with(
+ "/tmp/test.db",
+ isolation_level="IMMEDIATE",
+ check_same_thread=True,
+ )
+
+ def test_sets_sqlcipher_key(self, manager, mock_sqlcipher):
+ """Must call set_sqlcipher_key with cursor and password."""
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "my_pass")
+
+ mock_sqlcipher["set_key"].assert_called_once_with(
+ mock_sqlcipher["cursor"], "my_pass"
+ )
+
+ def test_verifies_connection(self, manager, mock_sqlcipher):
+ """Must verify connection works after setting key."""
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ mock_sqlcipher["verify"].assert_called_once_with(
+ mock_sqlcipher["cursor"]
+ )
+
+ def test_applies_sqlcipher_pragmas(self, manager, mock_sqlcipher):
+ """Must apply SQLCipher pragmas with creation_mode=False."""
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ mock_sqlcipher["pragmas"].assert_called_once_with(
+ mock_sqlcipher["cursor"], creation_mode=False
+ )
+
+ def test_applies_performance_pragmas_with_username(
+ self, manager, mock_sqlcipher
+ ):
+ """Must pass username to apply_performance_pragmas."""
+ manager._make_sqlcipher_connection(
+ Path("/tmp/test.db"), "secret", username="alice"
+ )
+
+ mock_sqlcipher["performance"].assert_called_once_with(
+ mock_sqlcipher["cursor"], "alice"
+ )
+
+ def test_applies_performance_pragmas_without_username(
+ self, manager, mock_sqlcipher
+ ):
+ """Username defaults to None when not provided."""
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ mock_sqlcipher["performance"].assert_called_once_with(
+ mock_sqlcipher["cursor"], None
+ )
+
+ def test_closes_cursor(self, manager, mock_sqlcipher):
+ """Cursor must be closed before returning."""
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ mock_sqlcipher["cursor"].close.assert_called_once()
+
+ def test_call_order(self, manager, mock_sqlcipher):
+ """Operations must happen in order: key → pragmas → verify → performance."""
+ call_order = []
+ mock_sqlcipher["set_key"].side_effect = lambda *a: call_order.append(
+ "key"
+ )
+ mock_sqlcipher["verify"].side_effect = lambda *a: (
+ call_order.append("verify") or True
+ )
+ mock_sqlcipher["pragmas"].side_effect = (
+ lambda *a, **kw: call_order.append("pragmas")
+ )
+ mock_sqlcipher["performance"].side_effect = (
+ lambda *a: call_order.append("performance")
+ )
+
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ assert call_order == ["key", "pragmas", "verify", "performance"]
+
+ def test_raises_on_failed_verification(self, manager, mock_sqlcipher):
+ """Must raise ValueError when verification fails."""
+ mock_sqlcipher["verify"].return_value = False
+
+ with pytest.raises(ValueError, match="Failed to verify database key"):
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ def test_no_performance_pragmas_after_failed_verification(
+ self, manager, mock_sqlcipher
+ ):
+ """Cipher pragmas run before verify, but performance pragmas must not."""
+ mock_sqlcipher["verify"].return_value = False
+
+ with pytest.raises(ValueError):
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ mock_sqlcipher["pragmas"].assert_called_once_with(
+ mock_sqlcipher["cursor"], creation_mode=False
+ )
+ mock_sqlcipher["performance"].assert_not_called()
+
+ def test_closes_cursor_and_conn_on_verification_failure(
+ self, manager, mock_sqlcipher
+ ):
+ """Cursor and connection must be closed when verification fails."""
+ mock_sqlcipher["verify"].return_value = False
+
+ with pytest.raises(ValueError):
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ mock_sqlcipher["cursor"].close.assert_called_once()
+ mock_sqlcipher["conn"].close.assert_called_once()
+
+ def test_closes_cursor_and_conn_on_key_error(self, manager, mock_sqlcipher):
+ """Cursor and connection must be closed when set_sqlcipher_key raises."""
+ mock_sqlcipher["set_key"].side_effect = RuntimeError("key failed")
+
+ with pytest.raises(RuntimeError):
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ mock_sqlcipher["cursor"].close.assert_called_once()
+ mock_sqlcipher["conn"].close.assert_called_once()
+
+ def test_closes_cursor_and_conn_on_pragma_error(
+ self, manager, mock_sqlcipher
+ ):
+ """Cursor and connection must be closed when pragma application raises."""
+ mock_sqlcipher["pragmas"].side_effect = RuntimeError("pragma failed")
+
+ with pytest.raises(RuntimeError):
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ mock_sqlcipher["cursor"].close.assert_called_once()
+ mock_sqlcipher["conn"].close.assert_called_once()
+ mock_sqlcipher["verify"].assert_not_called()
+ mock_sqlcipher["performance"].assert_not_called()
+
+ def test_connect_converts_path_to_string(self, manager, mock_sqlcipher):
+ """Path objects must be converted to str for sqlcipher3.connect."""
+ manager._make_sqlcipher_connection(Path("/some/dir/test.db"), "secret")
+
+ call_args = mock_sqlcipher["module"].connect.call_args
+ assert call_args[0][0] == "/some/dir/test.db"
+ assert isinstance(call_args[0][0], str)
+
+ def test_connect_with_none_isolation_level(self, manager, mock_sqlcipher):
+ """isolation_level=None (autocommit) must be forwarded to connect."""
+ manager._make_sqlcipher_connection(
+ Path("/tmp/test.db"), "secret", isolation_level=None
+ )
+
+ mock_sqlcipher["module"].connect.assert_called_once_with(
+ "/tmp/test.db",
+ isolation_level=None,
+ check_same_thread=False,
+ )
+
+ def test_conn_not_closed_on_success(self, manager, mock_sqlcipher):
+ """On success, cursor is closed but connection must remain open."""
+ result = manager._make_sqlcipher_connection(
+ Path("/tmp/test.db"), "secret"
+ )
+
+ mock_sqlcipher["cursor"].close.assert_called_once()
+ mock_sqlcipher["conn"].close.assert_not_called()
+ assert result is mock_sqlcipher["conn"]
+
+ def test_key_error_prevents_further_calls(self, manager, mock_sqlcipher):
+ """If set_sqlcipher_key fails, no pragmas or verify should run."""
+ mock_sqlcipher["set_key"].side_effect = RuntimeError("bad key")
+
+ with pytest.raises(RuntimeError):
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ mock_sqlcipher["pragmas"].assert_not_called()
+ mock_sqlcipher["verify"].assert_not_called()
+ mock_sqlcipher["performance"].assert_not_called()
+
+ def test_original_exception_propagated(self, manager, mock_sqlcipher):
+ """The original exception must propagate without wrapping."""
+ original = RuntimeError("specific error")
+ mock_sqlcipher["pragmas"].side_effect = original
+
+ with pytest.raises(RuntimeError) as exc_info:
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ assert exc_info.value is original
+
+ def test_conn_closed_even_if_cursor_close_raises(
+ self, manager, mock_sqlcipher
+ ):
+ """Connection must close even when cursor.close() raises during cleanup."""
+ mock_sqlcipher["verify"].return_value = False
+ mock_sqlcipher["cursor"].close.side_effect = RuntimeError(
+ "cursor close failed"
+ )
+
+ with pytest.raises(ValueError, match="Failed to verify database key"):
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ mock_sqlcipher["conn"].close.assert_called_once()
+
+ def test_import_error_propagates(self, manager):
+ """ImportError from get_sqlcipher_module() must propagate cleanly."""
+ with patch(
+ f"{_EDB}.get_sqlcipher_module",
+ side_effect=ImportError("no sqlcipher3"),
+ ):
+ with pytest.raises(ImportError, match="no sqlcipher3"):
+ manager._make_sqlcipher_connection(
+ Path("/tmp/test.db"), "secret"
+ )
+
+ def test_connect_failure_propagates(self, manager, mock_sqlcipher):
+ """Exceptions from sqlcipher3.connect() must propagate without cleanup attempts."""
+ mock_sqlcipher["module"].connect.side_effect = OSError("disk full")
+
+ with pytest.raises(OSError, match="disk full"):
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ mock_sqlcipher["set_key"].assert_not_called()
+
+ def test_closes_cursor_and_conn_on_performance_pragma_error(
+ self, manager, mock_sqlcipher
+ ):
+ """Cursor and connection must be closed when apply_performance_pragmas raises."""
+ mock_sqlcipher["performance"].side_effect = RuntimeError(
+ "perf pragma failed"
+ )
+
+ with pytest.raises(RuntimeError, match="perf pragma failed"):
+ manager._make_sqlcipher_connection(
+ Path("/tmp/test.db"), "secret", username="alice"
+ )
+
+ mock_sqlcipher["cursor"].close.assert_called_once()
+ mock_sqlcipher["conn"].close.assert_called_once()
+ mock_sqlcipher["verify"].assert_called_once()
+
+ def test_closes_cursor_and_conn_on_verify_exception(
+ self, manager, mock_sqlcipher
+ ):
+ """Cleanup must happen when verify_sqlcipher_connection raises (not just returns False)."""
+ mock_sqlcipher["verify"].side_effect = RuntimeError("verify crashed")
+
+ with pytest.raises(RuntimeError, match="verify crashed"):
+ manager._make_sqlcipher_connection(Path("/tmp/test.db"), "secret")
+
+ mock_sqlcipher["cursor"].close.assert_called_once()
+ mock_sqlcipher["conn"].close.assert_called_once()
+ mock_sqlcipher["performance"].assert_not_called()
+
+
+class TestMetricsThreadConnectionLogging:
+ """Verify that create_thread_safe_session_for_metrics preserves error logging."""
+
+ @patch(f"{_EDB}.get_data_directory")
+ def test_logs_exception_on_connection_failure(self, mock_dir, tmp_path):
+ """Metrics thread connection failures must be logged with logger.exception."""
+ mock_dir.return_value = tmp_path
+
+ with patch.object(
+ DatabaseManager, "_check_encryption_available", return_value=True
+ ):
+ manager = DatabaseManager()
+ manager.has_encryption = True
+
+ db_path = tmp_path / "test.db"
+ db_path.touch()
+
+ with patch.object(
+ manager, "_get_user_db_path", return_value=db_path
+ ):
+ # Make _make_sqlcipher_connection raise an error
+ with patch.object(
+ manager,
+ "_make_sqlcipher_connection",
+ side_effect=RuntimeError("connection failed"),
+ ):
+ with patch(f"{_EDB}.logger") as mock_logger:
+ with patch(f"{_EDB}.create_engine") as mock_engine:
+ # create_engine should use our creator function
+ # Capture the creator argument and call it
+ def capture_creator(*args, **kwargs):
+ creator = kwargs.get("creator")
+ if creator:
+ with pytest.raises(RuntimeError):
+ creator()
+ return MagicMock()
+
+ mock_engine.side_effect = capture_creator
+
+ manager.create_thread_safe_session_for_metrics(
+ "testuser", "secret"
+ )
+
+ # Verify logger.exception was called
+ mock_logger.exception.assert_called_once()
+ log_msg = mock_logger.exception.call_args[0][0]
+ assert "testuser" in log_msg
+ assert "connection failed" in log_msg