Skip to content

Commit

Permalink
feat(api): allow new lineage insertion if not exists on plan submit
Browse files Browse the repository at this point in the history
  • Loading branch information
hbollon committed Jun 22, 2021
1 parent db01817 commit d0cde4c
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions db/db.go
Expand Up @@ -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", res.Error)
}

var p types.Plan
Expand Down

0 comments on commit d0cde4c

Please sign in to comment.