@@ -30,19 +30,45 @@ FROM (SELECT tv.template_id,
30
30
);
31
31
32
32
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
44
69
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 ;
46
72
47
73
CREATE VIEW workspace_prebuild_builds AS
48
74
SELECT *
0 commit comments