Skip to content

fix: SQLAlchemyAdapter degrades gracefully when async DB driver is missing (1.2.1)#6

Merged
svalench merged 1 commit into
masterfrom
hotfix/missing-orm-subpackage
Apr 25, 2026
Merged

fix: SQLAlchemyAdapter degrades gracefully when async DB driver is missing (1.2.1)#6
svalench merged 1 commit into
masterfrom
hotfix/missing-orm-subpackage

Conversation

@svalench
Copy link
Copy Markdown
Owner

Summary

Hotfix for v1.2.0 regression: from fastapi_viewsets import BaseViewset (or any code path that initialises the default SQLAlchemyAdapter) crashes at startup with ModuleNotFoundError: 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_env started always passing an explicit async_database_url (e.g. sqlite+aiosqlite:///...) to SQLAlchemyAdapter. The adapter's existing try/except around create_async_engine() only covered the auto-converted-URL branch — the explicit-URL branch raised ModuleNotFoundError directly from __init__.

Stack trace from a user with no aiosqlite installed:

File "fastapi_viewsets/orm/sqlalchemy_adapter.py", line 74, in __init__
    self.async_engine = create_async_engine(async_database_url, echo=False)
...
File "sqlalchemy/dialects/sqlite/aiosqlite.py", line 449, in import_dbapi
    __import__("aiosqlite"), __import__("sqlite3")
ModuleNotFoundError: No module named 'aiosqlite'

Fix

  • Both async-engine creation branches are now best-effort. On any failure (missing driver, no async dialect for the URL, etc.) async_engine and AsyncSessionLocal fall back to None, and get_async_session() raises a helpful RuntimeError lazily with installation hints. Sync usage works without any extra dependency.
  • Bumped version to 1.2.1.
  • Switched pyproject.toml packaging to [tool.setuptools.packages.find] so future subpackages are picked up automatically.
  • Added regression tests for both URL paths (explicit async_database_url and auto-converted URL) when the async driver import fails.

Verification

  • Repro: created adapter without aiosqlite → adapter constructs cleanly, sync session works, get_async_session() raises a friendly RuntimeError.
  • Built wheel and confirmed fastapi_viewsets/orm/* is included.
  • Test suite: 208 passed, 39 skipped (skips are pre-existing tortoise/peewee env-dependent tests).

Release notes

See RELEASE_1.2.1.md and updated RELEASE_NOTES.md.

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
Copy link
Copy Markdown

codecov Bot commented Apr 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@svalench svalench merged commit f649be1 into master Apr 25, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant