Automated Action b2cf3ca746 Fix database migration and path issues
generated with BackendIM... (backend.im)
2025-05-13 16:01:34 +00:00

31 lines
841 B
Python

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from pathlib import Path
# Create the database directory if it doesn't exist
DB_DIR = Path("/projects/genericrestapiservice-w6od1p")
DB_DIR.mkdir(parents=True, exist_ok=True)
# Define the SQLite URL
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"
# Create the SQLAlchemy engine
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False}
)
# Create a SessionLocal class
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Create a Base class for declarative models
Base = declarative_base()
# Dependency to get the database session
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()