online-course-api / app /database.py
leelanjan's picture
Update app/database.py
10c4276 verified
raw
history blame contribute delete
740 Bytes
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base, sessionmaker
import os # <--- This was missing!
# 1. Get the absolute path to the project folder
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
PROJECT_DIR = os.path.dirname(BASE_DIR)
# 2. Use the 'data' folder we created in Dockerfile
DATA_DIR = os.path.join(PROJECT_DIR, "data")
# 3. Create the database URL using the safe path
DATABASE_URL = f"sqlite:///{os.path.join(DATA_DIR, 'course.db')}"
engine = create_engine(
DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(bind=engine)
Base = declarative_base()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()