Skip to content

Commit be27a98

Browse files
refactor: minor refactoring
1 parent f3c9e06 commit be27a98

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

coderd/database/queries/prebuilds.sql

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
-- name: ClaimPrebuiltWorkspace :one
2+
UPDATE workspaces w
3+
SET owner_id = @new_user_id::uuid,
4+
name = @new_name::text,
5+
updated_at = NOW()
6+
WHERE w.id IN (
7+
SELECT p.id
8+
FROM workspace_prebuilds p
9+
INNER JOIN workspace_latest_builds b ON b.workspace_id = p.id
10+
INNER JOIN templates t ON p.template_id = t.id
11+
WHERE (b.transition = 'start'::workspace_transition
12+
AND b.job_status IN ('succeeded'::provisioner_job_status))
13+
-- The prebuilds system should never try to claim a prebuild for an inactive template version.
14+
-- Nevertheless, this filter is here as a defensive measure:
15+
AND b.template_version_id = t.active_version_id
16+
AND p.current_preset_id = @preset_id::uuid
17+
AND p.ready
18+
LIMIT 1 FOR UPDATE OF p SKIP LOCKED -- Ensure that a concurrent request will not select the same prebuild.
19+
)
20+
RETURNING w.id, w.name;
21+
122
-- name: GetTemplatePresetsWithPrebuilds :many
223
-- GetTemplatePresetsWithPrebuilds retrieves template versions with configured presets.
324
-- It also returns the number of desired instances for each preset.
@@ -104,27 +125,6 @@ WHERE tsb.rn <= tsb.desired_instances -- Fetch the last N builds, where N is the
104125
AND created_at >= @lookback::timestamptz
105126
GROUP BY tsb.template_version_id, tsb.preset_id, fc.num_failed;
106127

107-
-- name: ClaimPrebuiltWorkspace :one
108-
UPDATE workspaces w
109-
SET owner_id = @new_user_id::uuid,
110-
name = @new_name::text,
111-
updated_at = NOW()
112-
WHERE w.id IN (
113-
SELECT p.id
114-
FROM workspace_prebuilds p
115-
INNER JOIN workspace_latest_builds b ON b.workspace_id = p.id
116-
INNER JOIN templates t ON p.template_id = t.id
117-
WHERE (b.transition = 'start'::workspace_transition
118-
AND b.job_status IN ('succeeded'::provisioner_job_status))
119-
-- The prebuilds system should never try to claim a prebuild for an inactive template version.
120-
-- Nevertheless, this filter is here as a defensive measure:
121-
AND b.template_version_id = t.active_version_id
122-
AND p.current_preset_id = @preset_id::uuid
123-
AND p.ready
124-
LIMIT 1 FOR UPDATE OF p SKIP LOCKED -- Ensure that a concurrent request will not select the same prebuild.
125-
)
126-
RETURNING w.id, w.name;
127-
128128
-- name: GetPrebuildMetrics :many
129129
SELECT
130130
t.name as template_name,

0 commit comments

Comments
 (0)