Spaces:
Running
Running
Download backend/apply_order_migration.py from axeronis/microfunnel-api: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/backend/apply_order_migration.py
- Command line
-
hf download hf://spaces/axeronis/microfunnel-api/backend/apply_order_migration.py
-
curl -L -o apply_order_migration.py https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/backend/apply_order_migration.py
1.26 kB
| import asyncio | |
| import os | |
| from sqlmodel import SQLModel, create_engine, text | |
| from sqlalchemy.ext.asyncio import create_async_engine | |
| # Import models to register them with SQLModel.metadata | |
| from app.models.tenant import Tenant | |
| from app.models.lead import Lead | |
| from app.models.product import Product | |
| from app.models.order import Order, OrderItem | |
| from app.core.config import settings | |
| # Database Interface | |
| DATABASE_URL = str(settings.DATABASE_URL) | |
| if not DATABASE_URL: | |
| # Fallback for local dev if .env not loaded (try to read .env manually if needed, or assume local) | |
| # Using a default or erroring out. | |
| print("โ DATABASE_URL not set") | |
| exit(1) | |
| # Fix for asyncpg | |
| if DATABASE_URL.startswith("postgresql://"): | |
| DATABASE_URL = DATABASE_URL.replace("postgresql://", "postgresql+asyncpg://", 1) | |
| engine = create_async_engine(DATABASE_URL, echo=True) | |
| async def create_tables(): | |
| print("๐ Creating Order and OrderItem tables...") | |
| async with engine.begin() as conn: | |
| # Create all tables that don't exist | |
| # This is safe, it won't recreate existing ones | |
| await conn.run_sync(SQLModel.metadata.create_all) | |
| print("โ Tables created successfully!") | |
| if __name__ == "__main__": | |
| asyncio.run(create_tables()) | |