Spaces:
Running

File size: 1,257 Bytes
c56f47b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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())