|
19 | 19 | -- and therefore we can't rely on (say) the latest build's chosen template_version_preset_id.
|
20 | 20 | --
|
21 | 21 | -- See https://github.com/coder/internal/issues/398
|
22 |
| - latest_prebuild_builds AS ( |
| 22 | + workspaces_with_latest_presets AS ( |
23 | 23 | SELECT DISTINCT ON (workspace_id) workspace_id, template_version_preset_id
|
24 | 24 | FROM workspace_builds
|
25 | 25 | WHERE template_version_preset_id IS NOT NULL
|
26 | 26 | ORDER BY workspace_id, build_number DESC
|
27 | 27 | ),
|
28 |
| - -- All workspace agents belonging to the workspaces owned by the "prebuilds" user. |
29 |
| - workspace_agents AS ( |
30 |
| - SELECT w.id AS workspace_id, wa.id AS agent_id, wa.lifecycle_state, wa.ready_at |
| 28 | + -- workspaces_with_agents_status contains workspaces owned by the "prebuilds" user, |
| 29 | + -- along with the readiness status of their agents. |
| 30 | + -- A workspace is marked as 'ready' only if ALL of its agents are ready. |
| 31 | + workspaces_with_agents_status AS ( |
| 32 | + SELECT w.id AS workspace_id, |
| 33 | + BOOL_AND(wa.lifecycle_state = 'ready'::workspace_agent_lifecycle_state) AS ready |
31 | 34 | FROM workspaces w
|
32 | 35 | INNER JOIN workspace_latest_builds wlb ON wlb.workspace_id = w.id
|
33 | 36 | INNER JOIN workspace_resources wr ON wr.job_id = wlb.job_id
|
34 | 37 | INNER JOIN workspace_agents wa ON wa.resource_id = wr.id
|
35 | 38 | WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0' -- The system user responsible for prebuilds.
|
36 |
| - GROUP BY w.id, wa.id |
| 39 | + GROUP BY w.id |
37 | 40 | ),
|
38 |
| - current_presets AS (SELECT w.id AS prebuild_id, lpb.template_version_preset_id |
| 41 | + current_presets AS (SELECT w.id AS prebuild_id, wlp.template_version_preset_id |
39 | 42 | FROM workspaces w
|
40 |
| - INNER JOIN latest_prebuild_builds lpb ON lpb.workspace_id = w.id |
| 43 | + INNER JOIN workspaces_with_latest_presets wlp ON wlp.workspace_id = w.id |
41 | 44 | WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0') -- The system user responsible for prebuilds.
|
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 |
| 45 | +SELECT p.id, p.name, p.template_id, p.created_at, COALESCE(a.ready, false) AS ready, cp.template_version_preset_id AS current_preset_id |
43 | 46 | FROM all_prebuilds p
|
44 |
| - LEFT JOIN workspace_agents a ON a.workspace_id = p.id |
| 47 | + LEFT JOIN workspaces_with_agents_status a ON a.workspace_id = p.id |
45 | 48 | INNER JOIN current_presets cp ON cp.prebuild_id = p.id;
|
46 | 49 |
|
47 | 50 | CREATE VIEW workspace_prebuild_builds AS
|
|
0 commit comments