fix: SQLAlchemyAdapter degrades gracefully when async DB driver is missing (1.2.1)#6
Merged
Merged
Conversation
Regression in 1.2.0: ORMFactory.get_adapter_from_env now always passes an explicit async_database_url to SQLAlchemyAdapter. The adapter's try/except guard only covered the auto-converted-URL branch, so any sync-only setup without aiosqlite/asyncpg/aiomysql crashed at startup with ModuleNotFoundError. Make the async engine creation best-effort on both branches: on import failure async_engine and AsyncSessionLocal fall back to None and get_async_session() raises a helpful RuntimeError lazily. Also switch packaging to [tool.setuptools.packages.find] so future subpackages are picked up automatically. Adds regression tests covering both URL paths.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Summary
Hotfix for v1.2.0 regression:
from fastapi_viewsets import BaseViewset(or any code path that initialises the defaultSQLAlchemyAdapter) crashes at startup withModuleNotFoundError: No module named 'aiosqlite'when the async DB-API driver is not installed — even for purely synchronous setups that never use async sessions.Root cause
In #3,
ORMFactory.get_adapter_from_envstarted always passing an explicitasync_database_url(e.g.sqlite+aiosqlite:///...) toSQLAlchemyAdapter. The adapter's existingtry/exceptaroundcreate_async_engine()only covered the auto-converted-URL branch — the explicit-URL branch raisedModuleNotFoundErrordirectly from__init__.Stack trace from a user with no
aiosqliteinstalled:Fix
async_engineandAsyncSessionLocalfall back toNone, andget_async_session()raises a helpfulRuntimeErrorlazily with installation hints. Sync usage works without any extra dependency.pyproject.tomlpackaging to[tool.setuptools.packages.find]so future subpackages are picked up automatically.async_database_urland auto-converted URL) when the async driver import fails.Verification
aiosqlite→ adapter constructs cleanly, sync session works,get_async_session()raises a friendlyRuntimeError.fastapi_viewsets/orm/*is included.Release notes
See
RELEASE_1.2.1.mdand updatedRELEASE_NOTES.md.