fix(api): auto-correct DATABASE_URL driver to psycopg v3 when legacy/unbound - #71
Merged
Merged
Conversation
…unbound SQLAlchemy lazily imports the dialect module at engine-creation time. A bare `postgresql://...` URL (no +<driver> hint) makes it default to the legacy `psycopg2` driver, but the workspace only ships `psycopg[binary]>=3.3.4` (v3). This crashes the lifespan schema-drift guard (and every subsequent `get_engine()` call) with `ModuleNotFoundError: No module named psycopg2`. Changes: - database.py: new `_normalise_database_url()` helper that rewrites `postgresql://`, `postgres://` and `postgresql+psycopg2://` to `postgresql+psycopg://`. Other dialects (asyncpg, pg8000, etc.) are left untouched. Idempotent on already-correct URLs. - get_engine() now passes URL through the helper before create_engine(). - alembic/env.py: threads the same helper so `uv run alembic upgrade head` also resolves to the right driver. - .env.example, apps/api/.env.example: sync example URL to the explicit `+psycopg` form so future contributors don hit the same hole. - apps/api/tests/conftest.py: test fixture URL synced. - apps/api/tests/test_database.py: new regression test (7 parametrized cases) locking the helper contract. Signed-off-by: RoddyGitHub <roddy@users.noreply.github.com>
…ault -> .python-version Signed-off-by: RoddyGitHub <roddy@users.noreply.github.com>
…est.yml Signed-off-by: RoddyGitHub <roddy@users.noreply.github.com>
Signed-off-by: RoddyGitHub <roddy@users.noreply.github.com>
Signed-off-by: RoddyGitHub <roddy@users.noreply.github.com>
…ion-test Signed-off-by: RoddyGitHub <roddy@users.noreply.github.com>
…igration-test Signed-off-by: RoddyGitHub <roddy@users.noreply.github.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.
Problem
Every uvicorn restart crashes the lifespan with:
because the default
.env/.env.exampleuseDATABASE_URL=postgresql://...(bare, no driver hint), and SQLAlchemy defaults to the legacypsycopg2driver — but the workspace only shipspsycopg[binary]>=3.3.4(v3).The same crash fires from
uv run alembic upgrade headbecausealembic/env.pyreadget_settings().database_urldirectly without the driver-rewrite helper.Fix
New
_normalise_database_url()helper indatabase.py(idempotent, private):postgresql://...(no hint) → rewrite topostgresql+psycopg://postgres://...(shorthand, Heroku etc.) → rewritepostgresql+psycopg2://...(explicit legacy) → rewrite,asyncpg, pg8000) left untouchedThreaded through:
get_engine()— in-process fixalembic/env.py— CLI fix so migrations resolve correctlyExample files synced to the explicit
+psycopgform. New regression test (test_database.py) locks the 6 URL shapes.Validation