Spaces:
Sleeping
Sleeping
File size: 740 Bytes
5103c2a 10c4276 5103c2a 10c4276 6ac8bb5 10c4276 6ac8bb5 5103c2a |
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 |
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() |