api/app/seeders/database_seeder.py

45 lines
1.3 KiB
Python

from sqlalchemy.orm import Session
from app.database import SessionLocal
from app.seeders.business_type_seeder import seed_business_types
from app.seeders.plan_seeder import seed_plans
from app.seeders.subscription_seeder import seed_subscriptions
from app.seeders.tenant_seeder import seed_tenants
from app.seeders.user_seeder import seed_users
def seed_all(db: Session, count: int = 5):
business_types = seed_business_types(db, count)
plans = seed_plans(db, count)
bt_ids = [bt.id for bt in business_types]
tenants = seed_tenants(db, count, business_type_ids=bt_ids)
plan_ids = [p.id for p in plans]
tenant_ids = [t.id for t in tenants]
seed_subscriptions(db, count, tenant_ids=tenant_ids, plan_ids=plan_ids)
users = seed_users(db, count)
db.commit()
return {
"business_types": len(business_types),
"plans": len(plans),
"tenants": len(tenants),
"subscriptions": count,
"users": len(users),
"user_profiles": len(users),
}
def seed_database(count: int = 5):
db = SessionLocal()
try:
result = seed_all(db, count)
print("Database seeding completed:")
for model, total in result.items():
print(f" - {model}: {total}")
finally:
db.close()