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.

7.4 KB
Raw Blame
#!/usr/bin/env python3
"""
Pre-commit hook to check for usage of deprecated database connection methods.
Ensures code uses per-user database connections instead of the deprecated shared database.
"""

import sys
import re
import os
from pathlib import Path


# Only these production modules own the raw-session or legacy-DB helpers.
# Match their complete repository path, never a substring of another name.
_SOURCE_EXEMPTIONS = (
    ("src", "local_deep_research", "web", "models", "database.py"),
    ("src", "local_deep_research", "database", "session_context.py"),
    ("src", "local_deep_research", "database", "encrypted_db.py"),
    ("src", "local_deep_research", "utilities", "db_utils.py"),
)
# A path is exempt when one of its directories has one of these exact names
# or is a test tree whose name ends in ``_tests`` (``release_tests``,
# ``ui_tests``). A directory name that merely contains "tests" (``contests``,
# ``tests_util``) stays checked.
_EXEMPT_DIRS = frozenset({"migrations", "tests", ".pre-commit-hooks"})
_TEST_TREE_SUFFIX = "_tests"


def _committed_parts(filepath):
    """Return the checkout-relative components of ``filepath``, or None.

    Pre-commit passes each path relative to the checkout, exactly as
    committed. The exemption is decided on that path and never on a symlink
    target, because a link under ``src/`` that points into ``tests/`` is still
    production code. None means the path cannot earn an exemption, so the
    file is checked: it is empty, lies outside the checkout, contains ``..``,
    or is (or passes through) a symlink.
    """
    cwd = Path.cwd()
    path = Path(filepath)
    if path.anchor:
        try:
            path = path.relative_to(cwd)
        except ValueError:
            return None
    parts = path.parts
    if not parts or ".." in parts:
        return None
    for depth in range(1, len(parts) + 1):
        if cwd.joinpath(*parts[:depth]).is_symlink():
            return None
    return parts


def _is_exempt_dir(name):
    return name in _EXEMPT_DIRS or name.endswith(_TEST_TREE_SUFFIX)


def _is_exempt_path(filepath):
    """Exempt test, migration and hook trees, ``test_*`` files, and helpers.

    The helpers are the exact module paths in ``_SOURCE_EXEMPTIONS``.
    """
    parts = _committed_parts(filepath)
    if parts is None:
        return False
    if parts[-1].startswith("test_"):
        return True
    if any(_is_exempt_dir(name) for name in parts[:-1]):
        return True
    return any(parts == source_path for source_path in _SOURCE_EXEMPTIONS)


def check_file(filepath):
    """Check a single file for deprecated database usage."""
    issues = []

    with open(filepath, "r", encoding="utf-8") as f:
        content = f.read()
        lines = content.split("\n")

    # Pattern to detect get_db_connection usage
    db_connection_pattern = re.compile(r"\bget_db_connection\s*\(")

    # Pattern to detect direct imports of get_db_connection
    import_pattern = re.compile(
        r"from\s+[\w.]+\s+import\s+.*\bget_db_connection\b"
    )

    # Pattern to detect db_manager.get_session() — returns a raw QueuePool
    # session that must be closed by the caller.  utilities/db_utils.py, which
    # is exempt by path, keeps such sessions in a per-thread cache that the
    # worker's cleanup_current_thread() closes when the request or background
    # task finishes.  Any other call site must close the session itself and
    # carry a raw-session noqa marker; everything else should use the
    # get_user_db_session() context manager instead.
    raw_session_pattern = re.compile(r"\bdb_manager\.get_session\s*\(")

    # 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()"
            )

        if import_pattern.search(line):
            issues.append(
                f"{filepath}:{i}: Import of deprecated get_db_connection"
            )

        if (
            raw_session_pattern.search(line)
            and "# noqa: raw-session" not in line
        ):
            issues.append(
                f"{filepath}:{i}: Direct db_manager.get_session() call — "
                "returns an unmanaged QueuePool session that leaks FDs if not closed. "
                "Use 'with get_user_db_session(username) as session:' instead, "
                "or add '# noqa: raw-session' if this is intentional (e.g. the session is closed in a finally block)"
            )

    # (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.
    #
    # 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 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"
            )

    return issues


def main():
    """Main function to check all provided files."""
    # Allow unencrypted databases when the hook runs standalone (dev
    # machines may still have a legacy unencrypted DB). Kept out of module
    # scope so importing this module — as tests/ does — cannot mutate the
    # process environment.
    os.environ["LDR_ALLOW_UNENCRYPTED"] = "true"

    if len(sys.argv) < 2:
        print("No files to check")
        return 0

    all_issues = []

    for filepath in sys.argv[1:]:
        if _is_exempt_path(filepath):
            continue

        issues = check_file(filepath)
        all_issues.extend(issues)

    if all_issues:
        print("❌ Deprecated or unsafe database access detected!\n")
        print("The shared database (get_db_connection) is deprecated.")
        print(
            "Direct db_manager.get_session() leaks QueuePool connections (FDs)."
        )
        print(
            "Please use get_user_db_session(username) for per-user database access.\n"
        )
        print("Issues found:")
        for issue in all_issues:
            print(f"  - {issue}")
        print("\nExample fix:")
        print("  # Old (deprecated):")
        print("  conn = get_db_connection()")
        print("  cursor = conn.cursor()")
        print("  # ... SQL query execution ...")
        print()
        print("  # New (correct), in a FastAPI route:")
        print("  def handler(username: Annotated[str, Depends(require_auth)]):")
        print("      with get_user_db_session(username) as db_session:")
        print("          results = db_session.query(Model).filter(...).all()")
        return 1

    return 0


if __name__ == "__main__":
    sys.exit(main())