Spaces:
Runtime error
Runtime error
Download app/core/database.py from lea97338/Files: direct link, hf CLI and curl.
- Browser
- Download file 982 Bytes
-
https://huggingface.co/spaces/lea97338/Files/resolve/refs%2Fpr%2F2/app/core/database.py
- Command line
-
hf download hf://spaces/lea97338/Files@refs/pr/2/app/core/database.py
-
curl -L -o database.py https://huggingface.co/spaces/lea97338/Files/resolve/refs%2Fpr%2F2/app/core/database.py
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() | |