Skip to content

Commit d1c6319

Browse files
committed
Remove excessive calls, use filters on a single query
1 parent f3123fe commit d1c6319

File tree

7 files changed

+103
-213
lines changed

7 files changed

+103
-213
lines changed

coderd/database/databasefake/databasefake.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ func (q *fakeQuerier) GetWorkspacesWithFilter(_ context.Context, arg database.Ge
334334
}
335335
}
336336
if arg.TemplateName != "" {
337-
templates, err := q.GetTemplatesByName(context.Background(), database.GetTemplatesByNameParams{
338-
Name: arg.TemplateName,
337+
templates, err := q.GetTemplatesWithFilter(context.Background(), database.GetTemplatesWithFilterParams{
338+
ExactName: arg.TemplateName,
339339
})
340340
// Add to later param
341341
if err == nil {
@@ -799,17 +799,39 @@ func (q *fakeQuerier) UpdateTemplateMetaByID(_ context.Context, arg database.Upd
799799
return sql.ErrNoRows
800800
}
801801

802-
func (q *fakeQuerier) GetTemplatesByName(_ context.Context, arg database.GetTemplatesByNameParams) ([]database.Template, error) {
802+
func (q *fakeQuerier) GetTemplatesWithFilter(_ context.Context, arg database.GetTemplatesWithFilterParams) ([]database.Template, error) {
803803
q.mutex.RLock()
804804
defer q.mutex.RUnlock()
805+
805806
var templates []database.Template
806807
for _, template := range q.templates {
807-
if !strings.EqualFold(template.Name, arg.Name) {
808+
if template.Deleted != arg.Deleted {
808809
continue
809810
}
810-
if template.Deleted != arg.Deleted {
811+
if arg.OrganizationID != uuid.Nil && template.OrganizationID != arg.OrganizationID {
812+
continue
813+
}
814+
815+
if arg.Name != "" && !strings.Contains(template.Name, arg.Name) {
816+
continue
817+
}
818+
819+
if arg.ExactName != "" && !strings.EqualFold(template.Name, arg.ExactName) {
811820
continue
812821
}
822+
823+
if len(arg.Ids) > 0 {
824+
match := false
825+
for _, id := range arg.Ids {
826+
if template.ID == id {
827+
match = true
828+
break
829+
}
830+
}
831+
if !match {
832+
continue
833+
}
834+
}
813835
templates = append(templates, template)
814836
}
815837
if len(templates) > 0 {
@@ -956,26 +978,6 @@ func (q *fakeQuerier) GetParameterValueByScopeAndName(_ context.Context, arg dat
956978
return database.ParameterValue{}, sql.ErrNoRows
957979
}
958980

959-
func (q *fakeQuerier) GetTemplatesByOrganization(_ context.Context, arg database.GetTemplatesByOrganizationParams) ([]database.Template, error) {
960-
q.mutex.RLock()
961-
defer q.mutex.RUnlock()
962-
963-
templates := make([]database.Template, 0)
964-
for _, template := range q.templates {
965-
if template.Deleted != arg.Deleted {
966-
continue
967-
}
968-
if template.OrganizationID != arg.OrganizationID {
969-
continue
970-
}
971-
templates = append(templates, template)
972-
}
973-
if len(templates) == 0 {
974-
return nil, sql.ErrNoRows
975-
}
976-
return templates, nil
977-
}
978-
979981
func (q *fakeQuerier) GetTemplatesByIDs(_ context.Context, ids []uuid.UUID) ([]database.Template, error) {
980982
q.mutex.RLock()
981983
defer q.mutex.RUnlock()

coderd/database/querier.go

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 41 additions & 151 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/templates.sql

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,39 @@ WHERE
88
LIMIT
99
1;
1010

11-
-- name: GetTemplatesByIDs :many
11+
-- name: GetTemplatesWithFilter :many
1212
SELECT
1313
*
1414
FROM
1515
templates
1616
WHERE
17-
id = ANY(@ids :: uuid [ ]);
17+
-- Optionally include deleted templates
18+
templates.deleted = @deleted
19+
-- Filter by organization_id
20+
AND CASE
21+
WHEN @organization_id :: uuid != '00000000-00000000-00000000-00000000' THEN
22+
organization_id = @organization_id
23+
ELSE true
24+
END
25+
-- Filter by name, matching on substring
26+
AND CASE
27+
WHEN @name :: text != '' THEN
28+
LOWER(name) LIKE '%' || LOWER(@name) || '%'
29+
ELSE true
30+
END
31+
-- Filter by exact name
32+
AND CASE
33+
WHEN @exact_name :: text != '' THEN
34+
LOWER("name") = LOWER(@exact_name)
35+
ELSE true
36+
END
37+
-- Filter by ids
38+
AND CASE
39+
WHEN array_length(@ids :: uuid[], 1) > 0 THEN
40+
id = ANY(@ids)
41+
ELSE true
42+
END
43+
;
1844

1945
-- name: GetTemplateByOrganizationAndName :one
2046
SELECT
@@ -28,24 +54,6 @@ WHERE
2854
LIMIT
2955
1;
3056

31-
-- name: GetTemplatesByName :many
32-
SELECT
33-
*
34-
FROM
35-
templates
36-
WHERE
37-
deleted = @deleted
38-
AND LOWER("name") = LOWER(@name);
39-
40-
-- name: GetTemplatesByOrganization :many
41-
SELECT
42-
*
43-
FROM
44-
templates
45-
WHERE
46-
organization_id = $1
47-
AND deleted = $2;
48-
4957
-- name: InsertTemplate :one
5058
INSERT INTO
5159
templates (

coderd/database/queries/workspaces.sql

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,6 @@ AND
7373
(ttl IS NOT NULL AND ttl > 0)
7474
);
7575

76-
-- name: GetWorkspacesByTemplateID :many
77-
SELECT
78-
*
79-
FROM
80-
workspaces
81-
WHERE
82-
template_id = $1
83-
AND deleted = $2;
84-
8576
-- name: GetWorkspaceByOwnerIDAndName :one
8677
SELECT
8778
*

0 commit comments

Comments
 (0)