diff --git a/seed/target.go b/seed/target.go index 9f665ee8d..9c0ce4e14 100644 --- a/seed/target.go +++ b/seed/target.go @@ -4,6 +4,7 @@ import ( "container/list" "errors" "fmt" + "slices" liberr "github.com/jortel/go-utils/error" "github.com/konveyor/tackle2-hub/model" @@ -97,6 +98,11 @@ func (r *Target) Apply(db *gorm.DB) (err error) { seedIds = append(seedIds, target.ID) } + err = r.deleteUnwanted(db) + if err != nil { + return + } + err = r.reorder(db, seedIds) if err != nil { return @@ -104,6 +110,35 @@ func (r *Target) Apply(db *gorm.DB) (err error) { return } +// deleteUnwanted deletes targets with a UUID not found +// in the set of seeded targets. +func (r *Target) deleteUnwanted(db *gorm.DB) (err error) { + var found []*model.Target + err = db.Find(&found).Error + if err != nil { + err = liberr.Wrap(err) + return + } + wanted := make(map[string]byte) + for _, t := range r.targets { + wanted[t.UUID] = 0 + } + for _, target := range found { + if target.UUID == nil { + continue + } + if _, found := wanted[*target.UUID]; found { + continue + } + err = db.Delete(target).Error + if err != nil { + err = liberr.Wrap(err) + return + } + } + return +} + // reorder updates the value of the ui.target.order setting // to add any missing target ids. (namely, newly added targets.) func (r *Target) reorder(db *gorm.DB, seedIds []uint) (err error) { @@ -126,7 +161,7 @@ func (r *Target) reorder(db *gorm.DB, seedIds []uint) (err error) { } userOrder := []uint{} _ = s.As(&userOrder) - s.Value = merge(userOrder, seedIds, targetIds) + s.Value = r.merge(userOrder, seedIds, targetIds) result = db.Where("key", UITargetOrder).Updates(s) if result.Error != nil { @@ -142,11 +177,13 @@ func (r *Target) reorder(db *gorm.DB, seedIds []uint) (err error) { // userOrder: slice of target IDs in the user's desired order // seedOrder: slice of target IDs in seedfile order // ids: slice of ids of all the targets in the DB -func merge(userOrder []uint, seedOrder []uint, ids []uint) (mergedOrder []uint) { +func (r *Target) merge(userOrder []uint, seedOrder []uint, ids []uint) (mergedOrder []uint) { ll := list.New() known := make(map[uint]*list.Element) for _, id := range userOrder { - known[id] = ll.PushBack(id) + if slices.Contains(ids, id) { + known[id] = ll.PushBack(id) + } } for i, id := range seedOrder { if _, found := known[id]; found { diff --git a/seed/target_test.go b/seed/target_test.go index 0505b55d5..d46a8bdc7 100644 --- a/seed/target_test.go +++ b/seed/target_test.go @@ -9,6 +9,8 @@ import ( func TestMerge(t *testing.T) { g := gomega.NewWithT(t) + seeder := Target{} + // the seed contains 10 targets in a given order, 3 of which are new seedOrder := []uint{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} // the user has set up a custom order for the 7 targets that already exist in the db @@ -20,6 +22,6 @@ func TestMerge(t *testing.T) { // that had previously been dropped on the floor being added to the end of the ordering. expectedOrder := []uint{6, 7, 8, 9, 10, 5, 4, 1, 3, 2, 11, 12, 13} - mergedOrder := merge(userOrder, seedOrder, allIds) + mergedOrder := seeder.merge(userOrder, seedOrder, allIds) g.Expect(mergedOrder).To(gomega.Equal(expectedOrder)) }