Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 02aa1ac

Browse files
committed
Review notes and test fixing
1 parent a40eb01 commit 02aa1ac

11 files changed

+76
-89
lines changed

coderd/database/dbauthz/dbauthz_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -3709,7 +3709,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
37093709
OrganizationID: org.ID,
37103710
CreatedBy: user.ID,
37113711
})
3712-
_, err := db.InsertPreset(context.Background(), database.InsertPresetParams{
3712+
preset, err := db.InsertPreset(context.Background(), database.InsertPresetParams{
37133713
TemplateVersionID: templateVersion.ID,
37143714
Name: "test",
37153715
})
@@ -3722,10 +3722,11 @@ func (s *MethodTestSuite) TestSystemFunctions() {
37223722
OrganizationID: org.ID,
37233723
})
37243724
workspaceBuild := dbgen.WorkspaceBuild(s.T(), db, database.WorkspaceBuild{
3725-
WorkspaceID: workspace.ID,
3726-
TemplateVersionID: templateVersion.ID,
3727-
InitiatorID: user.ID,
3728-
JobID: job.ID,
3725+
WorkspaceID: workspace.ID,
3726+
TemplateVersionID: templateVersion.ID,
3727+
TemplateVersionPresetID: uuid.NullUUID{UUID: preset.ID, Valid: true},
3728+
InitiatorID: user.ID,
3729+
JobID: job.ID,
37293730
})
37303731
require.NoError(s.T(), err)
37313732
db.GetPresetByWorkspaceBuildID(context.Background(), workspaceBuild.ID)

coderd/database/dbmem/dbmem.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -3675,10 +3675,9 @@ func (q *FakeQuerier) GetPresetByWorkspaceBuildID(_ context.Context, workspaceBu
36753675
for _, preset := range q.presets {
36763676
if preset.TemplateVersionID == workspaceBuild.TemplateVersionID {
36773677
return database.GetPresetByWorkspaceBuildIDRow{
3678-
ID: uuid.NullUUID{UUID: preset.ID, Valid: true},
3679-
Name: sql.NullString{String: preset.Name, Valid: true},
3680-
CreatedAt: sql.NullTime{Time: preset.CreatedAt, Valid: true},
3681-
UpdatedAt: preset.UpdatedAt,
3678+
ID: preset.ID,
3679+
Name: preset.Name,
3680+
CreatedAt: preset.CreatedAt,
36823681
}, nil
36833682
}
36843683
}
@@ -3714,7 +3713,6 @@ func (q *FakeQuerier) GetPresetsByTemplateVersionID(_ context.Context, templateV
37143713
ID: preset.ID,
37153714
Name: preset.Name,
37163715
CreatedAt: preset.CreatedAt,
3717-
UpdatedAt: preset.UpdatedAt,
37183716
})
37193717
}
37203718
}
@@ -7920,7 +7918,6 @@ func (q *FakeQuerier) InsertPreset(_ context.Context, arg database.InsertPresetP
79207918
TemplateVersionID: arg.TemplateVersionID,
79217919
Name: arg.Name,
79227920
CreatedAt: arg.CreatedAt,
7923-
UpdatedAt: arg.UpdatedAt,
79247921
}
79257922
q.presets = append(q.presets, preset)
79267923
return preset, nil

coderd/database/dump.sql

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/foreign_key_constraint.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/migrations/000288_workspace_parameter_presets.down.sql

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
-- Recreate the view to exclude the new column.
1+
-- DROP the workspace_build_with_user view so that we can recreate without
2+
-- workspace_builds.template_version_preset_id below. We need to drop the view
3+
-- before dropping workspace_builds.template_version_preset_id because the view
4+
-- references it. We can only recreate the view after dropping the column,
5+
-- because the view needs to be created without the column.
26
DROP VIEW workspace_build_with_user;
37

48
ALTER TABLE workspace_builds

coderd/database/migrations/000288_workspace_parameter_presets.up.sql

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,9 @@ CREATE TABLE template_version_presets
22
(
33
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
44
template_version_id UUID NOT NULL,
5-
-- TODO (sasswart): TEXT vs VARCHAR? Check with Mr Kopping.
6-
-- TODO (sasswart): A comment on the presets RFC mentioned that we may need to
7-
-- aggregate presets by name because we want statistics for related presets across
8-
-- template versions. This makes me uncomfortable. It couples constraints to a user
9-
-- facing field that could be avoided.
105
name TEXT NOT NULL,
116
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
12-
-- TODO (sasswart): What do we need updated_at for? Is it worth it to have a history table?
137
-- TODO (sasswart): Will auditing have any relevance to presets?
14-
updated_at TIMESTAMP WITH TIME ZONE,
158
FOREIGN KEY (template_version_id) REFERENCES template_versions (id) ON DELETE CASCADE
169
);
1710

