Skip to content

Commit a91482c

Browse files
authored
fix: populate default created_by and add not-null constraint in templates (#2290)
1 parent 49f8578 commit a91482c

File tree

12 files changed

+28
-31
lines changed

12 files changed

+28
-31
lines changed

coderd/audit/diff_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestDiff(t *testing.T) {
8888
ActiveVersionID: uuid.UUID{3},
8989
MaxTtl: int64(time.Hour),
9090
MinAutostartInterval: int64(time.Minute),
91-
CreatedBy: uuid.NullUUID{UUID: uuid.UUID{4}, Valid: true},
91+
CreatedBy: uuid.UUID{4},
9292
},
9393
exp: audit.Map{
9494
"id": uuid.UUID{1}.String(),

coderd/database/dump.sql

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE ONLY templates ALTER COLUMN created_by DROP NOT NULL;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
UPDATE
2+
templates
3+
SET
4+
created_by = (
5+
SELECT user_id FROM organization_members
6+
WHERE organization_members.organization_id = templates.organization_id
7+
ORDER BY created_at
8+
LIMIT 1
9+
)
10+
WHERE
11+
created_by IS NULL;
12+
13+
14+
ALTER TABLE ONLY templates ALTER COLUMN created_by SET NOT NULL;

coderd/database/models.go

Lines changed: 1 addition & 1 deletion
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: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/templates.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
186186
Description: createTemplate.Description,
187187
MaxTtl: int64(maxTTL),
188188
MinAutostartInterval: int64(minAutostartInterval),
189-
CreatedBy: uuid.NullUUID{
190-
UUID: apiKey.UserID,
191-
Valid: true,
192-
},
189+
CreatedBy: apiKey.UserID,
193190
})
194191
if err != nil {
195192
return xerrors.Errorf("insert template: %s", err)
@@ -453,15 +450,11 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
453450
func getCreatedByNamesByTemplateIDs(ctx context.Context, db database.Store, templates []database.Template) (map[string]string, error) {
454451
creators := make(map[string]string, len(templates))
455452
for _, template := range templates {
456-
if template.CreatedBy.Valid {
457-
creator, err := db.GetUserByID(ctx, template.CreatedBy.UUID)
458-
if err != nil {
459-
return map[string]string{}, err
460-
}
461-
creators[template.ID.String()] = creator.Username
462-
} else {
463-
creators[template.ID.String()] = ""
453+
creator, err := db.GetUserByID(ctx, template.CreatedBy)
454+
if err != nil {
455+
return map[string]string{}, err
464456
}
457+
creators[template.ID.String()] = creator.Username
465458
}
466459
return creators, nil
467460
}

codersdk/templates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Template struct {
2525
Description string `json:"description"`
2626
MaxTTLMillis int64 `json:"max_ttl_ms"`
2727
MinAutostartIntervalMillis int64 `json:"min_autostart_interval_ms"`
28-
CreatedByID uuid.NullUUID `json:"created_by_id"`
28+
CreatedByID uuid.UUID `json:"created_by_id"`
2929
CreatedByName string `json:"created_by_name"`
3030
}
3131

site/src/api/typesGenerated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export interface Template {
247247
readonly description: string
248248
readonly max_ttl_ms: number
249249
readonly min_autostart_interval_ms: number
250-
readonly created_by_id?: string
250+
readonly created_by_id: string
251251
readonly created_by_name: string
252252
}
253253

site/src/components/TemplateStats/TemplateStats.stories.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,3 @@ UsedByMany.args = {
2323
},
2424
activeVersion: Mocks.MockTemplateVersion,
2525
}
26-
27-
export const UnknownCreator = Template.bind({})
28-
UnknownCreator.args = {
29-
template: {
30-
...Mocks.MockTemplate,
31-
created_by_name: "",
32-
},
33-
activeVersion: Mocks.MockTemplateVersion,
34-
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy