Skip to content

Commit

Permalink
Make listIndexes return a sorted list (#3602)
Browse files Browse the repository at this point in the history
Closes #3589.
  • Loading branch information
codenoid authored Oct 20, 2023
1 parent 19c361a commit 05a2ee3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
6 changes: 3 additions & 3 deletions integration/indexes_command_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func TestDropIndexesCommandCompat(tt *testing.T) {
targetList := FetchAll(t, ctx, targetCursor)
compatList := FetchAll(t, ctx, compatCursor)

require.Equal(t, compatList, targetList)
require.ElementsMatch(t, compatList, targetList)
}

targetCommand := bson.D{
Expand Down Expand Up @@ -451,7 +451,7 @@ func TestDropIndexesCommandCompat(tt *testing.T) {
require.Nil(t, compatRes)
}

require.Equal(t, compatRes, targetRes)
require.ElementsMatch(t, compatRes, targetRes)

if compatErr == nil {
nonEmptyResults = true
Expand All @@ -474,7 +474,7 @@ func TestDropIndexesCommandCompat(tt *testing.T) {
targetList := FetchAll(t, ctx, targetCursor)
compatList := FetchAll(t, ctx, compatCursor)

assert.Equal(t, compatList, targetList)
assert.ElementsMatch(t, compatList, targetList)
})
}

Expand Down
4 changes: 2 additions & 2 deletions integration/indexes_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func TestCreateIndexesCompat(tt *testing.T) {
targetIndexes := FetchAll(t, ctx, targetCursor)
compatIndexes := FetchAll(t, ctx, compatCursor)

assert.Equal(t, compatIndexes, targetIndexes)
assert.ElementsMatch(t, compatIndexes, targetIndexes)

// List specifications to check they are identical after creation.
targetSpec, targetErr := targetCollection.Indexes().ListSpecifications(ctx)
Expand All @@ -295,7 +295,7 @@ func TestCreateIndexesCompat(tt *testing.T) {
require.NoError(t, targetErr)

require.NotEmpty(t, compatSpec)
assert.Equal(t, compatSpec, targetSpec)
assert.ElementsMatch(t, compatSpec, targetSpec)
})
}

Expand Down
13 changes: 7 additions & 6 deletions internal/backends/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package backends

import (
"cmp"
"context"
"slices"
"time"

"github.com/FerretDB/FerretDB/internal/types"
Expand Down Expand Up @@ -319,12 +321,11 @@ func (cc *collectionContract) ListIndexes(ctx context.Context, params *ListIndex
res, err := cc.c.ListIndexes(ctx, params)
checkError(err, ErrorCodeCollectionDoesNotExist)

// TODO https://github.com/FerretDB/FerretDB/issues/3589
// if res != nil && len(res.Indexes) > 0 {
// must.BeTrue(slices.IsSortedFunc(res.Indexes, func(a, b IndexInfo) int {
// return cmp.Compare(a.Name, b.Name)
// }))
// }
if res != nil && len(res.Indexes) > 0 {
must.BeTrue(slices.IsSortedFunc(res.Indexes, func(a, b IndexInfo) int {
return cmp.Compare(a.Name, b.Name)
}))
}

return res, err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/backends/postgresql/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"sort"
"strings"

"github.com/jackc/pgerrcode"
Expand Down Expand Up @@ -527,10 +528,9 @@ func (c *collection) ListIndexes(ctx context.Context, params *backends.ListIndex
}
}

// TODO https://github.com/FerretDB/FerretDB/issues/3589
// slices.SortFunc(res.Indexes, func(a, b backends.IndexInfo) int {
// return cmp.Compare(a.Name, b.Name)
// })
sort.Slice(res.Indexes, func(i, j int) bool {
return res.Indexes[i].Name < res.Indexes[j].Name
})

return &res, nil
}
Expand Down
8 changes: 4 additions & 4 deletions internal/backends/sqlite/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"sort"
"strings"

sqlite3 "modernc.org/sqlite"
Expand Down Expand Up @@ -468,10 +469,9 @@ func (c *collection) ListIndexes(ctx context.Context, params *backends.ListIndex
}
}

// TODO https://github.com/FerretDB/FerretDB/issues/3589
// slices.SortFunc(res.Indexes, func(a, b backends.IndexInfo) int {
// return cmp.Compare(a.Name, b.Name)
// })
sort.Slice(res.Indexes, func(i, j int) bool {
return res.Indexes[i].Name < res.Indexes[j].Name
})

return &res, nil
}
Expand Down

0 comments on commit 05a2ee3

Please sign in to comment.