From 7d7e4efc78d5c76c49960b911cb4021d2981fe89 Mon Sep 17 00:00:00 2001 From: Russ Allbery Date: Thu, 30 May 2024 17:32:11 -0700 Subject: [PATCH] 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. --- changelog.d/20240530_173040_rra_DM_44606.md | 3 +++ src/safir/dependencies/db_session.py | 29 +++++---------------- 2 files changed, 9 insertions(+), 23 deletions(-) create mode 100644 changelog.d/20240530_173040_rra_DM_44606.md diff --git a/changelog.d/20240530_173040_rra_DM_44606.md b/changelog.d/20240530_173040_rra_DM_44606.md new file mode 100644 index 00000000..d937a79c --- /dev/null +++ b/changelog.d/20240530_173040_rra_DM_44606.md @@ -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. diff --git a/src/safir/dependencies/db_session.py b/src/safir/dependencies/db_session.py index 457e4fc7..845ef795 100644 --- a/src/safir/dependencies/db_session.py +++ b/src/safir/dependencies/db_session.py @@ -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]: @@ -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()