2323 / shared /database /schema.py
GreenMan101
Add Doctor Freud Bot - all files
55b6d21
Raw History Blame Contribute Delete
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()