2022-04-01 15:45:23 -05:00
|
|
|
-- name: GetWorkspaceByID :one
|
|
|
|
|
SELECT
|
|
|
|
|
*
|
|
|
|
|
FROM
|
|
|
|
|
workspaces
|
|
|
|
|
WHERE
|
|
|
|
|
id = $1
|
|
|
|
|
LIMIT
|
|
|
|
|
1;
|
|
|
|
|
|
2022-06-17 00:26:40 -05:00
|
|
|
-- name: GetWorkspaces :many
|
2022-05-18 10:09:07 -05:00
|
|
|
SELECT
|
|
|
|
|
*
|
|
|
|
|
FROM
|
|
|
|
|
workspaces
|
|
|
|
|
WHERE
|
|
|
|
|
-- Optionally include deleted workspaces
|
2022-06-14 08:46:33 -05:00
|
|
|
workspaces.deleted = @deleted
|
2022-05-18 10:09:07 -05:00
|
|
|
-- Filter by owner_id
|
|
|
|
|
AND CASE
|
2022-06-14 08:46:33 -05:00
|
|
|
WHEN @owner_id :: uuid != '00000000-00000000-00000000-00000000' THEN
|
|
|
|
|
owner_id = @owner_id
|
|
|
|
|
ELSE true
|
|
|
|
|
END
|
|
|
|
|
-- Filter by owner_name
|
|
|
|
|
AND CASE
|
|
|
|
|
WHEN @owner_username :: text != '' THEN
|
2022-06-25 06:22:59 -05:00
|
|
|
owner_id = (SELECT id FROM users WHERE lower(username) = lower(@owner_username))
|
2022-06-14 08:46:33 -05:00
|
|
|
ELSE true
|
|
|
|
|
END
|
|
|
|
|
-- Filter by template_name
|
|
|
|
|
-- There can be more than 1 template with the same name across organizations.
|
|
|
|
|
-- Use the organization filter to restrict to 1 org if needed.
|
|
|
|
|
AND CASE
|
|
|
|
|
WHEN @template_name :: text != '' THEN
|
2022-06-25 06:22:59 -05:00
|
|
|
template_id = ANY(SELECT id FROM templates WHERE lower(name) = lower(@template_name))
|
2022-06-14 08:46:33 -05:00
|
|
|
ELSE true
|
|
|
|
|
END
|
|
|
|
|
-- Filter by template_ids
|
|
|
|
|
AND CASE
|
|
|
|
|
WHEN array_length(@template_ids :: uuid[], 1) > 0 THEN
|
|
|
|
|
template_id = ANY(@template_ids)
|
|
|
|
|
ELSE true
|
2022-05-18 10:09:07 -05:00
|
|
|
END
|
2022-06-06 14:43:16 -05:00
|
|
|
-- Filter by name, matching on substring
|
2022-06-03 12:20:28 -05:00
|
|
|
AND CASE
|
2022-06-14 08:46:33 -05:00
|
|
|
WHEN @name :: text != '' THEN
|
2022-06-25 06:22:59 -05:00
|
|
|
name ILIKE '%' || @name || '%'
|
2022-06-14 08:46:33 -05:00
|
|
|
ELSE true
|
2022-06-03 12:20:28 -05:00
|
|
|
END
|
2022-05-18 10:09:07 -05:00
|
|
|
;
|
2022-04-25 16:11:03 -05:00
|
|
|
|
2022-05-19 20:09:27 +01:00
|
|
|
-- name: GetWorkspacesAutostart :many
|
2022-05-11 23:03:02 +01:00
|
|
|
SELECT
|
|
|
|
|
*
|
|
|
|
|
FROM
|
|
|
|
|
workspaces
|
|
|
|
|
WHERE
|
|
|
|
|
deleted = false
|
|
|
|
|
AND
|
|
|
|
|
(
|
2022-05-19 20:09:27 +01:00
|
|
|
(autostart_schedule IS NOT NULL AND autostart_schedule <> '')
|
2022-05-11 23:03:02 +01:00
|
|
|
OR
|
2022-05-19 20:09:27 +01:00
|
|
|
(ttl IS NOT NULL AND ttl > 0)
|
2022-05-11 23:03:02 +01:00
|
|
|
);
|
|
|
|
|
|
2022-04-25 16:11:03 -05:00
|
|
|
-- name: GetWorkspaceByOwnerIDAndName :one
|
2022-04-01 15:45:23 -05:00
|
|
|
SELECT
|
|
|
|
|
*
|
|
|
|
|
FROM
|
|
|
|
|
workspaces
|
|
|
|
|
WHERE
|
|
|
|
|
owner_id = @owner_id
|
|
|
|
|
AND deleted = @deleted
|
2022-06-10 09:58:42 -05:00
|
|
|
AND LOWER("name") = LOWER(@name)
|
|
|
|
|
ORDER BY created_at DESC;
|
2022-04-01 15:45:23 -05:00
|
|
|
|
2022-04-06 12:42:40 -05:00
|
|
|
-- name: GetWorkspaceOwnerCountsByTemplateIDs :many
|
2022-04-01 15:45:23 -05:00
|
|
|
SELECT
|
2022-04-06 12:42:40 -05:00
|
|
|
template_id,
|
2022-04-01 15:45:23 -05:00
|
|
|
COUNT(DISTINCT owner_id)
|
|
|
|
|
FROM
|
|
|
|
|
workspaces
|
|
|
|
|
WHERE
|
2022-04-06 12:42:40 -05:00
|
|
|
template_id = ANY(@ids :: uuid [ ])
|
2022-07-12 12:52:28 -05:00
|
|
|
-- Ignore deleted workspaces
|
|
|
|
|
AND deleted != true
|
2022-04-01 15:45:23 -05:00
|
|
|
GROUP BY
|
2022-05-16 23:10:52 -05:00
|
|
|
template_id;
|
2022-04-01 15:45:23 -05:00
|
|
|
|
|
|
|
|
-- name: InsertWorkspace :one
|
|
|
|
|
INSERT INTO
|
|
|
|
|
workspaces (
|
|
|
|
|
id,
|
|
|
|
|
created_at,
|
|
|
|
|
updated_at,
|
|
|
|
|
owner_id,
|
2022-04-25 16:11:03 -05:00
|
|
|
organization_id,
|
2022-04-06 12:42:40 -05:00
|
|
|
template_id,
|
2022-05-23 23:31:41 +01:00
|
|
|
name,
|
|
|
|
|
autostart_schedule,
|
|
|
|
|
ttl
|
2022-04-01 15:45:23 -05:00
|
|
|
)
|
|
|
|
|
VALUES
|
2022-05-23 23:31:41 +01:00
|
|
|
($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *;
|
2022-04-01 15:45:23 -05:00
|
|
|
|
|
|
|
|
-- name: UpdateWorkspaceDeletedByID :exec
|
|
|
|
|
UPDATE
|
|
|
|
|
workspaces
|
|
|
|
|
SET
|
|
|
|
|
deleted = $2
|
|
|
|
|
WHERE
|
|
|
|
|
id = $1;
|
2022-04-07 10:03:35 +01:00
|
|
|
|
|
|
|
|
-- name: UpdateWorkspaceAutostart :exec
|
|
|
|
|
UPDATE
|
|
|
|
|
workspaces
|
|
|
|
|
SET
|
|
|
|
|
autostart_schedule = $2
|
|
|
|
|
WHERE
|
|
|
|
|
id = $1;
|
|
|
|
|
|
2022-05-19 20:09:27 +01:00
|
|
|
-- name: UpdateWorkspaceTTL :exec
|
2022-04-07 10:03:35 +01:00
|
|
|
UPDATE
|
|
|
|
|
workspaces
|
|
|
|
|
SET
|
2022-05-19 20:09:27 +01:00
|
|
|
ttl = $2
|
2022-04-07 10:03:35 +01:00
|
|
|
WHERE
|
|
|
|
|
id = $1;
|