From 717ec1e061ba8737c1e4425b6f5cd8f30dd51d7c Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Fri, 17 Jul 2026 23:22:09 +0700 Subject: [PATCH] Refactor database seeder to improve error handling and maintainability --- app/seeders/database_seeder.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/app/seeders/database_seeder.py b/app/seeders/database_seeder.py index db66f9d..c48f0b4 100644 --- a/app/seeders/database_seeder.py +++ b/app/seeders/database_seeder.py @@ -9,19 +9,23 @@ 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) + try: + 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) + 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) + 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) + users = seed_users(db, count) - db.commit() + db.commit() + except: + db.rollback() + raise return { "business_types": len(business_types),