From bcd38ec668fcfd0f11777d65f135dfcf1ac351ee Mon Sep 17 00:00:00 2001 From: hunterlong Date: Mon, 18 May 2020 21:17:38 -0700 Subject: [PATCH] bulk SQL records insert fix, chunk size per db type --- database/database.go | 12 ++++++++++++ types/failures/samples.go | 2 +- types/hits/samples.go | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/database/database.go b/database/database.go index 69c29989..1ed39905 100644 --- a/database/database.go +++ b/database/database.go @@ -105,6 +105,18 @@ type Database interface { ParseTime(t string) (time.Time, error) DbType() string GormDB() *gorm.DB + ChunkSize() int +} + +func (it *Db) ChunkSize() int { + switch it.Database.Dialect().GetName() { + case "mysql": + return 3000 + case "postgres": + return 3000 + default: + return 100 + } } func (it *Db) GormDB() *gorm.DB { diff --git a/types/failures/samples.go b/types/failures/samples.go index 069fb842..1fb333b9 100644 --- a/types/failures/samples.go +++ b/types/failures/samples.go @@ -42,7 +42,7 @@ func Samples() error { records = append(records, failure) createdAt = createdAt.Add(35 * time.Minute) } - if err := gormbulk.BulkInsert(db.GormDB(), records, 3000); err != nil { + if err := gormbulk.BulkInsert(db.GormDB(), records, db.ChunkSize()); err != nil { log.Error(err) return err } diff --git a/types/hits/samples.go b/types/hits/samples.go index a85cf19d..d432886d 100644 --- a/types/hits/samples.go +++ b/types/hits/samples.go @@ -16,7 +16,7 @@ var SampleHits = 99900. func Samples() error { for i := int64(1); i <= 5; i++ { records := createHitsAt(i) - if err := gormbulk.BulkInsert(db.GormDB(), records, 3000); err != nil { + if err := gormbulk.BulkInsert(db.GormDB(), records, db.ChunkSize()); err != nil { log.Error(err) return err }