-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
62 lines (51 loc) · 1.66 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
55
56
57
58
59
60
61
62
"""
Postgres database connection class.
Used to store and retrieve farmer information.
Note: Not currently in use.
"""
from typing import Optional
import psycopg2 as pg
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
class AguaDB:
"""A class that works with the Agua database.
This class expects calls to conform to the schema outlined in
schema.ddl
"""
connection: Optional[pg.extensions.connection]
def __init__(self) -> None:
"""Initializes the database connection.
This function will not return until the connection is established.
"""
self.connection = None
def connect(self) -> bool:
"""Connects to the database.
Takes no arguments as connection should be pulled direclty from env variables
to avoid leaking credentials and mistakes.
"""
try:
self.connection = pg.connect(
host=os.environ["DB_HOST"],
port=os.environ["DB_PORT"],
user=os.environ["DB_USER"],
password=os.environ["DB_PASSWORD"],
database=os.environ["DB_NAME"],
options="-c search_path=agua,public"
)
return True
except pg.Error as e:
print(e)
return False
def disconnect(self) -> bool:
"""Disconnects from the database.
This function will not return until the connection is closed.
"""
try:
if not self.connection.closed:
self.connection.close()
return True
except pg.Error as e:
print(e)
return False