fix: Sort workspace by name by created_at (#2214)

* fix: Sort workspace by name by created_at

Fix bug where deleting workspaces with the same name returns the
oldest deleted workspace
This commit is contained in:
Steven Masley
2022-06-10 09:58:42 -05:00
committed by GitHub
parent 953e8c8fe6
commit 6bee180bb3
10 changed files with 60 additions and 29 deletions

View File

@ -375,7 +375,9 @@ func (q *fakeQuerier) GetWorkspaceByOwnerIDAndName(_ context.Context, arg databa
q.mutex.RLock()
defer q.mutex.RUnlock()
var found *database.Workspace
for _, workspace := range q.workspaces {
workspace := workspace
if workspace.OwnerID != arg.OwnerID {
continue
}
@ -385,7 +387,14 @@ func (q *fakeQuerier) GetWorkspaceByOwnerIDAndName(_ context.Context, arg databa
if workspace.Deleted != arg.Deleted {
continue
}
return workspace, nil
// Return the most recent workspace with the given name
if found == nil || workspace.CreatedAt.After(found.CreatedAt) {
found = &workspace
}
}
if found != nil {
return *found, nil
}
return database.Workspace{}, sql.ErrNoRows
}

View File

@ -3480,6 +3480,7 @@ WHERE
owner_id = $1
AND deleted = $2
AND LOWER("name") = LOWER($3)
ORDER BY created_at DESC
`
type GetWorkspaceByOwnerIDAndNameParams struct {

View File

@ -70,7 +70,8 @@ FROM
WHERE
owner_id = @owner_id
AND deleted = @deleted
AND LOWER("name") = LOWER(@name);
AND LOWER("name") = LOWER(@name)
ORDER BY created_at DESC;
-- name: GetWorkspaceOwnerCountsByTemplateIDs :many
SELECT