@@ -9,30 +9,37 @@ ORDER BY workspace_id, build_number DESC;
9
9
CREATE VIEW workspace_prebuilds AS
10
10
WITH
11
11
-- 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
+ ),
15
17
-- We can't rely on the template_version_preset_id in the workspace_builds table because this value is only set on the
16
18
-- initial workspace creation. Subsequent stop/start transitions will not have a value for template_version_preset_id,
17
19
-- and therefore we can't rely on (say) the latest build's chosen template_version_preset_id.
18
20
--
19
21
-- 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
+ ),
23
28
-- 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
+ ),
31
38
current_presets AS (SELECT w .id AS prebuild_id, lpb .template_version_preset_id
32
39
FROM workspaces w
33
40
INNER JOIN latest_prebuild_builds lpb ON lpb .workspace_id = w .id
34
41
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
36
43
FROM all_prebuilds p
37
44
LEFT JOIN workspace_agents a ON a .workspace_id = p .id
38
45
INNER JOIN current_presets cp ON cp .prebuild_id = p .id ;
0 commit comments