37 lines
1.1 KiB
Python

from pathlib import Path
import os
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# Get base project directory - using current file location approach which is more reliable
# Get the directory of this file
CURRENT_FILE_DIR = Path(__file__).resolve().parent
# Get project root (2 levels up from db directory)
PROJECT_DIR = CURRENT_FILE_DIR.parent.parent
# Create database directory in the project directory
# Use relative paths from the project root to avoid permission issues
DB_DIR = Path("/projects/simpletodoapplication-222fyi/app/storage/db")
# Ensure directory exists before accessing it
os.makedirs(DB_DIR, exist_ok=True)
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
# Dependency to get DB session
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()