98 lines
4.4 KiB
SQL
98 lines
4.4 KiB
SQL
-- Identity persistence. The domain owns lifecycle semantics (internal/domain/
|
|
-- identity); these queries are the durable half of that contract. Aliases are
|
|
-- unique on lower(alias) rather than the case-sensitive PRIMARY KEY, matching
|
|
-- the SQLite schema's COLLATE NOCASE.
|
|
|
|
-- name: ListIdentities :many
|
|
-- A scoped read sees the identities that are members of, or agents assigned
|
|
-- to, its org.
|
|
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
|
|
FROM identities
|
|
WHERE (sqlc.narg(scope_org)::text IS NULL
|
|
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = identities.id AND m.org_id = sqlc.narg(scope_org))
|
|
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = identities.id AND a.org_id = sqlc.narg(scope_org)))
|
|
ORDER BY created_at, id;
|
|
|
|
-- name: GetIdentity :one
|
|
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
|
|
FROM identities WHERE id = $1;
|
|
|
|
-- name: GetVisibleIdentity :one
|
|
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
|
|
FROM identities WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL
|
|
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = identities.id AND m.org_id = sqlc.narg(scope_org))
|
|
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = identities.id AND a.org_id = sqlc.narg(scope_org)));
|
|
|
|
-- name: ResolveIdentityAlias :one
|
|
SELECT i.id, i.kind, i.display_name, i.lifecycle, i.version, i.created_at, i.updated_at
|
|
FROM identities i JOIN identity_aliases a ON a.identity_id = i.id
|
|
WHERE lower(a.alias) = lower(sqlc.arg(alias)) AND (sqlc.narg(scope_org)::text IS NULL
|
|
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = i.id AND m.org_id = sqlc.narg(scope_org))
|
|
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = i.id AND a.org_id = sqlc.narg(scope_org)));
|
|
|
|
-- name: InsertIdentity :exec
|
|
INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7);
|
|
|
|
-- name: InsertIdentityAlias :exec
|
|
INSERT INTO identity_aliases (alias, identity_id) VALUES ($1, $2);
|
|
|
|
-- name: InsertIdentityAliasIgnoreConflict :exec
|
|
INSERT INTO identity_aliases (alias, identity_id) VALUES ($1, $2) ON CONFLICT DO NOTHING;
|
|
|
|
-- name: InsertIdentityEvent :exec
|
|
INSERT INTO identity_events (identity_id, event_type, from_lifecycle, to_lifecycle, display_name, actor_id, source, request_id, at, org_id)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);
|
|
|
|
-- name: IdentityAuditOrgCount :one
|
|
-- How many distinct orgs one identity belongs to, across memberships and agent
|
|
-- assignments. Exactly one makes its events attributable to that org; two or
|
|
-- more mean an event is not attributable to a single org.
|
|
SELECT count(DISTINCT org_id)::int AS org_count
|
|
FROM (
|
|
SELECT m.org_id FROM memberships m WHERE m.identity_id = $1
|
|
UNION
|
|
SELECT a.org_id FROM org_agents a WHERE a.identity_id = $1
|
|
) belonging;
|
|
|
|
-- name: IdentityAuditOrg :one
|
|
-- The identity's org, read only when IdentityAuditOrgCount returns one.
|
|
SELECT min(org_id)::text AS org_id
|
|
FROM (
|
|
SELECT m.org_id FROM memberships m WHERE m.identity_id = $1
|
|
UNION
|
|
SELECT a.org_id FROM org_agents a WHERE a.identity_id = $1
|
|
) belonging;
|
|
|
|
-- name: UpdateIdentity :execrows
|
|
UPDATE identities SET display_name = $2, lifecycle = $3, version = $4, updated_at = $5
|
|
WHERE id = $1 AND version = $6;
|
|
|
|
-- name: ResolveIdentitySubject :one
|
|
SELECT i.id, i.kind, i.display_name, i.lifecycle, i.version, i.created_at, i.updated_at
|
|
FROM identities i JOIN identity_subjects b ON b.identity_id = i.id
|
|
WHERE b.issuer = $1 AND b.subject = $2;
|
|
|
|
-- name: ClaimIdentitySubject :one
|
|
-- Moves a placeholder binding onto the subject the provider asserts, in one
|
|
-- statement, so only the first claimant gets it.
|
|
UPDATE identity_subjects SET subject = @subject, bound_at = @bound_at
|
|
WHERE issuer = @issuer AND subject = @placeholder
|
|
RETURNING identity_id;
|
|
|
|
-- name: UpsertIdentitySubject :exec
|
|
INSERT INTO identity_subjects (issuer, subject, identity_id, bound_at)
|
|
VALUES ($1, $2, $3, $4)
|
|
ON CONFLICT (issuer, subject) DO UPDATE SET identity_id = excluded.identity_id, bound_at = excluded.bound_at;
|
|
|
|
-- name: UpdateTaskIdentity :exec
|
|
UPDATE tasks SET identity = $1 WHERE identity = $2;
|
|
|
|
-- name: ListIdentitySubjects :many
|
|
SELECT subject, bound_at FROM identity_subjects
|
|
WHERE identity_id = $1 AND issuer = $2
|
|
ORDER BY bound_at DESC;
|
|
|
|
-- name: DeleteIdentitySubject :execrows
|
|
DELETE FROM identity_subjects WHERE identity_id = $1 AND issuer = $2 AND subject = $3;
|