-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
29 lines (21 loc) · 802 Bytes
/
database.py
File metadata and controls
29 lines (21 loc) · 802 Bytes
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
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base, sessionmaker
def get_db_url():
if "DB_URL" not in os.environ:
raise ValueError("Environment variable 'DB_URL' is not set.")
return os.getenv("DB_URL")
SQLALCHEMY_DATABASE_URL = get_db_url()
engine = create_engine(SQLALCHEMY_DATABASE_URL)
# Uncomment the following lines to use SQLite instead of PostgreSQL
# SQLALCHEMY_DATABASE_URL = "sqlite:///./todosapp.db"
# engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
db = SessionLocal()
print("original get_db called")
try:
yield db
finally:
db.close()