Spaces:
Sleeping
Sleeping
Download database/init_db.py from sbasu2512/stock-scraper: direct link, hf CLI and curl.
- Browser
- Download file 1.87 kB
-
https://huggingface.co/spaces/sbasu2512/stock-scraper/resolve/main/database/init_db.py
- Command line
-
hf download hf://spaces/sbasu2512/stock-scraper/database/init_db.py
-
curl -L -o init_db.py https://huggingface.co/spaces/sbasu2512/stock-scraper/resolve/main/database/init_db.py
1.87 kB
| """Idempotent SQLite schema initialization. | |
| SQLite has no docker-entrypoint-initdb.d equivalent, so this is called on | |
| every startup of the long-running processes (API, scheduler). Every | |
| table/index in schema.sql uses IF NOT EXISTS, so repeated calls are safe. | |
| """ | |
| from __future__ import annotations | |
| from pathlib import Path | |
| from database.connection import connect | |
| SCHEMA_PATH = Path(__file__).parent / "schema.sql" | |
| JOB_RUN_PROGRESS_COLUMNS = { | |
| "total_rows": "INTEGER NOT NULL DEFAULT 0", | |
| "resolved_rows": "INTEGER NOT NULL DEFAULT 0", | |
| "failed_rows": "INTEGER NOT NULL DEFAULT 0", | |
| "last_updated_at": "TIMESTAMP", | |
| } | |
| PREDICTIONS_COLUMNS = {"calibrated_probability": "REAL"} | |
| def _migrate_job_runs(conn) -> None: | |
| """Add progress columns for databases created before resolution tracking.""" | |
| existing = {row["name"] for row in conn.execute("PRAGMA table_info(job_runs)")} | |
| for name, definition in JOB_RUN_PROGRESS_COLUMNS.items(): | |
| if name not in existing: | |
| conn.execute(f"ALTER TABLE job_runs ADD COLUMN {name} {definition}") | |
| def _migrate_predictions(conn) -> None: | |
| """Additive migration for databases created before Platt scaling.""" | |
| existing = {row["name"] for row in conn.execute("PRAGMA table_info(predictions)")} | |
| for name, definition in PREDICTIONS_COLUMNS.items(): | |
| if name not in existing: | |
| conn.execute(f"ALTER TABLE predictions ADD COLUMN {name} {definition}") | |
| def init_db(database_url: str | None = None) -> None: | |
| from app.config import settings | |
| conn = connect(database_url or settings.DATABASE_URL) | |
| try: | |
| conn.executescript(SCHEMA_PATH.read_text()) | |
| _migrate_job_runs(conn) | |
| _migrate_predictions(conn) | |
| conn.commit() | |
| finally: | |
| conn.close() | |
| if __name__ == "__main__": | |
| init_db() | |
| print("Database schema initialized.") | |