-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
53 lines (42 loc) · 1.54 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
import os
import sys
import dotenv
import logging
from contextlib import contextmanager
from typing import Generator
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
from models import Base
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
log.info("Database connection established")
dotenv.load_dotenv()
try:
SQL_DB_NAME = os.getenv("SQLITE_DB_NAME")
if not SQL_DB_NAME:
dotenv.load_dotenv(".env.example")
SQL_DB_NAME = os.getenv("SQLITE_DB_NAME")
if not SQL_DB_NAME:
raise Exception("SQLITE_DB_NAME not set in .env or .env.example")
SQLALCHEMY_DATABASE_URL = f"sqlite:///./{SQL_DB_NAME}.sqlite3"
except Exception as e:
log.error(f"Error loading environment variables: {e}")
sys.exit(1)
# Create the database engine
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
# Create a session object that will be used to interact with the database
SessionLocal = sessionmaker(autocommit=False, autoflush=True, bind=engine)
# Create the database tables
Base.metadata.create_all(bind=engine)
@contextmanager
def get_db() -> Generator[Session, None, None]:
"""
Get database connection object that can be used to interact with the database.
It is a generator function that will automatically close the connection after the operation is done.
The connection object is used to interact with the database in crud.py.
"""
db = SessionLocal()
try:
yield db
finally:
db.close()