Spaces:
Runtime error
Runtime error
Download scripts/bootstrap_db.py from welcometoFightclub/acpc-testing-7: direct link, hf CLI and curl.
- Browser
- Download file 2.21 kB
-
https://huggingface.co/spaces/welcometoFightclub/acpc-testing-7/resolve/main/scripts/bootstrap_db.py
- Command line
-
hf download hf://spaces/welcometoFightclub/acpc-testing-7/scripts/bootstrap_db.py
-
curl -L -o bootstrap_db.py https://huggingface.co/spaces/welcometoFightclub/acpc-testing-7/resolve/main/scripts/bootstrap_db.py
2.21 kB
| """Bootstrap script: create Qdrant collection and relational DB schema. | |
| Usage: | |
| python scripts/bootstrap_db.py | |
| """ | |
| from __future__ import annotations | |
| import asyncio | |
| import sys | |
| from loguru import logger | |
| from app.core.settings import settings | |
| from app.ingestion.vector_store import bootstrap_collection | |
| POSTGRES_SCHEMA = """ | |
| CREATE TABLE IF NOT EXISTS documents ( | |
| id SERIAL PRIMARY KEY, | |
| name TEXT NOT NULL, | |
| category TEXT, | |
| created_at TIMESTAMPTZ DEFAULT NOW() | |
| ); | |
| CREATE TABLE IF NOT EXISTS ranks ( | |
| id SERIAL PRIMARY KEY, | |
| student_id TEXT NOT NULL, | |
| rank INTEGER NOT NULL, | |
| category TEXT, | |
| year INTEGER, | |
| created_at TIMESTAMPTZ DEFAULT NOW() | |
| ); | |
| CREATE INDEX IF NOT EXISTS idx_ranks_category ON ranks(category); | |
| CREATE INDEX IF NOT EXISTS idx_ranks_year ON ranks(year); | |
| CREATE INDEX IF NOT EXISTS idx_ranks_student ON ranks(student_id); | |
| """ | |
| SQLITE_SCHEMA = POSTGRES_SCHEMA.replace( | |
| "SERIAL", "INTEGER" | |
| ).replace( | |
| "TIMESTAMPTZ", "TEXT" | |
| ).replace( | |
| "NOW()", "CURRENT_TIMESTAMP" | |
| ) | |
| async def _bootstrap_postgres() -> None: | |
| import asyncpg | |
| dsn = settings.POSTGRES_DSN.replace("postgresql+asyncpg://", "postgresql://") | |
| try: | |
| conn = await asyncpg.connect(dsn=dsn) | |
| except Exception as e: | |
| logger.warning(f"Postgres unreachable ({e}); skipping PG bootstrap.") | |
| return | |
| try: | |
| await conn.execute(POSTGRES_SCHEMA) | |
| logger.info("Postgres schema ensured.") | |
| finally: | |
| await conn.close() | |
| async def _bootstrap_sqlite() -> None: | |
| import aiosqlite | |
| async with aiosqlite.connect(settings.SQLITE_PATH) as db: | |
| await db.executescript(SQLITE_SCHEMA) | |
| await db.commit() | |
| logger.info("SQLite schema ensured.") | |
| async def main() -> None: | |
| logger.info("Bootstrapping Qdrant collection...") | |
| try: | |
| await bootstrap_collection() | |
| except Exception as e: | |
| logger.error(f"Qdrant bootstrap failed: {e}") | |
| logger.info("Bootstrapping relational schema...") | |
| if settings.USE_SQLITE_FALLBACK: | |
| await _bootstrap_sqlite() | |
| else: | |
| await _bootstrap_postgres() | |
| logger.info("Bootstrap complete.") | |
| if __name__ == "__main__": | |
| asyncio.run(main()) | |