From ccfae85a30b09b9f19b32a1a7b8ded2206899bb7 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Thu, 5 Sep 2024 21:25:28 +0545 Subject: [PATCH] fix: config_items_pkey constraint error (#916) --- db/update.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/db/update.go b/db/update.go index 7fff9fb4..596d5b5f 100644 --- a/db/update.go +++ b/db/update.go @@ -551,7 +551,11 @@ func saveResults(ctx api.ScrapeContext, results []v1.ScrapeResult) (v1.ScrapeSum summary.AddChangeSummary(configType, cs) } - if err := ctx.DB().CreateInBatches(newConfigs, configItemsBulkInsertSize).Error; err != nil { + // NOTE: On duplicate primary key do nothing + // because an incremental scraper might have already inserted the config item. + if err := ctx.DB(). + Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "id"}}, DoNothing: true}). + CreateInBatches(newConfigs, configItemsBulkInsertSize).Error; err != nil { return summary, fmt.Errorf("failed to create config items: %w", dutydb.ErrorDetails(err)) } for _, config := range newConfigs {