-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
56 lines (39 loc) · 1.31 KB
/
database.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base, sessionmaker
from contextlib import contextmanager
from config import settings
from utils.logger import logger
engine = None
SessionLocal = None
def get_connection_string():
connection_string = settings.SQLALCHEMY_DATABASE_URI
# if connection_string.startswith('postgresql://'):
# connection_string = connection_string.replace('postgresql://', 'postgresql+psycopg://')
return connection_string
Base = declarative_base()
def init_engine():
uri = get_connection_string()
logger.info(f"Initializing engine: {uri}")
global engine, SessionLocal
engine = create_engine(uri)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
return engine
def init_db():
Base.metadata.create_all(bind=engine)
def drop_db():
Base.metadata.drop_all(bind=engine)
@contextmanager
def session_scope():
session = SessionLocal()
try:
yield session
except Exception as e:
logger.info("Rolling back session due to error")
session.rollback()
raise e
finally:
logger.info("Closing session")
session.close()
def get_db():
with session_scope() as session:
yield session