Seed provenance + inventory: source links and seed lots (#50) (#64)
Build image / build-and-push (push) Successful in 8s
Build image / build-and-push (push) Successful in 8s
Co-authored-by: Steve Dudenhoeffer <[email protected]>
This commit was merged in pull request #64.
This commit is contained in:
@@ -417,6 +417,9 @@ type revertOps[T any] struct {
|
||||
// remove undoes a creation. It returns the changes it made, because deleting
|
||||
// an object also deletes the plantings hanging off it.
|
||||
remove func(ctx context.Context, row *T) ([]change, error)
|
||||
// beforeRestore fixes up a snapshot whose references may have gone stale
|
||||
// while it sat in history.
|
||||
beforeRestore func(ctx context.Context, row *T) error
|
||||
// blockRemoval optionally refuses a removal, e.g. an object that has gained
|
||||
// plantings since. Returns a conflict reason, or "" to allow it.
|
||||
blockRemoval func(ctx context.Context, row *T) (string, error)
|
||||
@@ -445,6 +448,11 @@ func revertEntity[T any](
|
||||
if err := unsnapshot(r.Before, &target); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if ops.beforeRestore != nil {
|
||||
if err := ops.beforeRestore(ctx, &target); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
restored, err := ops.restore(ctx, &target)
|
||||
if err != nil {
|
||||
// The id may have been taken between the check above and this insert.
|
||||
@@ -547,6 +555,22 @@ func plantingRevertOps(s *Service) revertOps[domain.Planting] {
|
||||
get: s.store.GetPlanting,
|
||||
update: s.store.UpdatePlanting,
|
||||
restore: s.store.RestorePlanting,
|
||||
// A snapshot can name a seed lot that has since been deleted. The live
|
||||
// rows got their link nulled by ON DELETE SET NULL, but the snapshot
|
||||
// still holds the old id, and restoring it would violate the foreign key
|
||||
// and abort the whole revert. Drop the dangling link instead: the plant
|
||||
// really was in the ground, which is the part worth restoring.
|
||||
beforeRestore: func(ctx context.Context, p *domain.Planting) error {
|
||||
if p.SeedLotID == nil {
|
||||
return nil
|
||||
}
|
||||
if _, err := s.store.GetSeedLot(ctx, *p.SeedLotID); errors.Is(err, domain.ErrNotFound) {
|
||||
p.SeedLotID = nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
remove: func(ctx context.Context, p *domain.Planting) ([]change, error) {
|
||||
if err := s.store.DeletePlanting(ctx, p.ID); err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user