mirror of
https://github.com/coder/coder.git
synced 2026-06-06 14:38:23 +00:00
8b125d6c5d
* feat: Implement view for workspace builds to include rbac info * Removes the need to fetch the workspace to run an rbac check. * chore: Use workspace build as RBAC object * chore: Use golang templates instead of sqlc files
129 lines
3.0 KiB
Plaintext
129 lines
3.0 KiB
Plaintext
{{ define "workspace_builds_rbac" }}
|
|
(
|
|
SELECT
|
|
workspace_builds.*,
|
|
workspaces.organization_id AS organization_id,
|
|
workspaces.owner_id AS workspace_owner_id
|
|
FROM
|
|
workspace_builds
|
|
INNER JOIN
|
|
workspaces ON workspace_builds.workspace_id = workspaces.id
|
|
)
|
|
{{ end }};
|
|
|
|
|
|
{{ define "GetWorkspaceBuild" }}
|
|
-- name: GetWorkspaceBuild :one
|
|
SELECT
|
|
*
|
|
FROM
|
|
{{ template "workspace_builds_rbac" }} workspace_builds
|
|
WHERE
|
|
CASE
|
|
WHEN @build_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
|
|
id = @build_id
|
|
ELSE true
|
|
END
|
|
AND CASE
|
|
WHEN @job_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
|
|
job_id = @job_id
|
|
ELSE true
|
|
END
|
|
AND CASE
|
|
WHEN @job_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
|
|
job_id = @job_id
|
|
ELSE true
|
|
END
|
|
AND CASE
|
|
WHEN @created_after :: timestamp with time zone != '0001-01-01 00:00:00Z' THEN
|
|
created_at > @created_after
|
|
ELSE true
|
|
END
|
|
AND CASE
|
|
WHEN @workspace_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
|
|
workspace_id = @workspace_id
|
|
ELSE true
|
|
END
|
|
AND CASE
|
|
WHEN @build_number :: integer != 0 THEN
|
|
build_number = @build_number
|
|
ELSE true
|
|
END
|
|
{{ if .Latest }}
|
|
ORDER BY
|
|
build_number desc
|
|
{{ end }}
|
|
{{ if gt .LimitOpt 0 }} LIMIT @limit_opt {{ end }}
|
|
;
|
|
{{ end }}
|
|
|
|
|
|
{{ define "GetWorkspaceBuildsByWorkspaceID" }}
|
|
-- name: GetWorkspaceBuildsByWorkspaceID :many
|
|
SELECT
|
|
*
|
|
FROM
|
|
{{ template "workspace_builds_rbac" }} workspace_builds
|
|
WHERE
|
|
workspace_builds.workspace_id = @workspace_id
|
|
AND workspace_builds.created_at > @since
|
|
AND CASE
|
|
-- This allows using the last element on a page as effectively a cursor.
|
|
-- This is an important option for scripts that need to paginate without
|
|
-- duplicating or missing data.
|
|
WHEN @after_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN (
|
|
-- The pagination cursor is the last ID of the previous page.
|
|
-- The query is ordered by the build_number field, so select all
|
|
-- rows after the cursor.
|
|
build_number > (
|
|
SELECT
|
|
build_number
|
|
FROM
|
|
workspace_builds
|
|
WHERE
|
|
id = @after_id
|
|
)
|
|
)
|
|
ELSE true
|
|
END
|
|
ORDER BY
|
|
build_number desc OFFSET @offset_opt
|
|
LIMIT
|
|
-- A null limit means "no limit", so 0 means return all
|
|
NULLIF(@limit_opt :: int, 0);
|
|
{{ end }}
|
|
|
|
{{ define "GetLatestWorkspaceBuildsByWorkspaceIDs" }}
|
|
-- name: GetLatestWorkspaceBuildsByWorkspaceIDs :many
|
|
SELECT wb.*
|
|
FROM (
|
|
SELECT
|
|
workspace_id, MAX(build_number) as max_build_number
|
|
FROM
|
|
workspace_builds
|
|
WHERE
|
|
workspace_id = ANY(@ids :: uuid [ ])
|
|
GROUP BY
|
|
workspace_id
|
|
) m
|
|
JOIN
|
|
{{ template "workspace_builds_rbac" }} wb
|
|
ON m.workspace_id = wb.workspace_id AND m.max_build_number = wb.build_number;
|
|
{{ end }}
|
|
|
|
{{ define "GetLatestWorkspaceBuilds" }}
|
|
-- name: GetLatestWorkspaceBuilds :many
|
|
SELECT wb.*
|
|
FROM (
|
|
SELECT
|
|
workspace_id, MAX(build_number) as max_build_number
|
|
FROM
|
|
workspace_builds
|
|
GROUP BY
|
|
workspace_id
|
|
) m
|
|
JOIN
|
|
{{ template "workspace_builds_rbac" }} wb
|
|
ON m.workspace_id = wb.workspace_id AND m.max_build_number = wb.build_number;
|
|
{{ end }}
|