Sphere audit log
Purpose
TBD by human
Identity & key fields
- Primary key:
id(uuid, defaultgen_random_uuid()). sphereId(nullable uuid, FK →activities.spheres.id, on-delete set null) — null if the sphere was hard-deleted.sphereCode(text, NOT NULL) — preserved even after sphere deletion.actorUserId(uuid, NOT NULL) — rawsubclaim from the super-admin Supabase project. INTENTIONAL: no.references()declared (per schema comment).action(enumsphere_audit_action:CREATE,UPDATE,DELETE).before,after(nullable jsonb).createdAt(timestamptz, NOT NULL).- Indexes:
sphere_audit_log_sphere_id_created_idxon(sphereId, createdAt DESC);sphere_audit_log_sphere_code_created_idxon(sphereCode, createdAt DESC).
business meaning: TBD by human
Invariants
sphereCode,actorUserId,action,createdAtNOT NULL (enforced in tktspace-backend/libs/shared/data-access-db/src/lib/schema/activities.schema.ts).sphereIdFK with ON DELETE SET NULL — log rows survive sphere deletion (enforced in tktspace-backend/libs/shared/data-access-db/src/lib/schema/activities.schema.ts).actorUserIdhas NO FK by design — super-admin auth uses a separate Supabase project fromusers.users(per schema comment in tktspace-backend/libs/shared/data-access-db/src/lib/schema/activities.schema.ts).
business invariants: TBD by human
Lifecycle
Append-only. No status column.
Relationships
- Sphere (ENT-037) —
sphereId→activities.spheres.id, on-delete set null. N:1, nullable for deleted spheres. actorUserId— references a super-admin user in a separate Supabase project (no DB-level FK).
API surfaces
| Surface | Exposed | Notes |
|---|---|---|
| client | no | — |
| business | no | — |
| super-admin | yes — /spheres/{id}/audit (SphereAuditEntryDto, SphereAuditAction) | Swagger UI |
Known gotchas / open questions
actorUserIdis intentionally not FK-linked tousers.users.id— super-admin auth is a separate Supabase project. Joins back touserswill not work.before/afterare free-form JSONB snapshots — schema TBD. Post-migration 0046 thedefaultActivityTypeandallowedActivityTypesfields captured in these snapshots will carry enum string values (same wire format as before, but the DB-level column is now enum-typed).- Two indexes — by id and by code — let queries survive the sphere being hard-deleted.
869dpxbj6(migration0049_worried_rattler.sql) DELETEd theCINEMAandSHOWSsphere rows after consolidating them into a newEVENTSrow. Audit-log entries that previously referenced the CINEMA / SHOWS sphere ids have theirsphere_idcolumn NULLed by theON DELETE SET NULLFK;sphere_code(text, NOT NULL) preserves the readable history ('CINEMA'/'SHOWS'strings remain queryable). Joins throughsphere_idreturn NULL for those rows; group-by-sphere_codequeries continue to work. This is expected join-loss, not data-loss.