Skip to content

Commit 5d81de5

Browse files
authored
Merge pull request #74 from python-ellar/session_close
fix: Session Cleanup
2 parents 5848fc4 + accb319 commit 5d81de5

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

ellar_sql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""EllarSQL Module adds support for SQLAlchemy and Alembic package to your Ellar application"""
22

3-
__version__ = "0.1.3"
3+
__version__ = "0.1.5"
44

55
from .model.database_binds import get_all_metadata, get_metadata
66
from .module import EllarSQLModule

ellar_sql/factory/base.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import inspect
12
import typing as t
23

4+
import factory
35
import sqlalchemy as sa
46
import sqlalchemy.orm as sa_orm
57
from ellar.threading import run_as_sync
@@ -118,3 +120,37 @@ def _save(
118120
cls._session_execute(session.commit)
119121
cls._session_execute(session.refresh, obj)
120122
return obj
123+
124+
125+
class EllarSQLSubFactoryId(factory.SubFactory):
126+
"""
127+
A SubFactory that returns the id of the created object.
128+
"""
129+
130+
def evaluate(self, instance, step, extra):
131+
value = super().evaluate(instance, step, extra)
132+
if inspect.isawaitable(value):
133+
134+
async def resolve_value():
135+
resolved_value = await value
136+
return resolved_value.id
137+
138+
return resolve_value()
139+
return value
140+
141+
142+
class EllarSQLSubFactory(factory.SubFactory):
143+
"""
144+
A SubFactory that returns the created object.
145+
"""
146+
147+
def evaluate(self, instance, step, extra):
148+
value = super().evaluate(instance, step, extra)
149+
if inspect.isawaitable(value):
150+
151+
async def resolve_value():
152+
resolved_value = await value
153+
return resolved_value
154+
155+
return resolve_value()
156+
return value

ellar_sql/module.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ def _raise_exception():
2727
return _raise_exception
2828

2929

30+
async def _session_cleanup(
31+
db_service: EllarSQLService, session: t.Union[Session, AsyncSession]
32+
) -> None:
33+
res = db_service.session_factory.remove()
34+
if isinstance(res, t.Coroutine):
35+
await res
36+
37+
res = session.close()
38+
if isinstance(res, t.Coroutine):
39+
await res
40+
41+
3042
@as_middleware
3143
async def session_middleware(
3244
context: IHostContext, call_next: t.Callable[..., t.Coroutine]
@@ -40,15 +52,8 @@ async def session_middleware(
4052

4153
try:
4254
await call_next()
43-
except Exception as ex:
44-
res = session.rollback()
45-
if isinstance(res, t.Coroutine):
46-
await res
47-
raise ex
48-
49-
res = db_service.session_factory.remove()
50-
if isinstance(res, t.Coroutine):
51-
await res
55+
finally:
56+
await _session_cleanup(db_service, session)
5257

5358

5459
@Module(

requirements-tests.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ autoflake
44
ellar-cli >= 0.3.7
55
factory-boy >= 3.3.0
66
httpx
7-
Pillow >=10.4.0, <11.2.0
87
mypy == 1.15.0
8+
Pillow >=10.4.0, <11.2.0
99
pytest >= 7.1.3,< 9.0.0
1010
pytest-asyncio
1111
pytest-cov >= 2.12.0,< 7.0.0
12-
ruff ==0.9.9
12+
ruff ==0.13.3

samples/db-learning/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def tm():
1616
test_module = Test.create_test_module(modules=[ApplicationModule])
1717
app = test_module.create_application()
1818

19-
with execute_async_context_manager(app.application_context()):
19+
with execute_async_context_manager(app.with_injector_context()):
2020
yield test_module
2121

2222

0 commit comments

Comments
 (0)