Spaces:
Sleeping
Sleeping
Download backend/apply_ai_training_migration.py from axeronis/microfunnel-api: direct link, hf CLI and curl.
- Browser
- Download file 1.84 kB
-
https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/backend/apply_ai_training_migration.py
- Command line
-
hf download hf://spaces/axeronis/microfunnel-api/backend/apply_ai_training_migration.py
-
curl -L -o apply_ai_training_migration.py https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/backend/apply_ai_training_migration.py
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()) | |