#!/usr/bin/env python3 """Initialize database metadata for PostgreSQL environments.""" import asyncio import sys from pathlib import Path # Add parent directory to path for imports sys.path.insert(0, str(Path(__file__).parent.parent)) from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession from sqlalchemy.orm import sessionmaker from backend.app.models import Base from backend.app.database import DATABASE_URL from sqlalchemy import text async def init_database(): """Create all tables that don't exist yet.""" print(f"Using database: {DATABASE_URL}") engine = create_async_engine(DATABASE_URL, echo=True) async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) async with engine.begin() as conn: # Create all tables that don't exist await conn.run_sync(Base.metadata.create_all) print("✅ All tables created successfully") # Verify tables were created async with engine.begin() as conn: result = await conn.run_sync( lambda sync_conn: sync_conn.execute( text( """ SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = 'public' ORDER BY tablename """ ) ).fetchall() ) tables = [row[0] for row in result] print(f"📊 Tables in database: {', '.join(sorted(tables))}") await engine.dispose() if __name__ == "__main__": asyncio.run(init_database())