feat: email_send_log gains subject + trigger_run_id (+ subject backfill)

SW
4mo ago
+46-0

Implements row 2 of recoupable/chat#1958.

What changed

One migration on email_send_log:

  • subject text NULL — denormalized from the send request, so the runs list reads it without parsing raw_body per row
  • trigger_run_id text NULL — the Trigger.dev run id of the scheduled task that produced the send; NULL for interactive sends
  • Partial index on trigger_run_id WHERE NOT NULL — serves the GET /api/tasks/runs list-mode annotation (IN (runIds)); most rows are interactive sends, so the partial form stays small
  • Backfill: raw_body stores the POST /api/emails request JSON verbatim, so historical subjects are recoverable — done in a DO block with per-row exception handling (rejected attempts can store invalid JSON; a bad row is skipped, never fatal). Run linkage is NOT backfillable — pre-threading runs fall back to their schedule's title, by design.

Sequencing

Merges after docs#268 (contract), before the api write path (row 4) and read path (api#764, row 5) that use these columns.

🤖 Generated with Claude Code


Summary by cubic

Adds subject and trigger_run_id to email_send_log to denormalize email subjects and link sends to their Trigger.dev run. No runtime behavior change yet; enables run lists to read subjects without parsing raw_body and to filter/annotate by run id once adopted.

  • Adds subject text NULL and trigger_run_id text NULL, plus a partial index on trigger_run_id IS NOT NULL.
  • Backfills subject from raw_body JSON when present; per-row errors are skipped. trigger_run_id is not backfilled.
  • Deploy after docs contract and before the API write/read paths that use these fields (chat#1958). No config changes or manual actions beyond running the migration.

Summary by CodeRabbit

  • New Features
    • Email delivery records can now include the email subject and associated trigger run ID.
    • Existing records are automatically backfilled with subjects when valid data is available.
    • Improved lookup support for emails linked to trigger runs.