|
1 | 1 | -- name: GetRunningPrebuilds :many
|
2 | 2 | SELECT p.id AS workspace_id,
|
3 |
| - p.name AS workspace_name, |
4 |
| - p.template_id, |
5 |
| - b.template_version_id, |
6 |
| - tvp_curr.id AS current_preset_id, |
7 |
| - tvp_desired.id AS desired_preset_id, |
8 |
| - -- TODO: just because a prebuild is in a ready state doesn't mean it's eligible; if the prebuild is due to be |
9 |
| - -- deleted to reconcile state then it MUST NOT be eligible for claiming. We'll need some kind of lock here. |
10 |
| - CASE |
11 |
| - WHEN p.lifecycle_state = 'ready'::workspace_agent_lifecycle_state THEN TRUE |
12 |
| - ELSE FALSE END AS ready, |
13 |
| - p.created_at |
| 3 | + p.name AS workspace_name, |
| 4 | + p.template_id, |
| 5 | + b.template_version_id, |
| 6 | + tvp_curr.id AS current_preset_id, |
| 7 | + -- TODO: just because a prebuild is in a ready state doesn't mean it's eligible; if the prebuild is due to be |
| 8 | + -- deleted to reconcile state then it MUST NOT be eligible for claiming. We'll need some kind of lock here. |
| 9 | + CASE |
| 10 | + WHEN p.lifecycle_state = 'ready'::workspace_agent_lifecycle_state THEN TRUE |
| 11 | + ELSE FALSE END AS ready, |
| 12 | + p.created_at |
14 | 13 | FROM workspace_prebuilds p
|
15 |
| - INNER JOIN workspace_latest_build b ON b.workspace_id = p.id |
16 |
| - INNER JOIN provisioner_jobs pj ON b.job_id = pj.id |
17 |
| - INNER JOIN templates t ON p.template_id = t.id |
18 |
| - LEFT JOIN template_version_presets tvp_curr |
19 |
| - ON tvp_curr.id = p.current_preset_id -- See https://github.com/coder/internal/issues/398. |
20 |
| - LEFT JOIN template_version_presets tvp_desired |
21 |
| - ON tvp_desired.template_version_id = t.active_version_id |
| 14 | + INNER JOIN workspace_latest_build b ON b.workspace_id = p.id |
| 15 | + INNER JOIN provisioner_jobs pj ON b.job_id = pj.id |
| 16 | + INNER JOIN templates t ON p.template_id = t.id |
| 17 | + LEFT JOIN template_version_presets tvp_curr |
| 18 | + ON tvp_curr.id = p.current_preset_id -- See https://github.com/coder/internal/issues/398. |
22 | 19 | WHERE (b.transition = 'start'::workspace_transition
|
23 |
| - -- if a deletion job fails, the workspace will still be running |
24 |
| - OR pj.job_status IN ('failed'::provisioner_job_status, 'canceled'::provisioner_job_status, |
25 |
| - 'unknown'::provisioner_job_status)) |
26 |
| - AND (tvp_curr.name = tvp_desired.name |
27 |
| - OR tvp_desired.id IS NULL); |
| 20 | + -- Jobs that are not in terminal states. |
| 21 | + OR pj.job_status IN ('failed'::provisioner_job_status, 'canceled'::provisioner_job_status, |
| 22 | + 'unknown'::provisioner_job_status)); |
28 | 23 |
|
29 | 24 | -- name: GetTemplatePresetsWithPrebuilds :many
|
30 | 25 | SELECT t.id AS template_id,
|
31 |
| - tv.id AS template_version_id, |
32 |
| - tv.id = t.active_version_id AS using_active_version, |
33 |
| - tvpp.preset_id, |
34 |
| - tvp.name, |
35 |
| - tvpp.desired_instances AS desired_instances, |
36 |
| - t.deleted, |
37 |
| - t.deprecated != '' AS deprecated |
| 26 | + tv.id AS template_version_id, |
| 27 | + tv.id = t.active_version_id AS using_active_version, |
| 28 | + tvpp.preset_id, |
| 29 | + tvp.name, |
| 30 | + tvpp.desired_instances AS desired_instances, |
| 31 | + t.deleted, |
| 32 | + t.deprecated != '' AS deprecated |
38 | 33 | FROM templates t
|
39 |
| - INNER JOIN template_versions tv ON tv.template_id = t.id |
40 |
| - INNER JOIN template_version_presets tvp ON tvp.template_version_id = tv.id |
41 |
| - INNER JOIN template_version_preset_prebuilds tvpp ON tvpp.preset_id = tvp.id |
| 34 | + INNER JOIN template_versions tv ON tv.template_id = t.id |
| 35 | + INNER JOIN template_version_presets tvp ON tvp.template_version_id = tv.id |
| 36 | + INNER JOIN template_version_preset_prebuilds tvpp ON tvpp.preset_id = tvp.id |
42 | 37 | WHERE (t.id = sqlc.narg('template_id')::uuid OR sqlc.narg('template_id') IS NULL);
|
43 | 38 |
|
44 | 39 | -- name: GetPrebuildsInProgress :many
|
45 |
| -SELECT wpb.template_version_id, wpb.transition, COUNT(wpb.transition) AS count |
| 40 | +SELECT t.id AS template_id, wpb.template_version_id, wpb.transition, COUNT(wpb.transition) AS count |
46 | 41 | FROM workspace_latest_build wlb
|
47 |
| - INNER JOIN provisioner_jobs pj ON wlb.job_id = pj.id |
48 |
| - INNER JOIN workspace_prebuild_builds wpb ON wpb.id = wlb.id |
49 |
| -WHERE pj.job_status NOT IN |
50 |
| - ('succeeded'::provisioner_job_status, 'canceled'::provisioner_job_status, |
51 |
| - 'failed'::provisioner_job_status) |
52 |
| -GROUP BY wpb.template_version_id, wpb.transition; |
| 42 | + INNER JOIN provisioner_jobs pj ON wlb.job_id = pj.id |
| 43 | + INNER JOIN workspace_prebuild_builds wpb ON wpb.id = wlb.id |
| 44 | + INNER JOIN templates t ON t.active_version_id = wlb.template_version_id |
| 45 | +WHERE pj.job_status NOT IN -- Jobs that are not in terminal states. |
| 46 | + ('succeeded'::provisioner_job_status, 'canceled'::provisioner_job_status, |
| 47 | + 'failed'::provisioner_job_status) |
| 48 | +GROUP BY t.id, wpb.template_version_id, wpb.transition; |
53 | 49 |
|
54 | 50 | -- name: ClaimPrebuild :one
|
55 | 51 | -- TODO: rewrite to use named CTE instead?
|
56 | 52 | UPDATE workspaces w
|
57 | 53 | SET owner_id = @new_user_id::uuid,
|
58 |
| - name = @new_name::text, |
59 |
| - updated_at = NOW() |
| 54 | + name = @new_name::text, |
| 55 | + updated_at = NOW() |
60 | 56 | WHERE w.id IN (SELECT p.id
|
61 |
| - FROM workspace_prebuilds p |
62 |
| - INNER JOIN workspace_latest_build b ON b.workspace_id = p.id |
63 |
| - INNER JOIN provisioner_jobs pj ON b.job_id = pj.id |
64 |
| - INNER JOIN templates t ON p.template_id = t.id |
65 |
| - WHERE (b.transition = 'start'::workspace_transition |
66 |
| - AND pj.job_status IN ('succeeded'::provisioner_job_status)) |
67 |
| - AND b.template_version_id = t.active_version_id |
68 |
| - AND b.template_version_preset_id = @preset_id::uuid |
69 |
| - AND p.lifecycle_state = 'ready'::workspace_agent_lifecycle_state |
70 |
| - ORDER BY random() |
71 |
| - LIMIT 1 FOR UPDATE OF p SKIP LOCKED) |
| 57 | + FROM workspace_prebuilds p |
| 58 | + INNER JOIN workspace_latest_build b ON b.workspace_id = p.id |
| 59 | + INNER JOIN provisioner_jobs pj ON b.job_id = pj.id |
| 60 | + INNER JOIN templates t ON p.template_id = t.id |
| 61 | + WHERE (b.transition = 'start'::workspace_transition |
| 62 | + AND pj.job_status IN ('succeeded'::provisioner_job_status)) |
| 63 | + AND b.template_version_id = t.active_version_id |
| 64 | + AND b.template_version_preset_id = @preset_id::uuid |
| 65 | + AND p.lifecycle_state = 'ready'::workspace_agent_lifecycle_state |
| 66 | + ORDER BY random() |
| 67 | + LIMIT 1 FOR UPDATE OF p SKIP LOCKED) |
72 | 68 | RETURNING w.id, w.name;
|
73 | 69 |
|
74 | 70 | -- name: InsertPresetPrebuild :one
|
|
0 commit comments