[PR #432] Use shared multi-tenant get_db dependency across content-strategy endpoints #737

Open
opened 2026-03-13 21:06:36 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/AJaySi/ALwrity/pull/432
Author: @AJaySi
Created: 3/12/2026
Status: 🔄 Open

Base: mainHead: codex/refactor-db-access-in-content-strategy


📝 Commits (1)

  • a9252ed Use shared multi-tenant DB dependency in strategy endpoints

📊 Changes

6 files changed (+168 additions, -47 deletions)

View changed files

📝 backend/api/content_planning/api/content_strategy/endpoints/ai_generation_endpoints.py (+7 -9)
📝 backend/api/content_planning/api/content_strategy/endpoints/analytics_endpoints.py (+2 -10)
📝 backend/api/content_planning/api/content_strategy/endpoints/autofill_endpoints.py (+7 -9)
📝 backend/api/content_planning/api/content_strategy/endpoints/streaming_endpoints.py (+1 -9)
📝 backend/api/content_planning/api/content_strategy/endpoints/utility_endpoints.py (+2 -10)
backend/tests/api/content_planning/content_strategy/endpoints/test_shared_get_db_dependency.py (+149 -0)

📄 Description

Motivation

  • Endpoint-local get_db() helpers called get_db_session() without an authenticated user, which returns None in multi-tenant mode and can break DB access.
  • Centralized, tenant-aware DB session lifecycle is provided by services.database.get_db, so endpoints should rely on that shared dependency.
  • Routes that access the DB must run in an authenticated user context so the shared get_db can resolve a tenant-specific engine/session.

Description

  • Removed local get_db() wrappers that used get_db_session() from the following modules and switched them to import get_db from services.database: utility_endpoints.py, analytics_endpoints.py, autofill_endpoints.py, streaming_endpoints.py, and ai_generation_endpoints.py.
  • Added Depends(get_current_user) to DB-backed routes that previously lacked explicit authentication parameters so tenant context is available to get_db (notably in autofill_endpoints and core AI generation routes).
  • Eliminated local db.close() handling in endpoint-local wrappers by relying on the shared get_db dependency to manage session lifecycle.
  • Added regression tests backend/tests/api/content_planning/content_strategy/endpoints/test_shared_get_db_dependency.py that override get_db and auth dependencies and assert that injected/fake services receive a non-null DB session for one representative endpoint per modified module.

Testing

  • Ran the new test file with pytest -q backend/tests/api/content_planning/content_strategy/endpoints/test_shared_get_db_dependency.py and verified all tests passed: 5 passed.
  • Tests instantiate a FastAPI TestClient, override get_db with a FakeDBSession, override auth dependencies, and monkeypatch endpoint services to assert db is non-null and responses return a successful status.

Codex Task


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/AJaySi/ALwrity/pull/432 **Author:** [@AJaySi](https://github.com/AJaySi) **Created:** 3/12/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `codex/refactor-db-access-in-content-strategy` --- ### 📝 Commits (1) - [`a9252ed`](https://github.com/AJaySi/ALwrity/commit/a9252edc18605f1544773b5f0964da18d7eb530f) Use shared multi-tenant DB dependency in strategy endpoints ### 📊 Changes **6 files changed** (+168 additions, -47 deletions) <details> <summary>View changed files</summary> 📝 `backend/api/content_planning/api/content_strategy/endpoints/ai_generation_endpoints.py` (+7 -9) 📝 `backend/api/content_planning/api/content_strategy/endpoints/analytics_endpoints.py` (+2 -10) 📝 `backend/api/content_planning/api/content_strategy/endpoints/autofill_endpoints.py` (+7 -9) 📝 `backend/api/content_planning/api/content_strategy/endpoints/streaming_endpoints.py` (+1 -9) 📝 `backend/api/content_planning/api/content_strategy/endpoints/utility_endpoints.py` (+2 -10) ➕ `backend/tests/api/content_planning/content_strategy/endpoints/test_shared_get_db_dependency.py` (+149 -0) </details> ### 📄 Description ### Motivation - Endpoint-local `get_db()` helpers called `get_db_session()` without an authenticated user, which returns `None` in multi-tenant mode and can break DB access. - Centralized, tenant-aware DB session lifecycle is provided by `services.database.get_db`, so endpoints should rely on that shared dependency. - Routes that access the DB must run in an authenticated user context so the shared `get_db` can resolve a tenant-specific engine/session. ### Description - Removed local `get_db()` wrappers that used `get_db_session()` from the following modules and switched them to import `get_db` from `services.database`: `utility_endpoints.py`, `analytics_endpoints.py`, `autofill_endpoints.py`, `streaming_endpoints.py`, and `ai_generation_endpoints.py`. - Added `Depends(get_current_user)` to DB-backed routes that previously lacked explicit authentication parameters so tenant context is available to `get_db` (notably in `autofill_endpoints` and core AI generation routes). - Eliminated local `db.close()` handling in endpoint-local wrappers by relying on the shared `get_db` dependency to manage session lifecycle. - Added regression tests `backend/tests/api/content_planning/content_strategy/endpoints/test_shared_get_db_dependency.py` that override `get_db` and auth dependencies and assert that injected/fake services receive a non-null DB session for one representative endpoint per modified module. ### Testing - Ran the new test file with `pytest -q backend/tests/api/content_planning/content_strategy/endpoints/test_shared_get_db_dependency.py` and verified all tests passed: `5 passed`. - Tests instantiate a `FastAPI` `TestClient`, override `get_db` with a `FakeDBSession`, override auth dependencies, and monkeypatch endpoint services to assert `db` is non-null and responses return a successful status. ------ [Codex Task](https://chatgpt.com/codex/tasks/task_e_69b287fabbec832880a09b2e414e1040) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ALwrity#737
No description provided.