Spaces:
Runtime error
Runtime error
Download backend/src/database.py from debashis2007/IncidentMgmtSystem: direct link, hf CLI and curl.
- Browser
- Download file 897 Bytes
-
https://huggingface.co/spaces/debashis2007/IncidentMgmtSystem/resolve/main/backend/src/database.py
- Command line
-
hf download hf://spaces/debashis2007/IncidentMgmtSystem/backend/src/database.py
-
curl -L -o database.py https://huggingface.co/spaces/debashis2007/IncidentMgmtSystem/resolve/main/backend/src/database.py
897 Bytes
| """ | |
| Database and ORM configuration | |
| """ | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import declarative_base, sessionmaker | |
| from typing import Generator | |
| from src.config.settings import settings | |
| # Create database engine | |
| engine = create_engine( | |
| settings.database_url, | |
| echo=settings.debug, | |
| pool_size=20, | |
| max_overflow=40, | |
| pool_pre_ping=True, | |
| ) | |
| # Create session factory | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| # Base class for all models | |
| Base = declarative_base() | |
| def get_db() -> Generator: | |
| """Dependency for getting database session""" | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |
| def init_db(): | |
| """Initialize database tables""" | |
| Base.metadata.create_all(bind=engine) | |
| def drop_db(): | |
| """Drop all database tables (use with caution)""" | |
| Base.metadata.drop_all(bind=engine) | |