Skip to content

fix: Order database queries for templates #3249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,8 @@ func (q *fakeQuerier) GetLatestWorkspaceBuildsByWorkspaceIDs(_ context.Context,
}

func (q *fakeQuerier) GetWorkspaceBuildByWorkspaceID(_ context.Context,
params database.GetWorkspaceBuildByWorkspaceIDParams) ([]database.WorkspaceBuild, error) {
params database.GetWorkspaceBuildByWorkspaceIDParams,
) ([]database.WorkspaceBuild, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

Expand Down Expand Up @@ -893,6 +894,12 @@ func (q *fakeQuerier) GetTemplatesWithFilter(_ context.Context, arg database.Get
templates = append(templates, template)
}
if len(templates) > 0 {
slices.SortFunc(templates, func(i, j database.Template) bool {
if !i.CreatedAt.Before(j.CreatedAt) {
return false
}
return i.ID.String() < j.ID.String()
})
return templates, nil
}

Expand Down Expand Up @@ -1069,7 +1076,15 @@ func (q *fakeQuerier) GetTemplates(_ context.Context) ([]database.Template, erro
q.mutex.RLock()
defer q.mutex.RUnlock()

return q.templates[:], nil
templates := slices.Clone(q.templates)
slices.SortFunc(templates, func(i, j database.Template) bool {
if !i.CreatedAt.Before(j.CreatedAt) {
return false
}
return i.ID.String() < j.ID.String()
})

return templates, nil
}

func (q *fakeQuerier) GetOrganizationMemberByUserID(_ context.Context, arg database.GetOrganizationMemberByUserIDParams) (database.OrganizationMember, error) {
Expand Down
2 changes: 2 additions & 0 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion coderd/database/queries/templates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ WHERE
id = ANY(@ids)
ELSE true
END
ORDER BY (created_at, id) ASC
;

-- name: GetTemplateByOrganizationAndName :one
Expand All @@ -49,7 +50,9 @@ LIMIT
1;

-- name: GetTemplates :many
SELECT * FROM templates;
SELECT * FROM templates
ORDER BY (created_at, id) ASC
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't relevant for the test failure, but a bonus fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the fake db need this too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing not, since they'll be appended to a slice. But I can add it there as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, we should explicitly sort to match the sql imo. I do this with a copied slice somewhere else in the fake db where ordering matters.

Copy link
Member Author

@mafredri mafredri Jul 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c0498a9, nevermind... f8df8ef, nevermind... 97e0667 (sort functions :smh:).

;

-- name: InsertTemplate :one
INSERT INTO
Expand Down