Skip to content

Commit c88c04c

Browse files
committed
Determine latest chosen preset using latest non-null preset selection
See coder/internal#398 Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 44d12aa commit c88c04c

File tree

5 files changed

+178
-108
lines changed

5 files changed

+178
-108
lines changed

coderd/database/dump.sql

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

coderd/database/migrations/000297_prebuilds.up.sql

+38-12
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,45 @@ FROM (SELECT tv.template_id,
3030
);
3131

3232
CREATE VIEW workspace_prebuilds AS
33-
WITH all_prebuilds AS (SELECT w.*
34-
FROM workspaces w
35-
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'),
36-
workspace_agents AS (SELECT w.id AS workspace_id, wa.id AS agent_id, wa.lifecycle_state, wa.ready_at
37-
FROM workspaces w
38-
INNER JOIN workspace_latest_build wlb ON wlb.workspace_id = w.id
39-
INNER JOIN workspace_resources wr ON wr.job_id = wlb.job_id
40-
INNER JOIN workspace_agents wa ON wa.resource_id = wr.id
41-
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'
42-
GROUP BY w.id, wa.id)
43-
SELECT p.*, a.agent_id, a.lifecycle_state, a.ready_at
33+
WITH
34+
-- All workspaces owned by the "prebuilds" user.
35+
all_prebuilds AS (SELECT w.*
36+
FROM workspaces w
37+
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'),
38+
-- All workspace agents belonging to the workspaces owned by the "prebuilds" user.
39+
workspace_agents AS (SELECT w.id AS workspace_id, wa.id AS agent_id, wa.lifecycle_state, wa.ready_at
40+
FROM workspaces w
41+
INNER JOIN workspace_latest_build wlb ON wlb.workspace_id = w.id
42+
INNER JOIN workspace_resources wr ON wr.job_id = wlb.job_id
43+
INNER JOIN workspace_agents wa ON wa.resource_id = wr.id
44+
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'
45+
GROUP BY w.id, wa.id),
46+
-- We can't rely on the template_version_preset_id in the workspace_builds table because this value is only set on the
47+
-- initial workspace creation. Subsequent stop/start transitions will not have a value for template_version_preset_id,
48+
-- and therefore we can't rely on (say) the latest build's chosen template_version_preset_id.
49+
--
50+
-- See https://github.com/coder/internal/issues/398
51+
current_presets AS (SELECT w.id AS prebuild_id, lps.template_version_preset_id
52+
FROM workspaces w
53+
INNER JOIN (
54+
-- The latest workspace build which had a preset explicitly selected
55+
SELECT wb.*
56+
FROM (SELECT tv.template_id,
57+
wbmax.workspace_id,
58+
MAX(wbmax.build_number) as max_build_number
59+
FROM workspace_builds wbmax
60+
JOIN template_versions tv ON (tv.id = wbmax.template_version_id)
61+
WHERE wbmax.template_version_preset_id IS NOT NULL
62+
GROUP BY tv.template_id, wbmax.workspace_id) wbmax
63+
JOIN workspace_builds wb ON (
64+
wb.workspace_id = wbmax.workspace_id
65+
AND wb.build_number = wbmax.max_build_number
66+
)) lps ON lps.workspace_id = w.id
67+
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0')
68+
SELECT p.*, a.agent_id, a.lifecycle_state, a.ready_at, cp.template_version_preset_id AS current_preset_id
4469
FROM all_prebuilds p
45-
LEFT JOIN workspace_agents a ON a.workspace_id = p.id;
70+
LEFT JOIN workspace_agents a ON a.workspace_id = p.id
71+
INNER JOIN current_presets cp ON cp.prebuild_id = p.id;
4672

4773
CREATE VIEW workspace_prebuild_builds AS
4874
SELECT *

coderd/database/models.go

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

coderd/database/queries.sql.go

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

0 commit comments

Comments
 (0)