-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_helper.py
37 lines (30 loc) · 1.1 KB
/
db_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from sqlalchemy import create_engine, exc, text
from sqlalchemy.orm import scoped_session, sessionmaker
import os
import psycopg2
from contextlib import contextmanager
class DbHelper:
conn_string = os.getenv("DATABASE_URL") if os.getenv("DATABASE_URL") else None
def __init__(self, conn_string=None):
if not conn_string and not self.conn_string:
raise RuntimeError("DATABASE_URL is not set")
self.engine = create_engine(conn_string or self.conn_string)
self.db_session = scoped_session(sessionmaker(bind=self.engine))
@contextmanager
def session_scope(self):
"""Provide a transactional scope around a series of operations."""
session = self.db_session()
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
def close_connection(self):
self.engine.dispose()
db = DbHelper()
engine = db.engine
session = db.db_session() # session.execute
context_session = db.session_scope # with context_session() as session: