-
Notifications
You must be signed in to change notification settings - Fork 615
[CHORE]: Consolidate redundant get_db definitions to single source #2147
Description
Summary
There are 9 separate definitions of get_db() across the codebase. These should be consolidated to a single canonical definition in mcpgateway/db.py, with all other modules importing from there.
Current State
| File | Line | Notes |
|---|---|---|
mcpgateway/db.py |
5267 | Canonical definition |
mcpgateway/main.py |
1661 | Should import from db.py |
mcpgateway/auth.py |
88 | Should import from db.py |
mcpgateway/routers/auth.py |
35 | Should import from db.py |
mcpgateway/routers/email_auth.py |
60 | Should import from db.py |
mcpgateway/routers/rbac.py |
40 | Should import from db.py |
mcpgateway/routers/observability.py |
30 | Should import from db.py |
mcpgateway/middleware/rbac.py |
37 | Should import from db.py |
mcpgateway/transports/streamablehttp_transport.py |
387 | Async variant - evaluate if needed |
Problems Caused
-
Test complexity: Dependency overrides only affect one definition at a time, requiring tests to override multiple
get_dbfunctions. See workarounds in:tests/e2e/test_oauth_protected_resource.py- overrides bothmain.get_dbanddb.get_dbtests/unit/mcpgateway/test_well_known.py- same pattern
-
Maintenance burden: Changes to database session handling must be replicated across all definitions.
-
Inconsistent behavior: Different modules may have subtly different session handling.
Proposed Solution
-
Analyze variant implementations: Each
get_db()definition must be analyzed to determine if it can be replaced by the canonical implementation. Check for differences in:- Session configuration (autocommit, autoflush, expire_on_commit)
- Error handling and rollback behavior
- Connection pooling or scoping differences
- Sync vs async requirements
-
Consolidate or rename:
- If a variant can be replaced by the canonical definition, replace it with an import from
mcpgateway.db - If a variant has legitimate differences that cannot be reconciled, rename it to reflect its purpose (e.g.,
get_async_db,get_scoped_db) to avoid confusion
- If a variant can be replaced by the canonical definition, replace it with an import from
-
Update tests to only need a single override where possible
Future Direction
This consolidation is a stepping stone toward using Python context managers (with statements) for database connection management. Having a single get_db source ensures all logic uses a singleton-like database connection pattern, which will make the transition to context managers cleaner and more consistent.
Acceptance Criteria
- All
get_db()variants analyzed and documented - Canonical
get_db()inmcpgateway/db.pyused wherever possible - Variants with legitimate differences renamed to reflect their purpose
- Tests simplified to override single
get_dbwhere applicable - All existing tests pass