Skip to content

Commit d04ba2c

Browse files
authored
feat: add template version creator (#3001)
1 parent d26b3b7 commit d04ba2c

File tree

14 files changed

+114
-16
lines changed

14 files changed

+114
-16
lines changed

coderd/audit/diff_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,14 @@ func TestDiff(t *testing.T) {
114114
UpdatedAt: time.Now(),
115115
OrganizationID: uuid.UUID{3},
116116
Name: "rust",
117+
CreatedBy: uuid.NullUUID{UUID: uuid.UUID{4}, Valid: true},
117118
},
118119
exp: audit.Map{
119120
"id": uuid.UUID{1}.String(),
120121
"template_id": uuid.UUID{2}.String(),
121122
"organization_id": uuid.UUID{3}.String(),
122123
"name": "rust",
124+
"created_by": uuid.UUID{4}.String(),
123125
},
124126
},
125127
{
@@ -132,11 +134,13 @@ func TestDiff(t *testing.T) {
132134
UpdatedAt: time.Now(),
133135
OrganizationID: uuid.UUID{3},
134136
Name: "rust",
137+
CreatedBy: uuid.NullUUID{UUID: uuid.UUID{4}, Valid: true},
135138
},
136139
exp: audit.Map{
137140
"id": uuid.UUID{1}.String(),
138141
"organization_id": uuid.UUID{3}.String(),
139142
"name": "rust",
143+
"created_by": uuid.UUID{4}.String(),
140144
},
141145
},
142146
})

coderd/audit/table.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ var AuditableResources = auditMap(map[any]map[string]Action{
8383
"name": ActionTrack,
8484
"readme": ActionTrack,
8585
"job_id": ActionIgnore, // Not helpful in a diff because jobs aren't tracked in audit logs.
86+
"created_by": ActionTrack,
8687
},
8788
&database.User{}: {
8889
"id": ActionTrack,

coderd/database/databasefake/databasefake.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,7 @@ func (q *fakeQuerier) InsertTemplateVersion(_ context.Context, arg database.Inse
15111511
Name: arg.Name,
15121512
Readme: arg.Readme,
15131513
JobID: arg.JobID,
1514+
CreatedBy: arg.CreatedBy,
15141515
}
15151516
q.templateVersions = append(q.templateVersions, version)
15161517
return version, nil

coderd/database/dump.sql

Lines changed: 5 additions & 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 template_versions DROP COLUMN IF EXISTS created_by;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
BEGIN;
2+
3+
ALTER TABLE ONLY template_versions ADD COLUMN IF NOT EXISTS created_by uuid REFERENCES users (id) ON DELETE RESTRICT;
4+
5+
UPDATE
6+
template_versions
7+
SET
8+
created_by = (
9+
SELECT created_by FROM templates
10+
WHERE template_versions.template_id = templates.id
11+
LIMIT 1
12+
)
13+
WHERE
14+
created_by IS NULL;
15+
16+
COMMIT;

coderd/database/models.go

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

coderd/database/queries/templateversions.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ INSERT INTO
7070
updated_at,
7171
"name",
7272
readme,
73-
job_id
73+
job_id,
74+
created_by
7475
)
7576
VALUES
76-
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;
77+
($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *;
7778

7879
-- name: UpdateTemplateVersionByID :exec
7980
UPDATE

coderd/templates.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
209209
UUID: dbTemplate.ID,
210210
Valid: true,
211211
},
212+
UpdatedAt: database.Now(),
212213
})
213214
if err != nil {
214215
return xerrors.Errorf("insert template version: %s", err)

0 commit comments

Comments
 (0)