Skip to content

Commit 9b9c2e6

Browse files
authored
feat(lifespan): remove lifespan from main configurations (#12)
* feat(lifespan): remove lifespan from main configurations * chore: updated test
1 parent 00de3f3 commit 9b9c2e6

File tree

11 files changed

+3
-126
lines changed

11 files changed

+3
-126
lines changed

sqlspec/adapters/aiosqlite/config.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,6 @@ async def create_connection(self) -> Connection:
7878
msg = f"Could not configure the Aiosqlite connection. Error: {e!s}"
7979
raise ImproperConfigurationError(msg) from e
8080

81-
@asynccontextmanager
82-
async def lifespan(self, *args: Any, **kwargs: Any) -> AsyncGenerator[None, None]:
83-
"""Manage the lifecycle of a database connection.
84-
85-
Yields:
86-
None
87-
88-
Raises:
89-
ImproperConfigurationError: If the connection could not be established.
90-
"""
91-
connection = await self.create_connection()
92-
try:
93-
yield
94-
finally:
95-
await connection.close()
96-
9781
@asynccontextmanager
9882
async def provide_connection(self, *args: Any, **kwargs: Any) -> AsyncGenerator[Connection, None]:
9983
"""Create and provide a database connection.

sqlspec/adapters/asyncmy/config.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,6 @@ async def provide_pool(self, *args: Any, **kwargs: Any) -> Pool:
161161
"""
162162
return await self.create_pool()
163163

164-
@asynccontextmanager
165-
async def lifespan(self, *args: Any, **kwargs: Any) -> AsyncGenerator[None, None]:
166-
"""Manage the lifecycle of a database connection pool.
167-
168-
Yields:
169-
None
170-
171-
Raises:
172-
ImproperConfigurationError: If the pool could not be established.
173-
"""
174-
pool = await self.create_pool()
175-
try:
176-
yield
177-
finally:
178-
pool.close()
179-
await pool.wait_closed()
180-
181164
@asynccontextmanager
182165
async def provide_connection(self, *args: Any, **kwargs: Any) -> AsyncGenerator[Connection, None]:
183166
"""Create and provide a database connection.

sqlspec/adapters/asyncpg/config.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,6 @@ async def create_pool(self) -> Pool:
123123
)
124124
return self.pool_instance
125125

126-
@asynccontextmanager
127-
async def lifespan(self, *args: Any, **kwargs: Any) -> AsyncGenerator[None, None]:
128-
db_pool = await self.create_pool()
129-
try:
130-
yield
131-
finally:
132-
db_pool.terminate()
133-
await db_pool.close()
134-
135126
def provide_pool(self, *args: Any, **kwargs: Any) -> Awaitable[Pool]:
136127
"""Create a pool instance.
137128

sqlspec/adapters/duckdb/config.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -184,22 +184,6 @@ def create_connection(self) -> DuckDBPyConnection:
184184
msg = f"Could not configure the DuckDB connection. Error: {e!s}"
185185
raise ImproperConfigurationError(msg) from e
186186

187-
@contextmanager
188-
def lifespan(self, *args: Any, **kwargs: Any) -> Generator[None, None, None]:
189-
"""Manage the lifecycle of a database connection.
190-
191-
Yields:
192-
None
193-
194-
Raises:
195-
ImproperConfigurationError: If the connection could not be established.
196-
"""
197-
connection = self.create_connection()
198-
try:
199-
yield
200-
finally:
201-
connection.close()
202-
203187
@contextmanager
204188
def provide_connection(self, *args: Any, **kwargs: Any) -> Generator[DuckDBPyConnection, None, None]:
205189
"""Create and provide a database connection.

sqlspec/adapters/oracledb/config/_asyncio.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,6 @@ async def create_pool(self) -> AsyncConnectionPool:
7575
raise ImproperConfigurationError(msg)
7676
return self.pool_instance
7777

78-
@asynccontextmanager
79-
async def lifespan(self, *args: Any, **kwargs: Any) -> AsyncGenerator[None, None]:
80-
db_pool = await self.create_pool()
81-
try:
82-
yield
83-
finally:
84-
await db_pool.close(force=True)
85-
8678
def provide_pool(self, *args: Any, **kwargs: Any) -> Awaitable[AsyncConnectionPool]:
8779
"""Create a pool instance.
8880

sqlspec/adapters/oracledb/config/_sync.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@ def create_pool(self) -> ConnectionPool:
7575
raise ImproperConfigurationError(msg)
7676
return self.pool_instance
7777

78-
def lifespan(self, *args: Any, **kwargs: Any) -> Generator[None, None, None]:
79-
db_pool = self.create_pool()
80-
try:
81-
yield
82-
finally:
83-
db_pool.close()
84-
8578
def provide_pool(self, *args: Any, **kwargs: Any) -> ConnectionPool:
8679
"""Create a pool instance.
8780

sqlspec/adapters/psycopg/config/_async.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,6 @@ async def create_pool(self) -> AsyncConnectionPool:
6363
raise ImproperConfigurationError(msg)
6464
return self.pool_instance
6565

66-
@asynccontextmanager
67-
async def lifespan(self, *args: Any, **kwargs: Any) -> AsyncGenerator[None, None]:
68-
"""Manage the lifecycle of the connection pool."""
69-
pool = await self.create_pool()
70-
try:
71-
yield
72-
finally:
73-
await pool.close()
74-
7566
def provide_pool(self, *args: Any, **kwargs: Any) -> Awaitable[AsyncConnectionPool]:
7667
"""Create and return a connection pool."""
7768
return self.create_pool()

sqlspec/adapters/psycopg/config/_sync.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,6 @@ def create_pool(self) -> ConnectionPool:
6363
raise ImproperConfigurationError(msg)
6464
return self.pool_instance
6565

66-
@contextmanager
67-
def lifespan(self, *args: Any, **kwargs: Any) -> Generator[None, None, None]:
68-
"""Manage the lifecycle of the connection pool."""
69-
pool = self.create_pool()
70-
try:
71-
yield
72-
finally:
73-
pool.close()
74-
7566
def provide_pool(self, *args: Any, **kwargs: Any) -> ConnectionPool:
7667
"""Create and return a connection pool."""
7768
return self.create_pool()

sqlspec/adapters/sqlite/config.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,6 @@ def create_connection(self) -> Connection:
7575
msg = f"Could not configure the SQLite connection. Error: {e!s}"
7676
raise ImproperConfigurationError(msg) from e
7777

78-
@contextmanager
79-
def lifespan(self, *args: Any, **kwargs: Any) -> Generator[None, None, None]:
80-
"""Manage the lifecycle of a database connection.
81-
82-
Yields:
83-
None
84-
85-
Raises:
86-
ImproperConfigurationError: If the connection could not be established.
87-
"""
88-
connection = self.create_connection()
89-
try:
90-
yield
91-
finally:
92-
connection.close()
93-
9478
@contextmanager
9579
def provide_connection(self, *args: Any, **kwargs: Any) -> Generator[Connection, None, None]:
9680
"""Create and provide a database connection.

tests/unit/test_adapters/test_duckdb/test_config.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,3 @@ def test_connection_creation_error(self) -> None:
250250
config = DuckDBConfig()
251251
with pytest.raises(ImproperConfigurationError, match="Could not configure"):
252252
config.create_connection()
253-
254-
def test_connection_lifecycle(self, mock_duckdb_connection: MagicMock) -> None:
255-
"""Test connection lifecycle management."""
256-
config = DuckDBConfig()
257-
258-
# Test lifespan
259-
with config.lifespan():
260-
assert mock_duckdb_connection.close.call_count == 0
261-
assert mock_duckdb_connection.close.call_count == 1
262-
263-
# Test provide_connection
264-
mock_duckdb_connection.reset_mock()
265-
with config.provide_connection() as conn:
266-
assert conn is mock_duckdb_connection
267-
assert mock_duckdb_connection.close.call_count == 0
268-
assert mock_duckdb_connection.close.call_count == 1

0 commit comments

Comments
 (0)