Skip to content

Commit

Permalink
chore: fail topology run if any child component persistence fails
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Oct 23, 2024
1 parent 7d75548 commit d08c6fa
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pkg/db/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty"
"github.com/flanksource/duty/context"
dutydb "github.com/flanksource/duty/db"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
"github.com/google/uuid"
Expand Down Expand Up @@ -78,7 +79,7 @@ func PersistComponent(ctx context.Context, component *pkg.Component) ([]uuid.UUI

existing, err = component.FindExisting(ctx)
if err != nil {
return persisted, fmt.Errorf("error finding component: %v", err)
return persisted, fmt.Errorf("error finding component: %w", err)
}

tx := db.Table("components")
Expand All @@ -94,7 +95,7 @@ func PersistComponent(ctx context.Context, component *pkg.Component) ([]uuid.UUI
if existing.DeletedAt != component.DeletedAt {
// Since gorm ignores nil fields, we are setting deleted_at explicitly
if err := db.Table("components").Where("id = ?", existing.ID).UpdateColumn("deleted_at", nil).Error; err != nil {
return nil, fmt.Errorf("failed to undelete: %v", err)
return nil, fmt.Errorf("failed to undelete: %w", err)
}
}
} else {
Expand All @@ -106,7 +107,7 @@ func PersistComponent(ctx context.Context, component *pkg.Component) ([]uuid.UUI
).Create(component)
}
if tx.Error != nil {
return persisted, tx.Error
return persisted, dutydb.ErrorDetails(tx.Error)
}

persisted = append(persisted, component.ID)
Expand All @@ -120,13 +121,13 @@ func PersistComponent(ctx context.Context, component *pkg.Component) ([]uuid.UUI

child.ParentId = &component.ID
if childIDs, err := PersistComponent(ctx, child); err != nil {
logger.Errorf("Error persisting child component of %v, :v", component.ID, err)
return persisted, fmt.Errorf("error persisting child component of [%s]: %w", component.ID, dutydb.ErrorDetails(err))
} else {
persisted = append(persisted, childIDs...)
}
}

return persisted, tx.Error
return persisted, dutydb.ErrorDetails(tx.Error)
}

func UpdateStatusAndSummaryForComponent(db *gorm.DB, id uuid.UUID, status types.ComponentStatus, summary types.Summary) (int64, error) {
Expand Down

0 comments on commit d08c6fa

Please sign in to comment.