forked from itsadityagupta/track-job-applications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
59 lines (46 loc) · 1.92 KB
/
database.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
import os.path
import sqlite3
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from datamodels.job_application import Base
from logger import logger
from shared.app_functions import get_conn_string
Session = sessionmaker()
class Database:
"""Initializes database connection"""
def __init__(self, db_path: str, echo: bool = True):
self.conn = None
self.engine = None
self.connection_string = get_conn_string(db_path)
self.create_db(db_path, echo)
self.session: Session = Session(bind=self.engine)
def create_conn(self, db_path: str):
"""Creates a connection to the database"""
if not self.conn:
self.conn = sqlite3.connect(db_path)
logger.info("Connection to db created successfully!")
else:
logger.warning("Connection already established!")
def create_engine(self, echo: bool):
"""Create an engine for sqlalchemy to bind the datamodels"""
if not self.engine:
self.engine = create_engine(self.connection_string, echo=echo)
logger.info("Engine created successfully!")
else:
logger.warning("Engine already created!")
def create_db(self, db_path: str, echo: bool):
"""Main function to create the database"""
# TODO: use less lines of code
if os.path.isfile(db_path):
self.create_engine(echo)
Base.metadata.create_all(self.engine)
logger.info("Database already created.")
# TODO: Add option to delete the file and recreate the DB (force create)
else:
self.create_conn(db_path)
self.create_engine(echo)
Base.metadata.create_all(self.engine)
logger.info(f"Database created at {db_path}.")
# if __name__ == "__main__":
# path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'site.db')
# obj = Database(path)