Files
coder/coderd/database/queries/organizations.sql
T
Steven Masley 1451f5281b feat(coderd): add organizations.default_org_member_roles for Gateway Accounts
Adds the column that drives per-org Gateway Accounts behavior. Effective
roles for an org member at request time are now the union of
organization_members.roles and organizations.default_org_member_roles,
so changes to the org default propagate to every member on the next
request. The deployment-wide default is
'organization-workspace-access', matching today's effective behavior.

The PATCH organization handler accepts the new field but rejects
deviations from the deployment default unless the
minimum-implicit-member experiment is enabled. The experiment constant
ships in this PR so the write-gating has something to check; the floor
shrink behavior lands in a follow-up.

Refs #25936. Stacks on #25929.
2026-06-02 18:40:39 +00:00

157 lines
3.0 KiB
SQL

-- name: GetDefaultOrganization :one
SELECT
*
FROM
organizations
WHERE
is_default = true
LIMIT
1;
-- name: GetOrganizations :many
SELECT
*
FROM
organizations
WHERE
-- Optionally include deleted organizations
deleted = @deleted
-- Filter by ids
AND CASE
WHEN array_length(@ids :: uuid[], 1) > 0 THEN
id = ANY(@ids)
ELSE true
END
AND CASE
WHEN @name::text != '' THEN
LOWER("name") = LOWER(@name)
ELSE true
END
;
-- name: GetOrganizationByID :one
SELECT
*
FROM
organizations
WHERE
id = $1;
-- name: GetOrganizationByName :one
SELECT
*
FROM
organizations
WHERE
-- Optionally include deleted organizations
deleted = @deleted AND
LOWER("name") = LOWER(@name)
LIMIT
1;
-- name: GetOrganizationsByUserID :many
SELECT
*
FROM
organizations
WHERE
-- Optionally provide a filter for deleted organizations.
CASE WHEN
sqlc.narg('deleted') :: boolean IS NULL THEN
true
ELSE
deleted = sqlc.narg('deleted')
END AND
id = ANY(
SELECT
organization_id
FROM
organization_members
WHERE
user_id = $1
);
-- name: GetOrganizationResourceCountByID :one
SELECT
(
SELECT
count(*)
FROM
workspaces
WHERE
workspaces.organization_id = $1
AND workspaces.deleted = FALSE) AS workspace_count,
(
SELECT
count(*)
FROM
GROUPS
WHERE
groups.organization_id = $1) AS group_count,
(
SELECT
count(*)
FROM
templates
WHERE
templates.organization_id = $1
AND templates.deleted = FALSE) AS template_count,
(
SELECT
count(*)
FROM
organization_members
LEFT JOIN users ON organization_members.user_id = users.id
WHERE
organization_members.organization_id = $1
AND users.deleted = FALSE) AS member_count,
(
SELECT
count(*)
FROM
provisioner_keys
WHERE
provisioner_keys.organization_id = $1) AS provisioner_key_count;
-- name: InsertOrganization :one
INSERT INTO
organizations (id, "name", display_name, description, icon, created_at, updated_at, is_default, default_org_member_roles)
VALUES
-- If no organizations exist, and this is the first, make it the default.
(@id, @name, @display_name, @description, @icon, @created_at, @updated_at, (SELECT TRUE FROM organizations LIMIT 1) IS NULL, @default_org_member_roles) RETURNING *;
-- name: UpdateOrganization :one
UPDATE
organizations
SET
updated_at = @updated_at,
name = @name,
display_name = @display_name,
description = @description,
icon = @icon,
default_org_member_roles = @default_org_member_roles
WHERE
id = @id
RETURNING *;
-- name: UpdateOrganizationDeletedByID :exec
UPDATE organizations
SET
deleted = true,
updated_at = @updated_at
WHERE
id = @id AND
is_default = false;
-- name: UpdateOrganizationWorkspaceSharingSettings :one
UPDATE
organizations
SET
shareable_workspace_owners = @shareable_workspace_owners,
updated_at = @updated_at
WHERE
id = @id
RETURNING *;