10000 fix: Add default created_by in templates and not null constraint by AbhineetJain · Pull Request #2290 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix: Add default created_by in templates and not null constraint #2290

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 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations 10000
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coderd/audit/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestDiff(t *testing.T) {
ActiveVersionID: uuid.UUID{3},
MaxTtl: int64(time.Hour),
MinAutostartInterval: int64(time.Minute),
CreatedBy: uuid.NullUUID{UUID: uuid.UUID{4}, Valid: true},
CreatedBy: uuid.UUID{4},
},
exp: audit.Map{
"id": uuid.UUID{1}.String(),
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/dump.sql

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

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE ONLY templates ALTER COLUMN created_by DROP NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
UPDATE
templates
SET
created_by = (
SELECT user_id FROM organization_members
WHERE organization_members.organization_id = templates.organization_id
ORDER BY created_at
LIMIT 1
)
WHERE
created_by IS NULL;


ALTER TABLE ONLY templates ALTER COLUMN created_by SET NOT NULL;
2 changes: 1 addition & 1 deletion coderd/database/models.go

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

2 changes: 1 addition & 1 deletion coderd/database/queries.sql.go

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

17 changes: 5 additions & 12 deletions coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
Description: createTemplate.Description,
MaxTtl: int64(maxTTL),
MinAutostartInterval: int64(minAutostartInterval),
CreatedBy: uuid.NullUUID{
UUID: apiKey.UserID,
Valid: true,
},
CreatedBy: apiKey.UserID,
})
if err != nil {
return xerrors.Errorf("insert template: %s", err)
Expand Down Expand Up @@ -453,15 +450,11 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
func getCreatedByNamesByTemplateIDs(ctx context.Context, db database.Store, templates []database.Template) (map[string]string, error) {
creators := make(map[string]string, len(templates))
for _, template := range templates {
if template.CreatedBy.Valid {
creator, err := db.GetUserByID(ctx, template.CreatedBy.UUID)
if err != nil {
return map[string]string{}, err
}
creators[template.ID.String()] = creator.Username
} else {
creators[template.ID.String()] = ""
creator, err := db.GetUserByID(ctx, template.CreatedBy)
if err != nil {
return map[string]string{}, err
}
creators[template.ID.String()] = creator.Username
}
return creators, nil
}
Expand Down
2 changes: 1 addition & 1 deletion codersdk/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Template struct {
Description string `json:"description"`
MaxTTLMillis int64 `json:"max_ttl_ms"`
MinAutostartIntervalMillis int64 `json:"min_autostart_interval_ms"`
CreatedByID uuid.NullUUID `json:"created_by_id"`
CreatedByID uuid.UUID `json:"created_by_id"`
CreatedByName string `json:"created_by_name"`
}

Expand Down
2 changes: 1 addition & 1 deletion site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export interface Template {
readonly description: string
readonly max_ttl_ms: number
readonly min_autostart_interval_ms: number
readonly created_by_id?: string
readonly created_by_id: string
readonly created_by_name: string
}

Expand Down
9 changes: 0 additions & 9 deletions site/src/components/TemplateStats/TemplateStats.stories.tsx
2364
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,3 @@ UsedByMany.args = {
},
activeVersion: Mocks.MockTemplateVersion,
}

export const UnknownCreator = Template.bind({})
UnknownCreator.args = {
template: {
...Mocks.MockTemplate,
created_by_name: "",
},
activeVersion: Mocks.MockTemplateVersion,
}
3 changes: 1 addition & 2 deletions
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const Language = {
userPlural: "users",
userSingular: "user",
createdByLabel: "Created by",
defaultTemplateCreator: "<unknown>",
}

export interface TemplateStatsProps {
Expand Down Expand Up @@ -50,7 +49,7 @@ export const TemplateStats: FC<TemplateStatsProps> = ({ template, activeVersion
<div className={styles.statsDivider} />
<div className={styles.statItem}>
<span className={styles.statsLabel}>{Language.createdByLabel}</span>
<span className={styles.statsValue}>{template.created_by_name || Language.defaultTemplateCreator}</span>
<span className={styles.statsValue}>{template.created_by_name}</span>
</div>
</div>
)
Expand Down
3 changes: 1 addition & 2 deletions site/src/pages/TemplatesPage/TemplatesPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export const Language = {
templateTooltipText: "With templates you can create a common configuration for your workspaces using Terraform.",
templateTooltipLink: "Manage templates",
createdByLabel: "Created by",
defaultTemplateCreator: "<unknown>",
}

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

<TableCell data-chromatic="ignore">{dayjs().to(dayjs(template.updated_at))}</TableCell>
<TableCell>{template.created_by_name || Language.defaultTemplateCreator}</TableCell>
<TableCell>{template.created_by_name}</TableCell>
<TableCell>
<div className={styles.arrowCell}>
<KeyboardArrowRight className={styles.arrowRight} />
Expand Down
0