Skip to content

Commit

Permalink
✨ Unwanted seeded targets removed. (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
jortel authored Dec 20, 2024
1 parent fd98244 commit dd398d1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
43 changes: 40 additions & 3 deletions seed/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"container/list"
"errors"
"fmt"
"slices"

liberr "github.com/jortel/go-utils/error"
"github.com/konveyor/tackle2-hub/model"
Expand Down Expand Up @@ -97,13 +98,47 @@ 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
}
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) {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion seed/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}

0 comments on commit dd398d1

Please sign in to comment.