Files / app /core /database.py
lea97338's picture
Upload 100 files (#1)
680fa2b
Raw
History Blame Contribute Delete
982 Bytes
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# Database URL configuration
# For local development, default to SQLite fallback.
# For production/staging, it will read PostgreSQL from environment variables.
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./constructhire.db")
# Support SQLite configuration parameters
if DATABASE_URL.startswith("sqlite"):
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
else:
# Use pool_pre_ping=True to prevent stale connection errors with Supabase/Render
engine = create_engine(DATABASE_URL, pool_pre_ping=True)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def get_db():
"""
Database session generator to be used as a FastAPI dependency.
Yields a database session and ensures it is closed after the request is finished.
"""
db = SessionLocal()
try:
yield db
finally:
db.close()