Skip to content

Commit

Permalink
fix: InsertUpstreamMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Feb 23, 2024
1 parent 74223b3 commit 89c3835
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions upstream/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,17 @@ func InsertUpstreamMsg(ctx context.Context, req *PushData) error {
}

if len(req.Canaries) > 0 {
if err := db.Clauses(clause.OnConflict{UpdateAll: true}).CreateInBatches(req.Canaries, batchSize).Error; err != nil {
return fmt.Errorf("error upserting canaries: %w", err)
// Save one by one (only for debugging purpose)
for _, c := range req.Canaries {
if err := db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&c).Error; err != nil {
return fmt.Errorf("error upserting canaries: (id=%s): %w", c.ID, err)
}
}
}

// components are inserted one by one, instead of in a batch, because of the foreign key constraint with itself.
for _, c := range req.Components {
if err := db.Clauses(clause.OnConflict{UpdateAll: true}).CreateInBatches(req.Components, batchSize).Error; err != nil {
if err := db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&c).Error; err != nil {
logger.Errorf("error upserting component (id=%s): %v", c.ID, err)
}
}
Expand All @@ -144,7 +147,7 @@ func InsertUpstreamMsg(ctx context.Context, req *PushData) error {

// config items are inserted one by one, instead of in a batch, because of the foreign key constraint with itself.
for _, ci := range req.ConfigItems {
if err := db.Clauses(clause.OnConflict{UpdateAll: true}).CreateInBatches(&ci, batchSize).Error; err != nil {
if err := db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&ci).Error; err != nil {
logger.Errorf("error upserting config item (id=%s): %v", ci.ID, err)
}
}
Expand Down

0 comments on commit 89c3835

Please sign in to comment.