Skip to content

Commit a91482c

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

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-
}

site/src/components/TemplateStats/TemplateStats.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const Language = {
1414
userPlural: "users",
1515
userSingular: "user",
1616
createdByLabel: "Created by",
17-
defaultTemplateCreator: "<unknown>",
1817
}
1918

2019
export interface TemplateStatsProps {
@@ -50,7 +49,7 @@ export const TemplateStats: FC<TemplateStatsProps> = ({ template, activeVersion
5049
<div className={styles.statsDivider} />
5150
<div className={styles.statItem}>
5251
<span className={styles.statsLabel}>{Language.createdByLabel}</span>
53-
<span className={styles.statsValue}>{template.created_by_name || Language.defaultTemplateCreator}</span>
52+
<span className={styles.statsValue}>{template.created_by_name}</span>
5453
</div>
5554
</div>
5655
)

site/src/pages/TemplatesPage/TemplatesPageView.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ export const Language = {
5050
templateTooltipText: "With templates you can create a common configuration for your workspaces using Terraform.",
5151
templateTooltipLink: "Manage templates",
5252
createdByLabel: "Created by",
53-
defaultTemplateCreator: "<unknown>",
5453
}
5554

5655
const TemplateHelpTooltip: React.FC = () => {
@@ -140,7 +139,7 @@ export const TemplatesPageView: FC<TemplatesPageViewProps> = (props) => {
140139
<TableCell>{Language.developerCount(template.workspace_owner_count)}</TableCell>
141140

142141
<TableCell data-chromatic="ignore">{dayjs().to(dayjs(template.updated_at))}</TableCell>
143-
<TableCell>{template.created_by_name || Language.defaultTemplateCreator}</TableCell>
142+
<TableCell>{template.created_by_name}</TableCell>
144143
<TableCell>
145144
<div className={styles.arrowCell}>
146145
<KeyboardArrowRight className={styles.arrowRight} />

0 commit comments

Comments
 (0)