Spaces:
Running
Running
| 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() |