From 98cf9e0e0b871cd75d204067a5154ccc9f0e86e7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Feb 2026 15:37:23 +0000 Subject: [PATCH] refactor: consolidate get_db into single module Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com> --- app/api/common.py | 10 ---------- app/api/files.py | 2 +- app/api/logs.py | 2 +- app/views/base.py | 13 +------------ tests/conftest.py | 9 +++------ 5 files changed, 6 insertions(+), 30 deletions(-) diff --git a/app/api/common.py b/app/api/common.py index 07e3283e..8cbb0535 100644 --- a/app/api/common.py +++ b/app/api/common.py @@ -9,21 +9,11 @@ from pathlib import Path from fastapi import HTTPException, status from app.config import settings -from app.database import SessionLocal # Set up logging logger = logging.getLogger(__name__) -def get_db(): - """Database dependency injection for routes""" - db = SessionLocal() - try: - yield db - finally: - db.close() - - def resolve_file_path(file_path: str, subfolder: str = None) -> str: """ Resolves a file path to an absolute path with path traversal protection. diff --git a/app/api/files.py b/app/api/files.py index 47d2b714..58e0d834 100644 --- a/app/api/files.py +++ b/app/api/files.py @@ -12,9 +12,9 @@ from fastapi import APIRouter, Depends, File, HTTPException, Query, Request, Upl from sqlalchemy import asc, desc, or_ from sqlalchemy.orm import Session -from app.api.common import get_db from app.auth import require_login from app.config import settings +from app.database import get_db from app.models import FileRecord, ProcessingLog from app.tasks.convert_to_pdf import convert_to_pdf from app.tasks.process_document import process_document diff --git a/app/api/logs.py b/app/api/logs.py index 6b2c4cdb..7ef0c434 100644 --- a/app/api/logs.py +++ b/app/api/logs.py @@ -9,8 +9,8 @@ from fastapi import APIRouter, Depends, HTTPException, Query, Request from sqlalchemy import desc from sqlalchemy.orm import Session -from app.api.common import get_db from app.auth import require_login +from app.database import get_db from app.models import FileRecord, ProcessingLog # Set up logging diff --git a/app/views/base.py b/app/views/base.py index 087e88b4..45ffa7b9 100644 --- a/app/views/base.py +++ b/app/views/base.py @@ -11,7 +11,7 @@ from sqlalchemy.orm import Session # noqa: F401 from app.auth import require_login # noqa: F401 from app.config import settings -from app.database import SessionLocal +from app.database import get_db # noqa: F401 # Set up Jinja2 templates templates_dir = Path(__file__).parent.parent.parent / "frontend" / "templates" @@ -39,14 +39,3 @@ templates.TemplateResponse = template_response_with_version # Set up logging logger = logging.getLogger(__name__) - - -def get_db(): - """ - Dependency to get a database session. - """ - db = SessionLocal() - try: - yield db - finally: - db.close() diff --git a/tests/conftest.py b/tests/conftest.py index 9060c9de..3dfdcb93 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -63,9 +63,8 @@ def db_session(): def client(db_session) -> TestClient: """Create a test client with a fresh database.""" - # Import all the different get_db functions used across the app - from app.api.common import get_db as api_get_db - from app.views.base import get_db as views_get_db + # Import the canonical get_db function + from app.database import get_db # Override the get_db dependency to use our test database def override_get_db(): @@ -74,10 +73,8 @@ def client(db_session) -> TestClient: finally: pass - # Override all variants of get_db + # Override the single canonical get_db dependency fastapi_app.dependency_overrides[get_db] = override_get_db - fastapi_app.dependency_overrides[api_get_db] = override_get_db - fastapi_app.dependency_overrides[views_get_db] = override_get_db # Use base_url to satisfy TrustedHostMiddleware with TestClient(fastapi_app, base_url="http://localhost") as test_client: