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())