Spaces:
Sleeping
Sleeping
Download src/database/database.py from AlishbaRehman/fullbackend: direct link, hf CLI and curl.
- Browser
- Download file 1.75 kB
-
https://huggingface.co/spaces/AlishbaRehman/fullbackend/resolve/main/src/database/database.py
- Command line
-
hf download hf://spaces/AlishbaRehman/fullbackend/src/database/database.py
-
curl -L -o database.py https://huggingface.co/spaces/AlishbaRehman/fullbackend/resolve/main/src/database/database.py
1.75 kB
| from sqlmodel import create_engine | |
| from sqlalchemy.pool import StaticPool | |
| from ..config import settings | |
| import urllib.parse | |
| import logging | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| # Global engine instance to reuse connection | |
| _engine = None | |
| # Function to create the database engine | |
| def get_engine(): | |
| global _engine | |
| if _engine is not None: | |
| return _engine | |
| logger.info(f"Creating database engine for: {settings.DATABASE_URL[:50]}...") | |
| # For SQLite, use StaticPool and check_same_thread=False | |
| if settings.DATABASE_URL.lower().startswith("sqlite"): | |
| _engine = create_engine( | |
| settings.DATABASE_URL, | |
| echo=True, | |
| connect_args={"check_same_thread": False}, # Required for SQLite in multithreaded environments | |
| poolclass=StaticPool | |
| ) | |
| else: | |
| # For PostgreSQL (including Neon), handle SSL requirements | |
| # Parse the database URL to check if it's a Neon URL | |
| if "neon.tech" in settings.DATABASE_URL.lower(): | |
| # For Neon databases, we need to add SSL parameters | |
| _engine = create_engine( | |
| settings.DATABASE_URL, | |
| echo=True, | |
| connect_args={ | |
| "sslmode": "require", # Required for Neon connections | |
| "channel_binding": "disable" # Changed from "require" to "disable" for broader compatibility | |
| } | |
| ) | |
| else: | |
| # Standard PostgreSQL connection | |
| _engine = create_engine( | |
| settings.DATABASE_URL, | |
| echo=True | |
| ) | |
| logger.info("Database engine created successfully") | |
| return _engine |