import asyncio import os from dotenv import load_dotenv # Load environment variables FIRST before importing app modules load_dotenv(os.path.join(os.path.dirname(__file__), ".env")) from sqlalchemy import text from app.core.database import async_session_maker async def apply_migration(): """Add ai_identity column to tenants table.""" print("🚀 Starting migration: Add ai_identity to tenants...") async with async_session_maker() as session: try: # Check if column exists print("Checking if column exists...") result = await session.execute(text( "SELECT column_name FROM information_schema.columns WHERE table_name='tenants' AND column_name='ai_identity'" )) if result.scalar(): print("⚠️ Column 'ai_identity' already exists. Skipping.") return # Add column print("Adding 'ai_identity' column...") await session.execute(text("ALTER TABLE tenants ADD COLUMN ai_identity JSON")) await session.commit() print("✅ Migration applied successfully!") except Exception as e: print(f"❌ Migration failed: {e}") await session.rollback() if __name__ == "__main__": asyncio.run(apply_migration())