Skip to content

Commit 70c7ea9

Browse files
committed
use a sql domain as a type alias so that we can override it as a StringMap in sqlc
1 parent a173bfb commit 70c7ea9

File tree

8 files changed

+24
-31
lines changed

8 files changed

+24
-31
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3635,12 +3635,7 @@ func (q *FakeQuerier) GetProvisionerDaemonsByOrganization(_ context.Context, arg
36353635
continue
36363636
}
36373637
if arg.Tags != nil {
3638-
var argTags map[string]string
3639-
err := json.Unmarshal(arg.Tags, &argTags)
3640-
if err != nil {
3641-
continue
3642-
}
3643-
for k, v := range argTags {
3638+
for k, v := range arg.Tags {
36443639
if t, found := daemon.Tags[k]; !found || t != v {
36453640
continue
36463641
}

coderd/database/dump.sql

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
CREATE OR REPLACE FUNCTION tags_compatible(subset_tags jsonb, superset_tags jsonb)
1+
CREATE DOMAIN tags AS jsonb;
2+
3+
CREATE OR REPLACE FUNCTION tags_compatible(subset_tags tags, superset_tags tags)
24
RETURNS boolean as $$
35
BEGIN
46
RETURN CASE
5-
WHEN superset_tags = '{"scope": "organization", "owner": ""}' :: jsonb
7+
WHEN superset_tags :: jsonb = '{"scope": "organization", "owner": ""}' :: jsonb
68
THEN subset_tags = superset_tags
7-
ELSE subset_tags <@ superset_tags
9+
ELSE subset_tags :: jsonb <@ superset_tags :: jsonb
810
END;
911
END;
1012
$$ LANGUAGE plpgsql;

coderd/database/queries.sql.go

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

coderd/database/queries/provisionerdaemons.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ WHERE
1414
organization_id = @organization_id :: uuid
1515
AND
1616
-- adding support for searching by tags:
17-
(@tags :: jsonb = '' :: jsonb OR tags_compatible(@tags :: jsonb, provisioner_daemons.tags :: jsonb));
17+
(@tags :: tags = '{}' :: tags OR tags_compatible(@tags::tags, provisioner_daemons.tags::tags));
1818

1919
-- name: DeleteOldProvisionerDaemons :exec
2020
-- Delete provisioner daemons that have been created at least a week ago

coderd/database/queries/provisionerjobs.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ WHERE
2222
AND nested.organization_id = @organization_id
2323
-- Ensure the caller has the correct provisioner.
2424
AND nested.provisioner = ANY(@types :: provisioner_type [ ])
25-
AND tags_compatible(@tags, nested.tags)
25+
AND tags_compatible(@tags :: jsonb, nested.tags)
2626
ORDER BY
2727
nested.created_at
2828
FOR UPDATE

coderd/database/sqlc.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ sql:
3535
- db_type: "name_organization_pair"
3636
go_type:
3737
type: "NameOrganizationPair"
38+
- db_type: "tags"
39+
go_type:
40+
type: "StringMap"
3841
- column: "custom_roles.site_permissions"
3942
go_type:
4043
type: "CustomRolePermissions"

enterprise/coderd/provisionerdaemons.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package coderd
33
import (
44
"context"
55
"database/sql"
6-
"encoding/json"
76
"fmt"
87
"io"
98
"net/http"
@@ -64,15 +63,7 @@ func (api *API) provisionerDaemons(rw http.ResponseWriter, r *http.Request) {
6463
ctx := r.Context()
6564
org := httpmw.OrganizationParam(r)
6665

67-
tags, err := provisionerTags(r)
68-
if err != nil {
69-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
70-
Message: "Internal error reading tags.",
71-
Detail: err.Error(),
72-
})
73-
return
74-
}
75-
66+
tags := provisionerTags(r)
7667
daemons, err := api.Database.GetProvisionerDaemonsByOrganization(
7768
ctx,
7869
database.GetProvisionerDaemonsByOrganizationParams{
@@ -91,10 +82,10 @@ func (api *API) provisionerDaemons(rw http.ResponseWriter, r *http.Request) {
9182
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.List(daemons, db2sdk.ProvisionerDaemon))
9283
}
9384

94-
func provisionerTags(r *http.Request) ([]byte, error) {
85+
func provisionerTags(r *http.Request) map[string]string {
9586
tags := r.URL.Query()["tags"]
9687
if len(tags) == 0 {
97-
return []byte{}, nil
88+
return nil
9889
}
9990

10091
tagMap := map[string]string{}
@@ -106,7 +97,7 @@ func provisionerTags(r *http.Request) ([]byte, error) {
10697
}
10798
}
10899

109-
return json.Marshal(tagMap)
100+
return tagMap
110101
}
111102

112103
type provisiionerDaemonAuthResponse struct {

0 commit comments

Comments
 (0)