-
Notifications
You must be signed in to change notification settings - Fork 0
/
psql_connection.py
33 lines (27 loc) · 1.17 KB
/
psql_connection.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
from requests import session
from sqlalchemy import MetaData, create_engine
from sqlalchemy.orm import sessionmaker
from psql_settings import postgresql
def get_engine(user, password, host, port, db):
"""Creates a mock connection to the database"""
url = f"postgresql://{user}:{password}@{host}:{port}/{db}"
engine = create_engine(url, pool_size=50, echo=False)
return engine
def get_engine_from_settings():
"""Uses settings to create a mock connection to the database"""
keys = ['pguser', 'pgpass', 'pghost', 'pgport', 'pgdb']
if not all(key in keys for key in postgresql.keys()):
raise Exception('Missing keys in postgresql settings')
return get_engine(postgresql['pguser'],
postgresql['pgpass'],
postgresql['pghost'],
postgresql['pgport'],
postgresql['pgdb'])
def get_session() -> sessionmaker:
"""Creates a session to use the database"""
engine = get_engine_from_settings()
session = sessionmaker(bind=engine, expire_on_commit=False)
return session
def get_meta_data(db) -> str:
recipes = MetaData(bind=db)
return recipes