mirror of
https://github.com/coder/coder.git
synced 2026-06-07 06:58:17 +00:00
chore: add db query to retrieve workspaces & their agents (#14792)
Second PR for #14716. Adds a query that, given a user ID, returns all the workspaces they own, that can also be `ActionRead` by the requesting user. ``` type GetWorkspacesAndAgentsByOwnerIDRow struct { WorkspaceID uuid.UUID `db:"workspace_id" json:"workspace_id"` WorkspaceName string `db:"workspace_name" json:"workspace_name"` JobStatus ProvisionerJobStatus `db:"job_status" json:"job_status"` Transition WorkspaceTransition `db:"transition" json:"transition"` Agents []AgentIDNamePair `db:"agents" json:"agents"` } ``` `JobStatus` and `Transition` are set using the latest build/job of the workspace. Deleted workspaces are not included.
This commit is contained in:
@@ -690,3 +690,42 @@ UPDATE workspaces SET favorite = true WHERE id = @id;
|
||||
|
||||
-- name: UnfavoriteWorkspace :exec
|
||||
UPDATE workspaces SET favorite = false WHERE id = @id;
|
||||
|
||||
-- name: GetWorkspacesAndAgentsByOwnerID :many
|
||||
SELECT
|
||||
workspaces.id as id,
|
||||
workspaces.name as name,
|
||||
job_status,
|
||||
transition,
|
||||
(array_agg(ROW(agent_id, agent_name)::agent_id_name_pair) FILTER (WHERE agent_id IS NOT NULL))::agent_id_name_pair[] as agents
|
||||
FROM workspaces
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT
|
||||
workspace_id,
|
||||
job_id,
|
||||
transition,
|
||||
job_status
|
||||
FROM workspace_builds
|
||||
JOIN provisioner_jobs ON provisioner_jobs.id = workspace_builds.job_id
|
||||
WHERE workspace_builds.workspace_id = workspaces.id
|
||||
ORDER BY build_number DESC
|
||||
LIMIT 1
|
||||
) latest_build ON true
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT
|
||||
workspace_agents.id as agent_id,
|
||||
workspace_agents.name as agent_name,
|
||||
job_id
|
||||
FROM workspace_resources
|
||||
JOIN workspace_agents ON workspace_agents.resource_id = workspace_resources.id
|
||||
WHERE job_id = latest_build.job_id
|
||||
) resources ON true
|
||||
WHERE
|
||||
-- Filter by owner_id
|
||||
workspaces.owner_id = @owner_id :: uuid
|
||||
AND workspaces.deleted = false
|
||||
-- Authorize Filter clause will be injected below in GetAuthorizedWorkspacesAndAgentsByOwnerID
|
||||
-- @authorize_filter
|
||||
GROUP BY workspaces.id, workspaces.name, latest_build.job_status, latest_build.job_id, latest_build.transition;
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user