Spaces:
Running on Zero
Running on Zero
Download app/db/session.py from avijit28/tunechecker-api: direct link, hf CLI and curl.
- Browser
- Download file 1.16 kB
-
https://huggingface.co/spaces/avijit28/tunechecker-api/resolve/main/app/db/session.py
- Command line
-
hf download hf://spaces/avijit28/tunechecker-api/app/db/session.py
-
curl -L -o session.py https://huggingface.co/spaces/avijit28/tunechecker-api/resolve/main/app/db/session.py
1.16 kB
| import os | |
| from sqlalchemy import create_engine, event | |
| from sqlalchemy.orm import sessionmaker, declarative_base | |
| from app.config import settings | |
| # Ensure data directory exists | |
| if settings.DATABASE_URL.startswith("sqlite:///"): | |
| db_path = settings.DATABASE_URL.replace("sqlite:///", "") | |
| os.makedirs(os.path.dirname(os.path.abspath(db_path)), exist_ok=True) | |
| engine = create_engine( | |
| settings.DATABASE_URL, | |
| connect_args={"check_same_thread": False} if settings.DATABASE_URL.startswith("sqlite") else {}, | |
| pool_pre_ping=True | |
| ) | |
| if settings.DATABASE_URL.startswith("sqlite"): | |
| def set_sqlite_pragma(dbapi_connection, connection_record): | |
| cursor = dbapi_connection.cursor() | |
| cursor.execute("PRAGMA foreign_keys=ON") | |
| cursor.execute("PRAGMA journal_mode=WAL") | |
| cursor.execute("PRAGMA busy_timeout=5000") | |
| cursor.execute("PRAGMA synchronous=NORMAL") | |
| cursor.close() | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| Base = declarative_base() | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |