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.

fix: Adjust SQLCipher setup order in DatabaseManager and sqlcipher_utils, fixes db connection error

zhd40709ebedcab
diff --git a/src/local_deep_research/database/encrypted_db.py b/src/local_deep_research/database/encrypted_db.py
index 0ca27cfdb4..8677662f26 100644
--- a/src/local_deep_research/database/encrypted_db.py
+++ b/src/local_deep_research/database/encrypted_db.py
@@ -194,9 +194,10 @@ class DatabaseManager:
conn = sqlcipher3.connect(
str(db_path), isolation_level=None, check_same_thread=False
)
- # Use centralized SQLCipher setup
- set_sqlcipher_key(conn, password)
+ # Use centralized SQLCipher setup - apply BEFORE setting key
apply_sqlcipher_pragmas(conn, creation_mode=True)
+ set_sqlcipher_key(conn, password)
+ apply_performance_pragmas(conn, username)
# Get the CREATE TABLE statements from SQLAlchemy models
from sqlalchemy.dialects import sqlite
@@ -242,17 +243,15 @@ class DatabaseManager:
)
cursor = conn.cursor()
- # Use centralized SQLCipher setup
+ # Use same order as creation: pragmas first, then key
+ apply_sqlcipher_pragmas(cursor, creation_mode=True)
set_sqlcipher_key(cursor, password)
+ apply_performance_pragmas(cursor, username)
- # Verify connection works
+ # Verify connection works AFTER setting everything up
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
@@ -550,11 +549,12 @@ class DatabaseManager:
)
cursor = conn.cursor()
- # Use centralized SQLCipher setup
+ # Use same settings as creation: pragmas first, then key
+ apply_sqlcipher_pragmas(cursor, creation_mode=True)
set_sqlcipher_key(cursor, password)
- apply_sqlcipher_pragmas(cursor, creation_mode=False)
+ apply_performance_pragmas(cursor, username)
- # Verify connection works
+ # Verify connection works AFTER applying all settings
if not verify_sqlcipher_connection(cursor):
raise ValueError("Failed to verify database key")
except Exception as e:
diff --git a/src/local_deep_research/database/sqlcipher_utils.py b/src/local_deep_research/database/sqlcipher_utils.py
index a26d3afb3c..792ae6baf1 100644
--- a/src/local_deep_research/database/sqlcipher_utils.py
+++ b/src/local_deep_research/database/sqlcipher_utils.py
@@ -136,11 +136,12 @@ def apply_sqlcipher_pragmas(
# Get settings (from database if available, otherwise defaults)
settings = get_sqlcipher_settings(username)
- # Core settings needed for both creation and access
+ # Core settings needed for both creation and access - apply in correct order
cursor_or_conn.execute(f"PRAGMA cipher_page_size = {settings['page_size']}")
cursor_or_conn.execute(
f"PRAGMA cipher_hmac_algorithm = {settings['hmac_algorithm']}"
)
+ cursor_or_conn.execute(f"PRAGMA kdf_iter = {settings['kdf_iterations']}")
if creation_mode:
# Additional settings only needed during creation
@@ -227,19 +228,15 @@ def create_sqlcipher_connection(db_path: str, password: str) -> Any:
conn = sqlcipher3.connect(str(db_path))
cursor = conn.cursor()
- # Set encryption key
+ # Apply settings in same order as creation: pragmas first, then key
+ apply_sqlcipher_pragmas(cursor, creation_mode=True)
set_sqlcipher_key(cursor, password)
+ apply_performance_pragmas(cursor)
- # Apply SQLCipher settings
- apply_sqlcipher_pragmas(cursor, creation_mode=False)
-
- # Verify connection
+ # Verify connection AFTER applying all settings
if not verify_sqlcipher_connection(cursor):
conn.close()
raise ValueError("Failed to establish encrypted database connection")
- # Apply performance settings
- apply_performance_pragmas(cursor)
-
cursor.close()
return conn