fix(state): resolve identity audit org in one statement

This commit is contained in:
sam committed 2026-10-25 21:37:50 +11:00
1 parent 274acfe6f5
commit 6c095e5ac6
3 files changed
+19 -51

No files matched your search

+10 -18
View File
@@ -50,22 +50,14 @@ func insertIdentityEventInOrg(ctx context.Context, q *postgresdb.Queries, orgID
// one would hide the event from the other org's feed. An identity with no org
// yet uses fallback, the acting org.
func auditSubjectOrg(ctx context.Context, q *postgresdb.Queries, fallback org.OrgID, id identity.IdentityID) (org.OrgID, error) {
count, err := q.IdentityAuditOrgCount(ctx, string(id))
if err != nil {
return "", fmt.Errorf("identity audit org count: %w", err)
}
switch {
case count > 1:
return "", nil
case count == 1:
orgID, err := q.IdentityAuditOrg(ctx, string(id))
if err != nil {
return "", fmt.Errorf("identity audit org: %w", err)
}
return org.OrgID(orgID), nil
default:
single, err := q.IdentityAuditOrg(ctx, string(id))
if errors.Is(err, pgx.ErrNoRows) {
return fallback, nil
}
if err != nil {
return "", fmt.Errorf("identity audit org: %w", err)
}
return org.OrgID(single), nil
}
// writeIdentityEvent records the event under orgID. An empty org is stored as
@@ -245,7 +237,7 @@ func (s *Store) Apply(ctx context.Context, id identity.IdentityID, expectedVersi
return identity.Identity{}, err
}
}
if err := insertIdentityEvent(ctx, q, org.OrgFromContext(ctx), id, ev, audit, now); err != nil {
if err := insertIdentityEvent(ctx, q, "", id, ev, audit, now); err != nil {
return identity.Identity{}, err
}
if err := tx.Commit(ctx); err != nil {
@@ -308,7 +300,7 @@ func (s *Store) ClaimSubject(ctx context.Context, placeholder, subject identity.
return err
}
claimed = identityFromRow(row)
return insertIdentityEvent(ctx, q, org.OrgFromContext(ctx), claimed.ID, identity.Event{
return insertIdentityEvent(ctx, q, "", claimed.ID, identity.Event{
IdentityID: claimed.ID, Type: "claim_subject", From: claimed.Lifecycle, To: claimed.Lifecycle, DisplayName: subject.Subject,
}, identity.Audit{ActorID: claimed.ID, Source: "sign-in", RequestID: placeholder.Subject}, now)
})
@@ -350,7 +342,7 @@ func (s *Store) BindSubject(ctx context.Context, id identity.IdentityID, subject
}); err != nil {
return err
}
if err := insertIdentityEvent(ctx, q, org.OrgFromContext(ctx), id, identity.Event{
if err := insertIdentityEvent(ctx, q, "", id, identity.Event{
IdentityID: id, Type: "bind_subject", From: cur.Lifecycle, To: cur.Lifecycle, DisplayName: subject.Subject,
}, audit, now); err != nil {
return err
@@ -402,7 +394,7 @@ func (s *Store) UnbindSubject(ctx context.Context, id identity.IdentityID, subje
return identity.ErrNotFound
}
cur := identityFromRow(current)
return insertIdentityEvent(ctx, q, org.OrgFromContext(ctx), id, identity.Event{
return insertIdentityEvent(ctx, q, "", id, identity.Event{
IdentityID: id, Type: "unbind_subject", From: cur.Lifecycle, To: cur.Lifecycle, DisplayName: subject.Subject,
}, audit, now)
})
@@ -111,9 +111,12 @@ FROM (
UNION
SELECT a.org_id FROM org_agents a WHERE a.identity_id = $1
) belonging
HAVING count(DISTINCT org_id) = 1
`
// The identity's org, read only when IdentityAuditOrgCount returns one.
// The identity's org only when it belongs to exactly one, across memberships
// and agent assignments; no row otherwise. One statement, so a concurrent
// membership add cannot make the count and the org disagree.
func (q *Queries) IdentityAuditOrg(ctx context.Context, identityID string) (string, error) {
row := q.db.QueryRow(ctx, identityAuditOrg, identityID)
var org_id string
@@ -121,25 +124,6 @@ func (q *Queries) IdentityAuditOrg(ctx context.Context, identityID string) (stri
return org_id, err
}
const identityAuditOrgCount = `-- name: IdentityAuditOrgCount :one
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
`
// 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.
func (q *Queries) IdentityAuditOrgCount(ctx context.Context, identityID string) (int32, error) {
row := q.db.QueryRow(ctx, identityAuditOrgCount, identityID)
var org_count int32
err := row.Scan(&org_count)
return org_count, err
}
const insertIdentity = `-- name: InsertIdentity :exec
INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)
@@ -44,25 +44,17 @@ INSERT INTO identity_aliases (alias, identity_id) VALUES ($1, $2) ON CONFLICT DO
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.
-- The identity's org only when it belongs to exactly one, across memberships
-- and agent assignments; no row otherwise. One statement, so a concurrent
-- membership add cannot make the count and the org disagree.
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;
) belonging
HAVING count(DISTINCT org_id) = 1;
-- name: UpdateIdentity :execrows
UPDATE identities SET display_name = $2, lifecycle = $3, version = $4, updated_at = $5