27 lines
665 B
Python
27 lines
665 B
Python
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from app.core.config import settings
|
|
|
|
# Ensure the database directory exists with proper permissions
|
|
settings.DB_DIR.mkdir(parents=True, exist_ok=True, mode=0o755)
|
|
|
|
# Create SQLAlchemy engine
|
|
engine = create_engine(
|
|
settings.SQLALCHEMY_DATABASE_URL,
|
|
connect_args={"check_same_thread": False} # Only needed for SQLite
|
|
)
|
|
|
|
# Create sessionmaker
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
def get_db():
|
|
"""
|
|
Dependency for getting a database session.
|
|
"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close() |