Skip to content

Commit ef081b3

Browse files
committed
Merge branch '135-sorted-clone-list' into 'master'
feat: return the list of clones descend ordered by creation time (the newest is the 1st) (#135) Closes #135 See merge request postgres-ai/database-lab!122
2 parents 9758193 + a2dfa94 commit ef081b3

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

pkg/services/cloning/mode_base.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"database/sql"
1010
"fmt"
11+
"sort"
1112
"strconv"
1213
"strings"
1314
"sync"
@@ -336,7 +337,7 @@ func (c *baseCloning) GetSnapshots() ([]models.Snapshot, error) {
336337
return snapshots, nil
337338
}
338339

339-
// GetClones returns all clones.
340+
// GetClones returns the list of clones descend ordered by creation time.
340341
func (c *baseCloning) GetClones() []*models.Clone {
341342
clones := make([]*models.Clone, 0, c.lenClones())
342343

@@ -346,6 +347,10 @@ func (c *baseCloning) GetClones() []*models.Clone {
346347
}
347348
c.cloneMutex.RUnlock()
348349

350+
sort.Slice(clones, func(i, j int) bool {
351+
return clones[i].CreatedAt > clones[j].CreatedAt
352+
})
353+
349354
return clones
350355
}
351356

pkg/services/cloning/mode_base_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ func (s *BaseCloningSuite) TestFindWrapper() {
4747
assert.Equal(s.T(), CloneWrapper{clone: &models.Clone{ID: "testCloneID"}}, *wrapper)
4848
}
4949

50+
func (s *BaseCloningSuite) TestCloneList() {
51+
clone1 := &models.Clone{CreatedAt: "2020-02-20 01:23:45 UTC"}
52+
clone2 := &models.Clone{CreatedAt: "2020-06-23 10:31:27 UTC"}
53+
clone3 := &models.Clone{CreatedAt: "2020-05-20 00:43:21 UTC"}
54+
55+
s.cloning.setWrapper("testCloneID1", &CloneWrapper{clone: clone1})
56+
s.cloning.setWrapper("testCloneID2", &CloneWrapper{clone: clone2})
57+
s.cloning.setWrapper("testCloneID3", &CloneWrapper{clone: clone3})
58+
59+
list := s.cloning.GetClones()
60+
61+
assert.Equal(s.T(), 3, len(list))
62+
63+
// Check clone order.
64+
assert.Equal(s.T(), []*models.Clone{
65+
{CreatedAt: "2020-06-23 10:31:27 UTC"},
66+
{CreatedAt: "2020-05-20 00:43:21 UTC"},
67+
{CreatedAt: "2020-02-20 01:23:45 UTC"},
68+
}, list)
69+
}
70+
5071
func (s *BaseCloningSuite) TestUpdateStatus() {
5172
s.cloning.setWrapper("testCloneID", &CloneWrapper{clone: &models.Clone{Status: models.Status{
5273
Code: models.StatusCreating,

0 commit comments

Comments
 (0)