Skip to content

Commit

Permalink
Drop DatabaseSessionDependency.override_engine
Browse files Browse the repository at this point in the history
This was used for Gafaelfawr to share a database engine across all
tests for speed, but this technique breaks with current versions of
pytest-asyncio and is no longer used or safe to use.
  • Loading branch information
rra committed Jun 6, 2024
1 parent 9ce8b39 commit 7d7e4ef
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
3 changes: 3 additions & 0 deletions changelog.d/20240530_173040_rra_DM_44606.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Backwards-incompatible changes

- Drop `DatabaseSessionDependency.override_engine`. This was used for Gafaelfawr to share a database engine across all tests for speed, but this technique breaks with current versions of pytest-asyncio and is no longer used or safe to use.
29 changes: 6 additions & 23 deletions src/safir/dependencies/db_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class DatabaseSessionDependency:

def __init__(self) -> None:
self._engine: AsyncEngine | None = None
self._override_engine: AsyncEngine | None = None
self._session: async_scoped_session | None = None

async def __call__(self) -> AsyncIterator[async_scoped_session]:
Expand Down Expand Up @@ -85,28 +84,12 @@ async def initialize(
If specified, sets a non-default isolation level for the database
engine.
"""
if self._override_engine:
self._session = await create_async_session(self._override_engine)
else:
self._engine = create_database_engine(
url, password, isolation_level=isolation_level
)
self._session = await create_async_session(self._engine)

def override_engine(self, engine: AsyncEngine) -> None:
"""Force the dependency to use the provided engine.
Intended for testing, this allows the test suite to configure a single
database engine and share it across all of the tests, benefiting from
connection pooling for a minor test speed-up. (This is not
significant enough to bother with except for an extensive test suite.)
Parameters
----------
engine
Database engine to use for all sessions.
"""
self._override_engine = engine
if self._engine:
await self._engine.dispose()
self._engine = create_database_engine(
url, password, isolation_level=isolation_level
)
self._session = await create_async_session(self._engine)


db_session_dependency = DatabaseSessionDependency()
Expand Down

0 comments on commit 7d7e4ef

Please sign in to comment.