fix: retain import actor attribution

Persist record and actor identities through job completion.\nWrite one bounded transactional completion audit per job.
This commit is contained in:
Marc-André Bombeck committed 2026-08-06 02:00:35 +02:00
1 parent adb932052c
commit 620817b10b
19 files changed
+211 -63

No files matched your search

@@ -0,0 +1,28 @@
-- Keep the record subject and the actor distinct for durable intake-import
-- work. Historical rows predate delegated imports, so their actor is the
-- record subject: the only truthful attribution available for those jobs.
ALTER TABLE "medication_intake_import_jobs"
RENAME COLUMN "user_id" TO "record_user_id";
ALTER TABLE "medication_intake_import_jobs"
RENAME CONSTRAINT "medication_intake_import_jobs_user_id_fkey"
TO "medication_intake_import_jobs_record_user_id_fkey";
ALTER TABLE "medication_intake_import_jobs"
ADD COLUMN "actor_user_id" TEXT;
UPDATE "medication_intake_import_jobs"
SET "actor_user_id" = "record_user_id"
WHERE "actor_user_id" IS NULL;
ALTER TABLE "medication_intake_import_jobs"
ALTER COLUMN "actor_user_id" SET NOT NULL;
-- No actor foreign key: a deleted delegate must remain identifiable in the
-- record's durable job and audit trail, just like audit_logs.actor_user_id.
DROP INDEX "medication_intake_import_jobs_user_id_created_at_idx";
CREATE INDEX "medication_intake_import_jobs_record_user_id_created_at_idx"
ON "medication_intake_import_jobs"("record_user_id", "created_at");
CREATE INDEX "medication_intake_import_jobs_actor_user_id_created_at_idx"
ON "medication_intake_import_jobs"("actor_user_id", "created_at");
+6 -4
View File
@@ -816,7 +816,7 @@ model User {
// pg-boss job state so the polling endpoint can read live per-
// phase progress without round-tripping through pg-boss internals.
importJobs ImportJob[]
medicationIntakeImportJobs MedicationIntakeImportJob[]
medicationIntakeImportJobs MedicationIntakeImportJob[] @relation("MedicationIntakeImportRecord")
// v1.5.0 — persistent measurement rollup table. Carries the
// count/min/max/mean/sd/slope/r2 stats per (type, granularity,
// bucketStart) so the analytics + comprehensive read paths bypass
@@ -4715,7 +4715,8 @@ model ImportJob {
// stays in Postgres so retries never depend on request-process memory.
model MedicationIntakeImportJob {
id String @id @default(cuid())
userId String @map("user_id")
recordUserId String @map("record_user_id")
actorUserId String @map("actor_user_id")
/// v1.33.0 (migration 0278) — nullable. The per-medication import route
/// scopes its job to one medication and names it here. A dose history
/// exported from another tracker covers a whole regimen in one file — the
@@ -4736,10 +4737,11 @@ model MedicationIntakeImportJob {
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
recordUser User @relation("MedicationIntakeImportRecord", fields: [recordUserId], references: [id], onDelete: Cascade)
medication Medication? @relation(fields: [medicationId], references: [id], onDelete: Cascade)
@@index([userId, createdAt])
@@index([recordUserId, createdAt])
@@index([actorUserId, createdAt])
@@index([medicationId, createdAt])
@@index([status, heartbeatAt])
@@map("medication_intake_import_jobs")