Spaces:
Sleeping

microfunnel-api / backend /apply_ai_training_migration.py
MicroFunnel
Production-ready: Refactored codebase with Railway + Vercel config
cd61bf7
Raw History Blame Contribute Delete
1.84 kB
import asyncio
import sys
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text
from app.core.config import settings
# Fix for Windows: psycopg requires SelectorEventLoop on Windows
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
async def apply_migration():
"""Apply AI training tables migration."""
# Create async engine
async_database_url = settings.DATABASE_URL.replace(
"postgresql://", "postgresql+psycopg://"
)
engine = create_async_engine(async_database_url, echo=True)
try:
# Read migration file
with open('migrations/create_ai_training_tables.sql', 'r', encoding='utf-8') as f:
sql = f.read()
# Execute migration
async with engine.begin() as conn:
await conn.execute(text(sql))
print("✅ AI Training tables migration applied successfully!")
# Verify tables
async with engine.connect() as conn:
result = await conn.execute(text("""
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name IN ('ai_feedback', 'ai_training_examples', 'ai_learning_rules')
ORDER BY table_name
"""))
tables = result.fetchall()
print(f"✅ Verified {len(tables)} tables created:")
for table in tables:
print(f" - {table[0]}")
except Exception as e:
print(f"❌ Migration failed: {e}")
import traceback
traceback.print_exc()
raise
finally:
await engine.dispose()
if __name__ == "__main__":
asyncio.run(apply_migration())