-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
37 lines (31 loc) · 1.01 KB
/
db.py
File metadata and controls
37 lines (31 loc) · 1.01 KB
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 geoalchemy2 import Geometry # <= not used but must be imported
from flask_sqlalchemy import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from credentials import USERNAME, PASSWORD, HOSTNAME, PORT, DB_NAME
from contextlib import contextmanager
engine = sqlalchemy.create_engine(f"postgresql+psycopg2://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DB_NAME}")
metadata = sqlalchemy.MetaData(engine)
metadata.reflect()
Base = automap_base(metadata=metadata)
Base.prepare(engine)
# All db tables 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
UserLog = Base.classes.UserLog
Contact = Base.classes.Contact
Locations = Base.classes.Locations
Volunteer = Base.classes.Volunteer
@contextmanager
def get_session():
Session = sqlalchemy.orm.sessionmaker(engine)
session = Session()
try:
yield session
except:
session.rollback()
raise
else:
session.commit()