|
7 | 7 | WHERE
|
8 | 8 | template_id = @template_id :: uuid
|
9 | 9 | AND CASE
|
10 |
| - -- If no filter is provided, default to returning ALL template versions. |
11 |
| - -- The called should always provide a filter if they want to omit |
12 |
| - -- deleted versions. |
| 10 | + -- If no filter is provided, default to returning ALL template versions. |
| 11 | + -- The called should always provide a filter if they want to omit |
| 12 | + -- deleted versions. |
13 | 13 | WHEN @deleted :: boolean IS NULL THEN true
|
14 | 14 | ELSE template_versions.deleted = @deleted :: boolean
|
15 | 15 | END
|
|
33 | 33 | ELSE true
|
34 | 34 | END
|
35 | 35 | ORDER BY
|
36 |
| - -- Deterministic and consistent ordering of all rows, even if they share |
37 |
| - -- a timestamp. This is to ensure consistent pagination. |
| 36 | + -- Deterministic and consistent ordering of all rows, even if they share |
| 37 | + -- a timestamp. This is to ensure consistent pagination. |
38 | 38 | (created_at, id) ASC OFFSET @offset_opt
|
39 | 39 | LIMIT
|
40 | 40 | -- A null limit means "no limit", so 0 means return all
|
@@ -149,45 +149,45 @@ UPDATE
|
149 | 149 | template_versions
|
150 | 150 | SET
|
151 | 151 | deleted = true,
|
152 |
| - updated_at = @updated_at |
| 152 | + updated_at = sqlc.arg('updated_at') |
153 | 153 | FROM
|
154 |
| - -- Delete all versions that are returned from this query. |
155 |
| - ( |
156 |
| - SELECT |
157 |
| - id |
158 |
| - FROM |
| 154 | + -- Delete all versions that are returned from this query. |
| 155 | + ( |
| 156 | + SELECT |
| 157 | + scoped_template_versions.id |
| 158 | + FROM |
159 | 159 | -- Scope a prune to a single template and ignore already deleted template versions
|
160 |
| - (SELECT * FROM template_versions WHERE template_id = @template_id AND deleted = false) AS template_versions |
161 |
| - LEFT JOIN |
162 |
| - provisioner_jobs ON template_versions.job_id = provisioner_jobs.id |
163 |
| - LEFT JOIN |
164 |
| - templates ON template_versions.template_id = templates.id |
165 |
| - WHERE |
| 160 | + (SELECT * FROM template_versions WHERE template_versions.template_id = sqlc.arg('template_id') :: uuid AND deleted = false) AS scoped_template_versions |
| 161 | + LEFT JOIN |
| 162 | + provisioner_jobs ON scoped_template_versions.job_id = provisioner_jobs.id |
| 163 | + LEFT JOIN |
| 164 | + templates ON scoped_template_versions.template_id = templates.id |
| 165 | + WHERE |
166 | 166 | -- Actively used template versions (meaning the latest build is using
|
167 | 167 | -- the version) are never pruned. A "restart" command on the workspace,
|
168 | 168 | -- even if failed, would use the version. So it cannot be pruned until
|
169 | 169 | -- the build is outdated.
|
170 | 170 | -- TODO: This is an issue for "deleted workspaces", since a deleted workspace
|
171 | 171 | -- has a build with the transition "delete". This will prevent that template
|
172 | 172 | -- version from ever being pruned. We need a method to prune deleted workspaces.
|
173 |
| - template_versions.id != ANY( |
| 173 | + scoped_template_versions.id != ANY( |
174 | 174 | SELECT DISTINCT ON(workspace_id)
|
175 |
| - template_version_id |
| 175 | + scoped_template_versions |
176 | 176 | FROM
|
177 | 177 | workspace_builds
|
178 | 178 | ORDER BY build_number DESC
|
179 | 179 | )
|
180 | 180 | -- Also never delete the active template version
|
181 |
| - AND active_version_id != template_versions.id |
182 |
| - AND CASE |
183 |
| - -- Optionally, only prune versions that match a given |
184 |
| - -- job status like 'failed'. |
185 |
| - WHEN @job_status != '' THEN |
186 |
| - provisioner_jobs.job_status = @job_status |
187 |
| - ELSE |
188 |
| - true |
189 |
| - END |
190 |
| - |
| 181 | + AND active_version_id != scoped_template_versions.id |
| 182 | + AND CASE |
| 183 | + -- Optionally, only prune versions that match a given |
| 184 | + -- job status like 'failed'. |
| 185 | + WHEN sqlc.narg('job_status') :: provisioner_job_status IS NOT NULL THEN |
| 186 | + provisioner_jobs.job_status = sqlc.narg('job_status') :: provisioner_job_status |
| 187 | + ELSE |
| 188 | + true |
| 189 | + END |
191 | 190 | ) AS deleted_versions
|
192 | 191 | WHERE
|
193 |
| - id = ANY(deleted_versions); |
| 192 | + template_versions.id = ANY(deleted_versions) |
| 193 | +RETURNING template_versions.id; |
0 commit comments