#!/usr/bin/env python3 """Legacy DB bootstrap utility. WARNING: runtime DDL is forbidden in PMFlow production flow. Use Alembic migrations instead of create_all. """ import asyncio import sys from pathlib import Path # Add backend directory to path for imports BACKEND_ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(BACKEND_ROOT)) from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession from sqlalchemy.orm import sessionmaker from app.models import Base from app.core.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())