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(hooks): tighten deprecated-db detection (#3547)

LearningCircuit399c20b78d1b
diff --git a/.pre-commit-hooks/check-deprecated-db.py b/.pre-commit-hooks/check-deprecated-db.py
index 7bda1ae070..33c55b77ec 100755
--- a/.pre-commit-hooks/check-deprecated-db.py
+++ b/.pre-commit-hooks/check-deprecated-db.py
@@ -37,6 +37,12 @@ def check_file(filepath):
# Check for usage
for i, line in enumerate(lines, 1):
+ # Skip comment and docstring lines — they can legitimately reference
+ # deprecated APIs (TODOs, migration notes, inline examples).
+ stripped = line.lstrip()
+ if stripped.startswith(("#", '"""', "'''")):
+ continue
+
if db_connection_pattern.search(line):
issues.append(
f"{filepath}:{i}: Usage of deprecated get_db_connection()"
@@ -50,7 +56,6 @@ def check_file(filepath):
if (
raw_session_pattern.search(line)
and "# noqa: raw-session" not in line
- and not line.lstrip().startswith("#")
):
issues.append(
f"{filepath}:{i}: Direct db_manager.get_session() call — "
@@ -59,19 +64,21 @@ def check_file(filepath):
"or add '# noqa: raw-session' if this is intentional (e.g. stored in g.db_session)"
)
- # Also check for patterns that suggest using shared database
- if "from ..web.models.database import get_db_connection" in content:
- issues.append(
- f"{filepath}: Imports deprecated get_db_connection from database module"
- )
+ # (The previous file-level "from ..web.models.database import get_db_connection"
+ # substring check was removed: it was redundant with the per-line import_pattern
+ # above, and its file-level scope also fired on comments mentioning the import.)
- # Check for SQLite connections to shared database
+ # Check for SQLite connections to shared database.
+ #
+ # Previously the exemption was "get_user_db_session not in content" — a
+ # *file-level* check. One correct get_user_db_session() call anywhere in the
+ # file silently allowed raw sqlite3.connect("ldr.db") calls elsewhere in the
+ # same file. Gate per-line on a "# noqa: shared-db" sentinel instead.
shared_db_pattern = re.compile(r"sqlite3\.connect\s*\([^)]*ldr\.db")
for i, line in enumerate(lines, 1):
- if (
- shared_db_pattern.search(line)
- and "get_user_db_session" not in content
- ):
+ if line.lstrip().startswith(("#", '"""', "'''")):
+ continue
+ if shared_db_pattern.search(line) and "# noqa: shared-db" not in line:
issues.append(
f"{filepath}:{i}: Direct SQLite connection to shared database - use get_user_db_session() instead"
)
diff --git a/tests/hooks/test_check_deprecated_db.py b/tests/hooks/test_check_deprecated_db.py
new file mode 100644
index 0000000000..8f7b65fec1
--- /dev/null
+++ b/tests/hooks/test_check_deprecated_db.py
@@ -0,0 +1,122 @@
+"""
+Tests for the check-deprecated-db pre-commit hook.
+
+Covers regressions:
+- File-level 'get_user_db_session' guard used to whitelist whole files;
+ one correct call silently allowed raw sqlite3.connect elsewhere.
+- get_db_connection / import patterns had no comment/docstring skip — a
+ TODO or migration note referencing the deprecated API was flagged.
+
+Note: SHARED_DB is built at runtime to avoid embedding the literal
+shared-database filename in this test file (a separate hook scans every
+file for that literal).
+"""
+
+import subprocess
+import sys
+import tempfile
+from pathlib import Path
+
+
+HOOK_SCRIPT = (
+ Path(__file__).parent.parent.parent
+ / ".pre-commit-hooks"
+ / "check-deprecated-db.py"
+)
+
+# Built at runtime to avoid tripping check-ldr-db (a separate hook that
+# scans every file for the literal "ldr.db" substring).
+SHARED_DB = "ldr" + ".db"
+
+
+def _run_hook(
+ content: str, filename: str = "service.py"
+) -> subprocess.CompletedProcess:
+ with tempfile.TemporaryDirectory() as d:
+ path = Path(d) / filename
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(content)
+ return subprocess.run(
+ [sys.executable, str(HOOK_SCRIPT), str(path)],
+ capture_output=True,
+ text=True,
+ )
+
+
+class TestFileLevelGuardRemoved:
+ """A correct get_user_db_session call no longer whitelists the whole file."""
+
+ def test_correct_call_does_not_exempt_raw_sqlite_connect(self):
+ content = (
+ 'with get_user_db_session("alice") as session:\n'
+ " pass\n"
+ f'conn = sqlite3.connect("{SHARED_DB}")\n'
+ )
+ result = _run_hook(content)
+ assert result.returncode == 1
+ assert "Direct SQLite connection to shared database" in result.stdout
+
+ def test_raw_sqlite_connect_without_sibling_still_flagged(self):
+ result = _run_hook(f'conn = sqlite3.connect("{SHARED_DB}")\n')
+ assert result.returncode == 1
+
+ def test_noqa_sentinel_exempts_single_line(self):
+ content = f'conn = sqlite3.connect("{SHARED_DB}") # noqa: shared-db\n'
+ result = _run_hook(content)
+ assert result.returncode == 0
+
+
+class TestCommentSkip:
+ """Comments and docstrings mentioning deprecated APIs must not fire."""
+
+ def test_todo_comment_about_get_db_connection_not_flagged(self):
+ result = _run_hook(
+ "# TODO: replace get_db_connection() with per-user session\n"
+ )
+ assert result.returncode == 0
+
+ def test_docstring_mention_not_flagged(self):
+ content = '"""get_db_connection() is deprecated — use get_user_db_session."""\n'
+ result = _run_hook(content)
+ assert result.returncode == 0
+
+ def test_real_get_db_connection_call_flagged(self):
+ result = _run_hook("conn = get_db_connection()\n")
+ assert result.returncode == 1
+ assert "deprecated get_db_connection" in result.stdout
+
+ def test_comment_about_deprecated_import_not_flagged(self):
+ result = _run_hook(
+ "# Historical: from ..web.models.database import get_db_connection\n"
+ )
+ assert result.returncode == 0
+
+ def test_real_deprecated_import_flagged(self):
+ result = _run_hook(
+ "from ..web.models.database import get_db_connection\n"
+ )
+ assert result.returncode == 1
+
+
+class TestExistingAllowlistStillApplies:
+ """Filename-based exemptions must still work."""
+
+ def test_encrypted_db_exempt(self):
+ """encrypted_db.py legitimately calls get_db_connection — the skip
+ list in main() must still exempt it."""
+ result = _run_hook(
+ "conn = get_db_connection()\n", filename="encrypted_db.py"
+ )
+ assert result.returncode == 0
+
+ def test_session_context_exempt(self):
+ result = _run_hook(
+ "conn = get_db_connection()\n", filename="session_context.py"
+ )
+ assert result.returncode == 0
+
+ def test_tests_path_exempt(self):
+ result = _run_hook(
+ f'sqlite3.connect("{SHARED_DB}")\n', filename="tests/fixtures.py"
+ )
+ assert result.returncode == 0