From 90df7066a136d5d11a7868a7406aa9f03412c5bd Mon Sep 17 00:00:00 2001 From: Hugo Bollon Date: Tue, 22 Jun 2021 13:52:16 +0200 Subject: [PATCH] feat(api): allow new lineage insertion if not exists on plan submit --- db/db.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/db/db.go b/db/db.go index d3aa64c7..0bb314aa 100644 --- a/db/db.go +++ b/db/db.go @@ -567,15 +567,15 @@ func (db *Database) ListAttributeKeys(resourceType string) (results []string, er // InsertPlan inserts a Terraform plan with associated information in the Database func (db *Database) InsertPlan(plan []byte) error { - // Check for lineage existence var lineage types.Lineage if err := json.Unmarshal(plan, &lineage); err != nil { return err } - res := db.First(&lineage, lineage) - if errors.Is(res.Error, gorm.ErrRecordNotFound) { - return fmt.Errorf("Plan's lineage not found in db") + // Recover lineage from db if it's already exists or insert it + res := db.FirstOrCreate(&lineage, lineage) + if res.Error != nil { + return fmt.Errorf("Error on lineage retrival during plan insertion: %v\n", res.Error) } var p types.Plan