ENG-3566: Refactor engine creation to use SQLAlchemy creator pattern#8148
Draft
ENG-3566: Refactor engine creation to use SQLAlchemy creator pattern#8148
Conversation
Switch all 6 database engines from URI-based connection creation to the creator pattern. The creator callable is invoked by the pool on every new connection, resolving credentials at connect time rather than engine construction time. This is the structural seam that enables dynamic credential rotation via AWS Secrets Manager in follow-up work. Changes: - Add raw_password/raw_readonly_password properties to DatabaseSettings for unescaped passwords (needed by psycopg2/asyncpg direct connect) - New engine_creators.py with make_sync_creator/make_async_creator factories and credential helpers - Add creator parameter to get_db_engine() in session.py - Switch ctl_session.py async/sync engines to creator pattern - Switch session_management.py sync engines to creator pattern - Switch tasks/__init__.py task engine to creator pattern - Update design doc to remove lazy factory requirement Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #8148 +/- ##
==========================================
- Coverage 85.23% 85.21% -0.02%
==========================================
Files 638 639 +1
Lines 42011 42070 +59
Branches 4937 4945 +8
==========================================
+ Hits 35807 35852 +45
- Misses 5096 5103 +7
- Partials 1108 1115 +7 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
25 tests covering credential helpers, raw_password round-trip, asyncpg param conversion, SSL context building, and end-to-end sync/async engine creation via creator pattern. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of ENG-3566
Description Of Changes
Refactors all 6 database engines to use SQLAlchemy's
creatorpattern instead of baking credentials into connection URIs. Thecreatorcallable is invoked by the pool on every new connection, resolving credentials at connect time rather than engine construction time. This is the structural seam required for dynamic credential rotation via AWS Secrets Manager (design doc: #8016).Today the creators read from static config (
CONFIG.database). In follow-up work, the credential helpers will be swapped to callprovider.get_secret()— no further engine changes needed.Also updates the design doc to remove the lazy factory requirement, since the creator closure captures a provider reference (not credentials), making it independent of when the engine is constructed.
Code Changes
database_settings.py— Addraw_password/raw_readonly_passwordproperties that reversequote_plusescaping for directpsycopg2.connect()/asyncpg.connect()useengine_creators.py(new) —make_sync_creator/make_async_creatorfactories that capture per-engine config (keepalives, SSL) in closures while resolving credentials per-connection. Includes credential helpers and asyncpg param/SSL conversionsession.py— Add optionalcreatorparameter toget_db_engine(). When provided, uses dialect-only URL and skips connect_args (creator handles them)ctl_session.py— Switch all 3 engines (async, readonly async, sync) from URI-based to creator-based. SSL context and asyncpg param handling moved intoengine_creators.pysession_management.py— Switch API and readonly sync engines to usemake_sync_creatorwith keepalive connect_argstasks/__init__.py— Switch Celery task engine to usemake_sync_creatorwith keepalive connect_argsdynamic-database-credentials.md— Remove lazy factory requirement from design docSteps to Confirm
GET /health/databaseand confirm all pools are healthyAsyncAdapt_asyncpg_connection)Pre-Merge Checklist
CHANGELOG.mdupdated