Skip to content

Commit f167b92

Browse files
committed
correctly select for the latest built with a preset in latest_prebuild_builds
1 parent bc4e7d2 commit f167b92

File tree

3 files changed

+40
-93
lines changed

3 files changed

+40
-93
lines changed

coderd/database/dump.sql

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

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

+21-14
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,37 @@ ORDER BY workspace_id, build_number DESC;
99
CREATE VIEW workspace_prebuilds AS
1010
WITH
1111
-- All workspaces owned by the "prebuilds" user.
12-
all_prebuilds AS (SELECT w.*
13-
FROM workspaces w
14-
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'), -- The system user responsible for prebuilds.
12+
all_prebuilds AS (
13+
SELECT w.id, w.name, w.template_id, w.created_at
14+
FROM workspaces w
15+
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0' -- The system user responsible for prebuilds.
16+
),
1517
-- We can't rely on the template_version_preset_id in the workspace_builds table because this value is only set on the
1618
-- initial workspace creation. Subsequent stop/start transitions will not have a value for template_version_preset_id,
1719
-- and therefore we can't rely on (say) the latest build's chosen template_version_preset_id.
1820
--
1921
-- See https://github.com/coder/internal/issues/398
20-
latest_prebuild_builds AS (SELECT *
21-
FROM workspace_latest_builds
22-
WHERE template_version_preset_id IS NOT NULL),
22+
latest_prebuild_builds AS (
23+
SELECT DISTINCT ON (workspace_id) workspace_id, template_version_preset_id
24+
FROM workspace_builds
25+
WHERE template_version_preset_id IS NOT NULL
26+
ORDER BY workspace_id, build_number DESC
27+
),
2328
-- All workspace agents belonging to the workspaces owned by the "prebuilds" user.
24-
workspace_agents AS (SELECT w.id AS workspace_id, wa.id AS agent_id, wa.lifecycle_state, wa.ready_at
25-
FROM workspaces w
26-
INNER JOIN workspace_latest_builds wlb ON wlb.workspace_id = w.id
27-
INNER JOIN workspace_resources wr ON wr.job_id = wlb.job_id
28-
INNER JOIN workspace_agents wa ON wa.resource_id = wr.id
29-
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0' -- The system user responsible for prebuilds.
30-
GROUP BY w.id, wa.id),
29+
workspace_agents AS (
30+
SELECT w.id AS workspace_id, wa.id AS agent_id, wa.lifecycle_state, wa.ready_at
31+
FROM workspaces w
32+
INNER JOIN workspace_latest_builds wlb ON wlb.workspace_id = w.id
33+
INNER JOIN workspace_resources wr ON wr.job_id = wlb.job_id
34+
INNER JOIN workspace_agents wa ON wa.resource_id = wr.id
35+
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0' -- The system user responsible for prebuilds.
36+
GROUP BY w.id, wa.id
37+
),
3138
current_presets AS (SELECT w.id AS prebuild_id, lpb.template_version_preset_id
3239
FROM workspaces w
3340
INNER JOIN latest_prebuild_builds lpb ON lpb.workspace_id = w.id
3441
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0') -- The system user responsible for prebuilds.
35-
SELECT p.*, a.agent_id, a.lifecycle_state, a.ready_at, cp.template_version_preset_id AS current_preset_id
42+
SELECT p.id, p.name, p.template_id, p.created_at, a.agent_id, a.lifecycle_state, a.ready_at, cp.template_version_preset_id AS current_preset_id
3643
FROM all_prebuilds p
3744
LEFT JOIN workspace_agents a ON a.workspace_id = p.id
3845
INNER JOIN current_presets cp ON cp.prebuild_id = p.id;

coderd/database/models.go

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

0 commit comments

Comments
 (0)