There was an error while loading. Please reload this page.
1 parent 2f259e9 commit ab399f7Copy full SHA for ab399f7
src/dialog/db/__init__.py
@@ -2,12 +2,26 @@
2
from sqlalchemy import create_engine
3
from sqlalchemy.orm import Session, DeclarativeBase
4
5
+from contextlib import contextmanager
6
+
7
engine = create_engine(Settings().DATABASE_URL)
8
9
10
class Base(DeclarativeBase):
11
pass
12
-def get_session(): # pragma: no cover
- with Session(engine) as session:
- yield session
13
+@contextmanager
14
+def session_scope():
15
+ with Session(bind=engine) as session:
16
+ try:
17
+ yield session
18
+ session.commit()
19
+ except Exception as exc:
20
+ session.rollback()
21
+ raise exc
22
+ finally:
23
+ session.close()
24
25
+def get_session():
26
+ with session_scope() as session:
27
0 commit comments