From f8668bcf30d1ca9ebf40d32f54a8907206be4985 Mon Sep 17 00:00:00 2001 From: Abhineet Jain Date: Mon, 13 Jun 2022 17:14:36 +0000 Subject: [PATCH] add default created_by in templates and not null constraint --- coderd/audit/diff_test.go | 2 +- coderd/database/dump.sql | 2 +- ...000023_template_created_by_not_null.down.sql | 1 + .../000023_template_created_by_not_null.up.sql | 14 ++++++++++++++ coderd/database/models.go | 2 +- coderd/database/queries.sql.go | 2 +- coderd/templates.go | 17 +++++------------ codersdk/templates.go | 2 +- site/src/api/typesGenerated.ts | 2 +- .../TemplateStats/TemplateStats.stories.tsx | 9 --------- .../components/TemplateStats/TemplateStats.tsx | 3 +-- .../pages/TemplatesPage/TemplatesPageView.tsx | 3 +-- 12 files changed, 28 insertions(+), 31 deletions(-) create mode 100644 coderd/database/migrations/000023_template_created_by_not_null.down.sql create mode 100644 coderd/database/migrations/000023_template_created_by_not_null.up.sql diff --git a/coderd/audit/diff_test.go b/coderd/audit/diff_test.go index ba91692a0734f..93e0d1bc33824 100644 --- a/coderd/audit/diff_test.go +++ b/coderd/audit/diff_test.go @@ -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(), diff --git a/coderd/database/dump.sql b/coderd/database/dump.sql index 95ef9ff0df3d6..0a795c29bea94 100644 --- a/coderd/database/dump.sql +++ b/coderd/database/dump.sql @@ -249,7 +249,7 @@ CREATE TABLE templates ( description character varying(128) DEFAULT ''::character varying NOT NULL, max_ttl bigint DEFAULT '604800000000000'::bigint NOT NULL, min_autostart_interval bigint DEFAULT '3600000000000'::bigint NOT NULL, - created_by uuid + created_by uuid NOT NULL ); CREATE TABLE users ( diff --git a/coderd/database/migrations/000023_template_created_by_not_null.down.sql b/coderd/database/migrations/000023_template_created_by_not_null.down.sql new file mode 100644 index 0000000000000..2aeab702c0aa5 --- /dev/null +++ b/coderd/database/migrations/000023_template_created_by_not_null.down.sql @@ -0,0 +1 @@ +ALTER TABLE ONLY templates ALTER COLUMN created_by DROP NOT NULL; diff --git a/coderd/database/migrations/000023_template_created_by_not_null.up.sql b/coderd/database/migrations/000023_template_created_by_not_null.up.sql new file mode 100644 index 0000000000000..73ccf7a89b2a5 --- /dev/null +++ b/coderd/database/migrations/000023_template_created_by_not_null.up.sql @@ -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; diff --git a/coderd/database/models.go b/coderd/database/models.go index 2454049da0ac0..6527fdc5bc8b8 100644 --- a/coderd/database/models.go +++ b/coderd/database/models.go @@ -438,7 +438,7 @@ type Template struct { Description string `db:"description" json:"description"` MaxTtl int64 `db:"max_ttl" json:"max_ttl"` MinAutostartInterval int64 `db:"min_autostart_interval" json:"min_autostart_interval"` - CreatedBy uuid.NullUUID `db:"created_by" json:"created_by"` + CreatedBy uuid.UUID `db:"created_by" json:"created_by"` } type TemplateVersion struct { diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index edb2fc774589c..38afa4aa40694 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -1797,7 +1797,7 @@ type InsertTemplateParams struct { Description string `db:"description" json:"description"` MaxTtl int64 `db:"max_ttl" json:"max_ttl"` MinAutostartInterval int64 `db:"min_autostart_interval" json:"min_autostart_interval"` - CreatedBy uuid.NullUUID `db:"created_by" json:"created_by"` + CreatedBy uuid.UUID `db:"created_by" json:"created_by"` } func (q *sqlQuerier) InsertTemplate(ctx context.Context, arg InsertTemplateParams) (Template, error) { diff --git a/coderd/templates.go b/coderd/templates.go index d79bd19f70fa2..c24ccf544465e 100644 --- a/coderd/templates.go +++ b/coderd/templates.go @@ -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) @@ -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 } diff --git a/codersdk/templates.go b/codersdk/templates.go index 31f1ea97d9b59..266de9305c905 100644 --- a/codersdk/templates.go +++ b/codersdk/templates.go @@ -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"` } diff --git a/site/src/api/typesGenerated.ts b/site/src/api/typesGenerated.ts index 79423268ac6a2..49ae60058a061 100644 --- a/site/src/api/typesGenerated.ts +++ b/site/src/api/typesGenerated.ts @@ -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 } diff --git a/site/src/components/TemplateStats/TemplateStats.stories.tsx b/site/src/components/TemplateStats/TemplateStats.stories.tsx index fb7f36d894652..3056187d110d4 100644 --- a/site/src/components/TemplateStats/TemplateStats.stories.tsx +++ b/site/src/components/TemplateStats/TemplateStats.stories.tsx @@ -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, -} diff --git a/site/src/components/TemplateStats/TemplateStats.tsx b/site/src/components/TemplateStats/TemplateStats.tsx index fed612f45f019..3309ab7286764 100644 --- a/site/src/components/TemplateStats/TemplateStats.tsx +++ b/site/src/components/TemplateStats/TemplateStats.tsx @@ -14,7 +14,6 @@ const Language = { userPlural: "users", userSingular: "user", createdByLabel: "Created by", - defaultTemplateCreator: "", } export interface TemplateStatsProps { @@ -50,7 +49,7 @@ export const TemplateStats: FC = ({ template, activeVersion
{Language.createdByLabel} - {template.created_by_name || Language.defaultTemplateCreator} + {template.created_by_name}
) diff --git a/site/src/pages/TemplatesPage/TemplatesPageView.tsx b/site/src/pages/TemplatesPage/TemplatesPageView.tsx index deda1cad6cdcc..f260ca2bac27c 100644 --- a/site/src/pages/TemplatesPage/TemplatesPageView.tsx +++ b/site/src/pages/TemplatesPage/TemplatesPageView.tsx @@ -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: "", } const TemplateHelpTooltip: React.FC = () => { @@ -140,7 +139,7 @@ export const TemplatesPageView: FC = (props) => { {Language.developerCount(template.workspace_owner_count)} {dayjs().to(dayjs(template.updated_at))} - {template.created_by_name || Language.defaultTemplateCreator} + {template.created_by_name}
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