@@ -145,6 +145,135 @@ OFFSET
145
145
@offset_
146
146
;
147
147
148
+ -- this duplicates the filtering in GetWorkspaces
149
+ -- name: GetWorkspaceCount :one
150
+ SELECT
151
+ COUNT (* ) as count
152
+ FROM
153
+ workspaces
154
+ LEFT JOIN LATERAL (
155
+ SELECT
156
+ workspace_builds .transition ,
157
+ provisioner_jobs .started_at ,
158
+ provisioner_jobs .updated_at ,
159
+ provisioner_jobs .canceled_at ,
160
+ provisioner_jobs .completed_at ,
161
+ provisioner_jobs .error
162
+ FROM
163
+ workspace_builds
164
+ LEFT JOIN
165
+ provisioner_jobs
166
+ ON
167
+ provisioner_jobs .id = workspace_builds .job_id
168
+ WHERE
169
+ workspace_builds .workspace_id = workspaces .id
170
+ ORDER BY
171
+ build_number DESC
172
+ LIMIT
173
+ 1
174
+ ) latest_build ON TRUE
175
+ WHERE
176
+ -- Optionally include deleted workspaces
177
+ workspaces .deleted = @deleted
178
+ AND CASE
179
+ WHEN @status :: text != ' ' THEN
180
+ CASE
181
+ WHEN @status = ' pending' THEN
182
+ latest_build .started_at IS NULL
183
+ WHEN @status = ' starting' THEN
184
+ latest_build .started_at IS NOT NULL AND
185
+ latest_build .canceled_at IS NULL AND
186
+ latest_build .completed_at IS NULL AND
187
+ latest_build .updated_at - INTERVAL ' 30 seconds' < NOW() AND
188
+ latest_build .transition = ' start' ::workspace_transition
189
+
190
+ WHEN @status = ' running' THEN
191
+ latest_build .completed_at IS NOT NULL AND
192
+ latest_build .canceled_at IS NULL AND
193
+ latest_build .error IS NULL AND
194
+ latest_build .transition = ' start' ::workspace_transition
195
+
196
+ WHEN @status = ' stopping' THEN
197
+ latest_build .started_at IS NOT NULL AND
198
+ latest_build .canceled_at IS NULL AND
199
+ latest_build .completed_at IS NULL AND
200
+ latest_build .updated_at - INTERVAL ' 30 seconds' < NOW() AND
201
+ latest_build .transition = ' stop' ::workspace_transition
202
+
203
+ WHEN @status = ' stopped' THEN
204
+ latest_build .completed_at IS NOT NULL AND
205
+ latest_build .canceled_at IS NULL AND
206
+ latest_build .error IS NULL AND
207
+ latest_build .transition = ' stop' ::workspace_transition
208
+
209
+ WHEN @status = ' failed' THEN
210
+ (latest_build .canceled_at IS NOT NULL AND
211
+ latest_build .error IS NOT NULL ) OR
212
+ (latest_build .completed_at IS NOT NULL AND
213
+ latest_build .error IS NOT NULL )
214
+
215
+ WHEN @status = ' canceling' THEN
216
+ latest_build .canceled_at IS NOT NULL AND
217
+ latest_build .completed_at IS NULL
218
+
219
+ WHEN @status = ' canceled' THEN
220
+ latest_build .canceled_at IS NOT NULL AND
221
+ latest_build .completed_at IS NOT NULL
222
+
223
+ WHEN @status = ' deleted' THEN
224
+ latest_build .started_at IS NOT NULL AND
225
+ latest_build .canceled_at IS NULL AND
226
+ latest_build .completed_at IS NOT NULL AND
227
+ latest_build .updated_at - INTERVAL ' 30 seconds' < NOW() AND
228
+ latest_build .transition = ' delete' ::workspace_transition
229
+
230
+ WHEN @status = ' deleting' THEN
231
+ latest_build .completed_at IS NOT NULL AND
232
+ latest_build .canceled_at IS NULL AND
233
+ latest_build .error IS NULL AND
234
+ latest_build .transition = ' delete' ::workspace_transition
235
+
236
+ ELSE
237
+ true
238
+ END
239
+ ELSE true
240
+ END
241
+ -- Filter by owner_id
242
+ AND CASE
243
+ WHEN @owner_id :: uuid != ' 00000000-00000000-00000000-00000000' THEN
244
+ owner_id = @owner_id
245
+ ELSE true
246
+ END
247
+ -- Filter by owner_name
248
+ AND CASE
249
+ WHEN @owner_username :: text != ' ' THEN
250
+ owner_id = (SELECT id FROM users WHERE lower (username) = lower (@owner_username) AND deleted = false)
251
+ ELSE true
252
+ END
253
+ -- Filter by template_name
254
+ -- There can be more than 1 template with the same name across organizations.
255
+ -- Use the organization filter to restrict to 1 org if needed.
256
+ AND CASE
257
+ WHEN @template_name :: text != ' ' THEN
258
+ template_id = ANY(SELECT id FROM templates WHERE lower (name) = lower (@template_name) AND deleted = false)
259
+ ELSE true
260
+ END
261
+ -- Filter by template_ids
262
+ AND CASE
263
+ WHEN array_length(@template_ids :: uuid[], 1 ) > 0 THEN
264
+ template_id = ANY(@template_ids)
265
+ ELSE true
266
+ END
267
+ -- Filter by name, matching on substring
268
+ AND CASE
269
+ WHEN @name :: text != ' ' THEN
270
+ name ILIKE ' %' || @name || ' %'
271
+ ELSE true
272
+ END
273
+ -- Authorize Filter clause will be injected below in GetAuthorizedWorkspaceCount
274
+ -- @authorize_filter
275
+ ;
276
+
148
277
-- name: GetWorkspaceByOwnerIDAndName :one
149
278
SELECT
150
279
*
0 commit comments