-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
54 lines (43 loc) · 1.37 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import sqlalchemy
from geoalchemy2 import Geometry # <= not used but must be imported
from sqlalchemy.ext.automap import automap_base
from credentials import USERNAME, PASSWORD, HOSTNAME, PORT, DB_NAME
from contextlib import contextmanager
engine = sqlalchemy.create_engine("postgresql+psycopg2://{}:{}@{}:{}/{}".format(USERNAME,PASSWORD,HOSTNAME,PORT,DB_NAME))
metadata = sqlalchemy.MetaData(engine)
metadata.reflect()
Base = automap_base(metadata=metadata)
Base.prepare(engine)
# All tables that can be queried using the API
Demand = Base.classes.Demand
Supply = Base.classes.Supply
Raw = Base.classes.Raw
Matches = Base.classes.Matches
Auth = Base.classes.Auth
Contact = Base.classes.Contact
UserLog = Base.classes.UserLog
"""
class Demand(Base):
__table__ = sqlalchemy.Table("Demand", metadata)
class Supply(Base):
__table__ = sqlalchemy.Table("Supply", metadata)
class Matches(Base):
__table__ = sqlalchemy.Table("Matches", metadata)
class Raw(Base):
__table__ = sqlalchemy.Table("Raw", metadata)
class Auth(Base):
__table__ = sqlalchemy.Table("Auth", metadata)
class UserLog(Base):
__table__ = sqlalchemy.Table("UserLog", metadata)
"""
@contextmanager
def get_session():
Session = sqlalchemy.orm.sessionmaker(engine)
session = Session()
try:
yield session
except:
session.rollback()
raise
else:
session.commit()