Spaces:
Runtime error
Runtime error
File size: 2,205 Bytes
bb0f4d5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | """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())
|