Skip to content

chore: include organization name when fetching templates #13751

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 6 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: rename template_with_user to template_with_names
  • Loading branch information
Emyrk committed Jul 1, 2024
commit d7a0abfd290c4aef5f9601d3aa5048c5883d1c27
2 changes: 1 addition & 1 deletion cli/templatelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func (r *RootCmd) templateList() *serpent.Command {
orgContext := NewOrganizationContext()
formatter := cliui.NewOutputFormatter(
cliui.TableFormat([]templateTableRow{}, []string{"name", "last updated", "used by"}),
cliui.TableFormat([]templateTableRow{}, []string{"name", "organization name", "last updated", "used by"}),
cliui.JSONFormat(),
)

Expand Down
30 changes: 20 additions & 10 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func (q *FakeQuerier) getLatestWorkspaceBuildByWorkspaceIDNoLock(_ context.Conte
func (q *FakeQuerier) getTemplateByIDNoLock(_ context.Context, id uuid.UUID) (database.Template, error) {
for _, template := range q.templates {
if template.ID == id {
return q.templateWithUserNoLock(template), nil
return q.templateWithNameNoLock(template), nil
}
}
return database.Template{}, sql.ErrNoRows
Expand All @@ -524,26 +524,36 @@ func (q *FakeQuerier) getTemplateByIDNoLock(_ context.Context, id uuid.UUID) (da
func (q *FakeQuerier) templatesWithUserNoLock(tpl []database.TemplateTable) []database.Template {
cpy := make([]database.Template, 0, len(tpl))
for _, t := range tpl {
cpy = append(cpy, q.templateWithUserNoLock(t))
cpy = append(cpy, q.templateWithNameNoLock(t))
}
return cpy
}

func (q *FakeQuerier) templateWithUserNoLock(tpl database.TemplateTable) database.Template {
func (q *FakeQuerier) templateWithNameNoLock(tpl database.TemplateTable) database.Template {
var user database.User
for _, _user := range q.users {
if _user.ID == tpl.CreatedBy {
user = _user
break
}
}
var withUser database.Template

var org database.Organization
for _, _org := range q.organizations {
if _org.ID == tpl.OrganizationID {
org = _org
break
}
}

var withNames database.Template
// This is a cheeky way to copy the fields over without explicitly listing them all.
d, _ := json.Marshal(tpl)
_ = json.Unmarshal(d, &withUser)
withUser.CreatedByUsername = user.Username
withUser.CreatedByAvatarURL = user.AvatarURL
return withUser
_ = json.Unmarshal(d, &withNames)
withNames.CreatedByUsername = user.Username
withNames.CreatedByAvatarURL = user.AvatarURL
withNames.OrganizationName = org.Name
return withNames
}

func (q *FakeQuerier) templateVersionWithUserNoLock(tpl database.TemplateVersionTable) database.TemplateVersion {
Expand Down Expand Up @@ -3675,7 +3685,7 @@ func (q *FakeQuerier) GetTemplateByOrganizationAndName(_ context.Context, arg da
if template.Deleted != arg.Deleted {
continue
}
return q.templateWithUserNoLock(template), nil
return q.templateWithNameNoLock(template), nil
}
return database.Template{}, sql.ErrNoRows
}
Expand Down Expand Up @@ -9323,7 +9333,7 @@ func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G

var templates []database.Template
for _, templateTable := range q.templates {
template := q.templateWithUserNoLock(templateTable)
template := q.templateWithNameNoLock(templateTable)
if prepared != nil && prepared.Authorize(ctx, template.RBACObject()) != nil {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions coderd/database/dump.sql

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/gentest/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestViewSubsetTemplate(t *testing.T) {
tableFields := allFields(table)
joinedFields := allFields(joined)
if !assert.Subset(t, fieldNames(joinedFields), fieldNames(tableFields), "table is not subset") {
t.Log("Some fields were added to the Template Table without updating the 'template_with_users' view.")
t.Log("Some fields were added to the Template Table without updating the 'template_with_names' view.")
t.Log("See migration 000138_join_users.up.sql to create the view.")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
-- Update the template_with_users view by recreating it.
DROP VIEW template_with_users;

-- Renaming template_with_users -> template_with_names
CREATE VIEW
template_with_users
template_with_names
AS
SELECT
templates.*,
Expand All @@ -20,4 +21,4 @@ FROM
ON templates.organization_id = organizations.id
;

COMMENT ON VIEW template_with_users IS 'Joins in the display name information such as username, avatar, and organization name.';
COMMENT ON VIEW template_with_names IS 'Joins in the display name information such as username, avatar, and organization name.';
8 changes: 4 additions & 4 deletions coderd/database/queries.sql.go

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

8 changes: 4 additions & 4 deletions coderd/database/queries/templates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
SELECT
*
FROM
template_with_users
template_with_names
WHERE
id = $1
LIMIT
Expand All @@ -12,7 +12,7 @@ LIMIT
SELECT
*
FROM
template_with_users AS templates
template_with_names AS templates
WHERE
-- Optionally include deleted templates
templates.deleted = @deleted
Expand Down Expand Up @@ -54,7 +54,7 @@ ORDER BY (name, id) ASC
SELECT
*
FROM
template_with_users AS templates
template_with_names AS templates
WHERE
organization_id = @organization_id
AND deleted = @deleted
Expand All @@ -63,7 +63,7 @@ LIMIT
1;

-- name: GetTemplates :many
SELECT * FROM template_with_users AS templates
SELECT * FROM template_with_names AS templates
ORDER BY (name, id) ASC
;

Expand Down
6 changes: 3 additions & 3 deletions coderd/database/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ sql:
- column: "templates.group_acl"
go_type:
type: "TemplateACL"
- column: "template_with_users.user_acl"
- column: "template_with_names.user_acl"
go_type:
type: "TemplateACL"
- column: "template_with_users.group_acl"
- column: "template_with_names.group_acl"
go_type:
type: "TemplateACL"
- column: "template_usage_stats.app_usage_mins"
Expand All @@ -72,7 +72,7 @@ sql:
type: "[]byte"
rename:
template: TemplateTable
template_with_user: Template
template_with_name: Template
workspace_build: WorkspaceBuildTable
workspace_build_with_user: WorkspaceBuild
template_version: TemplateVersionTable
Expand Down