Spaces:
Runtime error
Runtime error
File size: 1,461 Bytes
55b6d21 | 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 | """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()
|