Spaces:
Runtime error
Runtime error
Download shared/database/schema.py from greenman123/2323: direct link, hf CLI and curl.
- Browser
- Download file 1.46 kB
-
https://huggingface.co/spaces/greenman123/2323/resolve/main/shared/database/schema.py
- Command line
-
hf download hf://spaces/greenman123/2323/shared/database/schema.py
-
curl -L -o schema.py https://huggingface.co/spaces/greenman123/2323/resolve/main/shared/database/schema.py
1.46 kB
| """Database schema initialization.""" | |
| import os | |
| from pathlib import Path | |
| from .connection import DatabaseConnection | |
| async def init_database(db: DatabaseConnection): | |
| """ | |
| Initialize database schema from SQL file. | |
| Args: | |
| db: DatabaseConnection instance | |
| """ | |
| schema_file = Path(__file__).parent / "schema.sql" | |
| with open(schema_file, "r", encoding="utf-8") as f: | |
| schema_sql = f.read() | |
| # Split by semicolon and execute each statement | |
| statements = [s.strip() for s in schema_sql.split(";") if s.strip()] | |
| session = await db.get_session() | |
| try: | |
| for statement in statements: | |
| # Skip comments and empty lines | |
| if statement.startswith("--") or not statement: | |
| continue | |
| # Skip PostgreSQL-specific commands for SQLite | |
| db_type = os.getenv("DATABASE_TYPE", "sqlite") | |
| if db_type == "sqlite": | |
| if "CREATE EXTENSION" in statement: | |
| continue | |
| if "vector(" in statement: | |
| continue | |
| if "USING ivfflat" in statement: | |
| continue | |
| try: | |
| await session.execute(statement) | |
| except Exception as e: | |
| print(f"Warning: Could not execute statement: {e}") | |
| await session.commit() | |
| finally: | |
| await session.close() | |