forked from ExposuresProvider/icees-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
36 lines (26 loc) · 977 Bytes
/
db.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
from sqlalchemy import create_engine
import os
from contextlib import contextmanager
service_name = "ICEES"
serv_user = os.environ[service_name + "_DBUSER"]
serv_password = os.environ[service_name + "_DBPASS"]
serv_host = os.environ[service_name + "_HOST"]
serv_port = os.environ[service_name + "_PORT"]
serv_database = os.environ[service_name + "_DATABASE"]
serv_max_overflow = int(os.environ[service_name + "_DB_MAX_OVERFLOW"])
serv_pool_size = int(os.environ[service_name + "_DB_POOL_SIZE"])
engine = None
def get_db_connection():
global engine
if engine is None:
engine = create_engine("postgresql+psycopg2://"+serv_user+":"+serv_password+"@"+serv_host+":"+serv_port+"/"+serv_database, pool_size=serv_pool_size, max_overflow=serv_max_overflow)
return engine
@contextmanager
def DBConnection():
engine = get_db_connection()
conn = engine.connect()
try:
yield conn
finally:
conn.close()
# engine.dispose()