@@ -30,6 +23,15 @@ CREATE TABLE template_version_preset_parameters
3023
ALTER TABLE workspace_builds
3124
ADD COLUMN template_version_preset_id UUID NULL;
3225

26+
ALTER TABLE workspace_builds
27+
ADD CONSTRAINT workspace_builds_template_version_preset_id_fkey
28+
FOREIGN KEY (template_version_preset_id)
29+
REFERENCES template_version_presets (id)
30+
-- TODO (sasswart): SET NULL might not be the best choice here. The rest of the hierarchy has ON DELETE CASCADE.
31+
-- We don't want CASCADE here, because we don't want to delete the workspace build if the preset is deleted.
32+
-- However, do we want to lose record of the preset id for a workspace build?
33+
ON DELETE SET NULL;
34+
3335
-- Recreate the view to include the new column.
3436
DROP VIEW workspace_build_with_user;
3537
CREATE VIEW

coderd/database/migrations/testdata/fixtures/000288_workspace_parameter_presets.up.sql

+15-14
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,29 @@ VALUES ('d3fd38d2-ffc3-4ec2-8cfc-9c8dab6d9a74', 'Test Org', 'Test Organization',
66
INSERT INTO users (id, email, username, created_at, updated_at, status, rbac_roles, login_type, hashed_password)
77
VALUES ('1f573504-f7a0-4498-8b81-2e1939f3c4a2', '[email protected]', 'testuser', now(), now(), 'active', '{}', 'password', 'password');
88

9-
-- Template
10-
INSERT INTO templates (id, created_by, organization_id, created_at, updated_at, deleted, name, provisioner, active_version_id, description)
11-
VALUES ('0bd0713b-176a-4864-a58b-546a1b021025', '1f573504-f7a0-4498-8b81-2e1939f3c4a2', 'd3fd38d2-ffc3-4ec2-8cfc-9c8dab6d9a74', now(), now(), false, 'test-template', 'terraform', null, 'Test template');
9+
-- Provisioner Job
10+
INSERT INTO provisioner_jobs (id, organization_id, created_at, updated_at, initiator_id, provisioner, storage_method, type, input, file_id)
11+
VALUES ('50ebe702-82e5-4053-859d-c24a3b742b57', 'd3fd38d2-ffc3-4ec2-8cfc-9c8dab6d9a74', now(), now(), '1f573504-f7a0-4498-8b81-2e1939f3c4a2', 'echo', 'file', 'template_version_import', '{}', '00000000-82e5-4053-859d-c24a3b742b57');
1212

1313
-- Template Version
14-
INSERT INTO template_versions (id, template_id, organization_id, created_by, created_at, updated_at, name, job_id, readme, message)
15-
VALUES ('f1276e15-01cd-406d-8ea5-64f113a79601', '0bd0713b-176a-4864-a58b-546a1b021025', 'd3fd38d2-ffc3-4ec2-8cfc-9c8dab6d9a74', '1f573504-f7a0-4498-8b81-2e1939f3c4a2', now(), now(), 'test-version', null, '', '');
14+
INSERT INTO template_versions (id, organization_id, created_by, created_at, updated_at, name, job_id, readme, message)
15+
VALUES ('f1276e15-01cd-406d-8ea5-64f113a79601', 'd3fd38d2-ffc3-4ec2-8cfc-9c8dab6d9a74', '1f573504-f7a0-4498-8b81-2e1939f3c4a2', now(), now(), 'test-version', '50ebe702-82e5-4053-859d-c24a3b742b57', '', '');
16+
17+
-- Template
18+
INSERT INTO templates (id, created_by, organization_id, created_at, updated_at, deleted, name, provisioner, active_version_id, description)
19+
VALUES ('0bd0713b-176a-4864-a58b-546a1b021025', '1f573504-f7a0-4498-8b81-2e1939f3c4a2', 'd3fd38d2-ffc3-4ec2-8cfc-9c8dab6d9a74', now(), now(), false, 'test-template', 'terraform', 'f1276e15-01cd-406d-8ea5-64f113a79601', 'Test template');
1620

21+
UPDATE templates SET active_version_id = 'f1276e15-01cd-406d-8ea5-64f113a79601' WHERE id = '0bd0713b-176a-4864-a58b-546a1b021025';
1722
-- Workspace
1823
INSERT INTO workspaces (id, organization_id, owner_id, template_id, created_at, updated_at, name, deleted, automatic_updates)
1924
VALUES ('8cb0b7c4-47b5-4bfc-ad92-88ccc61f3c12', 'd3fd38d2-ffc3-4ec2-8cfc-9c8dab6d9a74', '1f573504-f7a0-4498-8b81-2e1939f3c4a2', '0bd0713b-176a-4864-a58b-546a1b021025', now(), now(), 'test-workspace', false, 'never');
2025

21-
-- Provisioner Job
22-
INSERT INTO provisioner_jobs (id, organization_id, created_at, updated_at, status, worker_id, error, started_at)
23-
VALUES ('50ebe702-82e5-4053-859d-c24a3b742b57', 'd3fd38d2-ffc3-4ec2-8cfc-9c8dab6d9a74', now(), now(), 'pending', null, null, null);
24-
2526
-- Workspace Build
26-
INSERT INTO workspace_builds (id, workspace_id, template_version_id, initiator_id, job_id, created_at, updated_at, transition, reason)
27-
VALUES ('83b28647-743c-4649-b226-f2be697ca06c', '8cb0b7c4-47b5-4bfc-ad92-88ccc61f3c12', 'f1276e15-01cd-406d-8ea5-64f113a79601', '1f573504-f7a0-4498-8b81-2e1939f3c4a2', '50ebe702-82e5-4053-859d-c24a3b742b57', now(), now(), 'start', 'initiator');
27+
INSERT INTO workspace_builds (id, workspace_id, template_version_id, initiator_id, job_id, created_at, updated_at, transition, reason, build_number)
28+
VALUES ('83b28647-743c-4649-b226-f2be697ca06c', '8cb0b7c4-47b5-4bfc-ad92-88ccc61f3c12', 'f1276e15-01cd-406d-8ea5-64f113a79601', '1f573504-f7a0-4498-8b81-2e1939f3c4a2', '50ebe702-82e5-4053-859d-c24a3b742b57', now(), now(), 'start', 'initiator', 45);
2829

2930
-- Template Version Presets
30-
INSERT INTO template_version_presets (id, template_version_id, name, created_at, updated_at, description)
31+
INSERT INTO template_version_presets (id, template_version_id, name, created_at)
3132
VALUES
32-
('575a0fbb-cc3e-4709-ae9f-d1a3f365909c', 'f1276e15-01cd-406d-8ea5-64f113a79601', 'test', now(), now(), 'Test preset'),
33-
('2c76596d-436d-42eb-a38c-8d5a70497030', 'f1276e15-01cd-406d-8ea5-64f113a79601', 'test', now(), now(), 'Test preset');
33+
('575a0fbb-cc3e-4709-ae9f-d1a3f365909c', 'f1276e15-01cd-406d-8ea5-64f113a79601', 'test', now()),
34+
('2c76596d-436d-42eb-a38c-8d5a70497030', 'f1276e15-01cd-406d-8ea5-64f113a79601', 'test', now());

coderd/database/models.go

+4-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+17-38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/presets.sql

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
-- name: InsertPreset :one
22
INSERT INTO
3-
template_version_presets (template_version_id, name, created_at, updated_at)
3+
template_version_presets (template_version_id, name, created_at)
44
VALUES
5-
(@template_version_id, @name, @created_at, @updated_at) RETURNING *;
5+
(@template_version_id, @name, @created_at) RETURNING *;
66

77
-- InsertPresetParameter :one
88
INSERT INTO
99
template_version_preset_parameters (template_version_preset_id, name, value)
10-
VALUES
11-
(@template_version_preset_id, @name, @value) RETURNING *;
10+
SELECT
11+
@template_version_preset_id,
12+
unnest(@name),
13+
unnest(@value)
14+
RETURNING *;
1215

1316
-- name: GetPresetsByTemplateVersionID :many
1417
SELECT
1518
id,
1619
name,
17-
created_at,
18-
updated_at
20+
created_at
1921
FROM
2022
template_version_presets
2123
WHERE
@@ -25,11 +27,10 @@ WHERE
2527
SELECT
2628
template_version_presets.id,
2729
template_version_presets.name,
28-
template_version_presets.created_at,
29-
template_version_presets.updated_at
30+
template_version_presets.created_at
3031
FROM
3132
workspace_builds
32-
LEFT JOIN template_version_presets ON workspace_builds.template_version_preset_id = template_version_presets.id
33+
INNER JOIN template_version_presets ON workspace_builds.template_version_preset_id = template_version_presets.id
3334
WHERE
3435
workspace_builds.id = @workspace_build_id;
3536

0 commit comments

Comments
 (0)