-- Orgs, workspaces, memberships and agent assignments: the durable half of -- the tenant boundary contract (internal/domain/org). IDs are caller-provided -- slugs; workspaces are unique per org, memberships per identity+org+workspace -- (schema 0012), and an agent assignment is unique per identity. -- name: InsertOrg :exec INSERT INTO orgs (id, name) VALUES ($1, $2); -- name: GetOrg :one SELECT id, name, created_at, updated_at FROM orgs WHERE id = $1; -- name: ListOrgs :many SELECT id, name, created_at, updated_at FROM orgs ORDER BY created_at, id; -- name: InsertWorkspace :exec INSERT INTO workspaces (id, org_id, name, environment) VALUES ($1, $2, $3, $4); -- name: ListWorkspaces :many SELECT id, org_id, name, environment, created_at, updated_at FROM workspaces WHERE org_id = $1 ORDER BY created_at, id; -- name: EnsureMembership :exec -- Grant or change a role: a membership that already exists moves to the -- supplied role, so the same call is both create and update. INSERT INTO memberships (identity_id, org_id, workspace_id, role) VALUES ($1, $2, $3, $4) ON CONFLICT (identity_id, org_id, COALESCE(workspace_id, '')) DO UPDATE SET role = excluded.role; -- name: ListMembers :many SELECT m.identity_id, i.kind, i.display_name, m.workspace_id, m.role FROM memberships m JOIN identities i ON i.id = m.identity_id WHERE m.org_id = $1 ORDER BY m.created_at, m.identity_id, m.workspace_id; -- name: RemoveMembership :exec -- The workspace is matched through the same COALESCE the unique index uses, so -- an org-wide membership (NULL) and the empty string are the same target. DELETE FROM memberships WHERE identity_id = $1 AND org_id = $2 AND COALESCE(workspace_id, '') = sqlc.arg(workspace_id)::text; -- name: ListOrgAgents :many SELECT identity_id, org_id FROM org_agents WHERE org_id = $1 ORDER BY identity_id; -- name: ListMembershipsByIdentity :many SELECT identity_id, org_id, workspace_id, role, created_at FROM memberships WHERE identity_id = $1 ORDER BY created_at, org_id; -- name: UpsertAgentAssignment :exec INSERT INTO org_agents (identity_id, org_id) VALUES ($1, $2) ON CONFLICT (identity_id) DO UPDATE SET org_id = excluded.org_id; -- name: ResolveIdentityOrg :one -- The assigned org wins for an agent; otherwise the identity's oldest -- membership decides; an identity in no org belongs to the default org. SELECT COALESCE( (SELECT a.org_id FROM org_agents a WHERE a.identity_id = $1), (SELECT m.org_id FROM memberships m WHERE m.identity_id = $1 ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1), 'org-sys' )::text AS org_id; -- name: EnsureAgentOrgMembership :exec -- Every agent identity serves its org with the shipped developer role: an -- agent is granted access the way a user is -- (docs/prds/orgs-and-access.md, "Identity"). Idempotent, so an operator's -- later role change survives a restart, and the shipped seed never -- overwrites it. INSERT INTO memberships (identity_id, org_id, role) SELECT a.identity_id, a.org_id, 'developer' FROM org_agents a ON CONFLICT (identity_id, org_id, COALESCE(workspace_id, '')) DO NOTHING; -- name: ListOrgOwners :many SELECT identity_id FROM memberships WHERE org_id = $1 AND workspace_id IS NULL AND role = 'owner' ORDER BY identity_id;