fix(clientcache): sort target names case insensitively (#6428)

* change targets sorted by name to sort case insensitive

* remove print

* switch to collate
This commit is contained in:
Emilia authored and GitHub committed 2026-02-16 12:17:47 -05:00
1 parent 4a7bef3939
commit 66a0e08112
2 files changed
+32 -6

No files matched your search

+12 -1
View File
@@ -379,7 +379,18 @@ func (r *Repository) searchTargets(ctx context.Context, condition string, search
if sd != Ascending && sd != Descending {
sd = Ascending
}
orderClause := fmt.Sprintf("%s %s", opts.withSortBy, sd)
// Sorting by name requires doing case insensitive, then tie-breaking with case sensitive to put names which were
// originally capitalized first. To avoid weird behavior when adding new columns, break sort by name's case
// out separately.
orderClause := ""
switch opts.withSortBy {
case "name":
orderClause = fmt.Sprintf("%s collate nocase %s, %s %s", opts.withSortBy, sd, opts.withSortBy, sd)
default:
orderClause = fmt.Sprintf("%s %s", opts.withSortBy, sd)
}
dbOpts = append(dbOpts, db.WithOrder(orderClause))
}
@@ -340,9 +340,11 @@ func TestRepository_ListTargets(t *testing.T) {
})
ts := []*targets.Target{
target("1"),
target("2"),
target("3"),
target("TARGET 1"),
target("Target 1"),
target("TaRgEt 2"),
target("Target 2"),
target("Target 3"),
}
require.NoError(t, r.refreshTargets(ctx, u1, map[AuthToken]string{{Id: "id"}: "something"},
WithTargetRetrievalFunc(testTargetStaticResourceRetrievalFunc(testStaticResourceRetrievalFunc(t, [][]*targets.Target{ts}, [][]string{nil})))))
@@ -362,9 +364,22 @@ func TestRepository_ListTargets(t *testing.T) {
t.Run("withSortBy sorts targets", func(t *testing.T) {
l, err := r.ListTargets(ctx, kt1.AuthTokenId, WithSort(SortByName, Descending, []SortBy{SortByName}))
assert.NoError(t, err)
assert.Equal(t, ts[2].Name, l.Targets[0].Name)
assert.Equal(t, ts[4].Name, l.Targets[0].Name)
assert.Equal(t, ts[3].Name, l.Targets[1].Name)
assert.Equal(t, ts[2].Name, l.Targets[2].Name)
assert.Equal(t, ts[1].Name, l.Targets[3].Name)
assert.Equal(t, ts[0].Name, l.Targets[4].Name)
})
t.Run("withSortBy Ascending sorts targets ascending order", func(t *testing.T) {
l, err := r.ListTargets(ctx, kt1.AuthTokenId, WithSort(SortByName, Ascending, []SortBy{SortByName}))
assert.NoError(t, err)
assert.Equal(t, ts[4].Name, l.Targets[4].Name)
assert.Equal(t, ts[3].Name, l.Targets[3].Name)
assert.Equal(t, ts[2].Name, l.Targets[2].Name)
assert.Equal(t, ts[1].Name, l.Targets[1].Name)
assert.Equal(t, ts[0].Name, l.Targets[2].Name)
assert.Equal(t, ts[0].Name, l.Targets[0].Name)
})
t.Run("withSortBy bad SortBy errors", func(t *testing.T) {