From 2a882f37b988f68118194c640bff0314b25d6935 Mon Sep 17 00:00:00 2001 From: Rahmat Hidayat Date: Sat, 16 Sep 2023 19:36:56 +0700 Subject: [PATCH] feat(resource): support batch in BulkUpsert (#71) * feat(resource): support batch in BulkUpsert * fix: use db session to fix not working batchinsert --- internal/store/postgres/resource_repository.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/store/postgres/resource_repository.go b/internal/store/postgres/resource_repository.go index 9e50ced9d..0818e95dc 100644 --- a/internal/store/postgres/resource_repository.go +++ b/internal/store/postgres/resource_repository.go @@ -115,7 +115,9 @@ func (r *ResourceRepository) BulkUpsert(ctx context.Context, resources []*domain if len(models) > 0 { return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { // upsert clause is moved to model.Resource.BeforeCreate() (gorm's hook) to apply the same for associations (model.Resource.Children) - if err := r.db.Create(models).Error; err != nil { + if err := r.db. + Session(&gorm.Session{CreateBatchSize: 1000}). + Create(models).Error; err != nil { return err }