fix(backend): seed.py add __main__ + seed_demo for demo tenant creation
This commit is contained in:
@@ -70,6 +70,21 @@ PRETURI = [
|
||||
]
|
||||
|
||||
|
||||
async def seed_demo(db: AsyncSession) -> None:
|
||||
from app.auth.service import register
|
||||
from sqlalchemy import select
|
||||
from app.db.models.user import User
|
||||
|
||||
r = await db.execute(select(User).where(User.email == "demo@roaauto.ro"))
|
||||
if r.scalar_one_or_none():
|
||||
print("Demo user already exists.")
|
||||
return
|
||||
user, tenant = await register(db, "demo@roaauto.ro", "demo123", "ROA AUTO Demo", "0722000000")
|
||||
print(f"Created demo tenant: {tenant.id}, user: {user.email}")
|
||||
counts = await seed_catalog(db, tenant.id)
|
||||
print(f"Seeded catalog: {counts}")
|
||||
|
||||
|
||||
async def seed_catalog(db: AsyncSession, tenant_id: str) -> dict:
|
||||
now = datetime.now(UTC).isoformat()
|
||||
counts = {}
|
||||
@@ -117,3 +132,19 @@ async def seed_catalog(db: AsyncSession, tenant_id: str) -> dict:
|
||||
|
||||
await db.commit()
|
||||
return counts
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import asyncio
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
|
||||
from app.config import settings
|
||||
import app.db.models # noqa
|
||||
|
||||
async def main():
|
||||
engine = create_async_engine(settings.DATABASE_URL)
|
||||
Session = async_sessionmaker(engine, expire_on_commit=False)
|
||||
async with Session() as db:
|
||||
await seed_demo(db)
|
||||
await engine.dispose()
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
Reference in New Issue
Block a user