feat(db): add automation trigger and event tables

Merged
SA
3mo ago
+20160-6

Stacked on #6485. Base will retarget to main once that merges.

What

Schema only. Three migrations, no behaviour change — nothing reads the new tables and the dispatcher still uses automations.rrule / next_run_at.

0068automations_id_org_unique
0069trigger kind enum, +debounced +rejected, both new tables, automation_runs columns, index swap
0070backfill one schedule trigger per automation

automation_triggers

One automation, many triggers. Schedule fields move into config jsonb; next_run_at stays a column because the dispatcher indexes and sorts on it.

  • composite FK (automation_id, organization_id) → automations (id, organization_id), so a trigger cannot name an automation in another org
  • CHECK (config->>'kind' = kind::text), so the indexed column and the parsed config cannot disagree

automation_events

The normalized log the matcher will read.

  • provider is text, not integration_provider, so webhook and superset events have a home
  • dedupe is UNIQUE NULLS NOT DISTINCT (integration_connection_id, provider, external_event_id) — connection-scoped, because a customer-chosen Idempotency-Key can legitimately collide across orgs. The Linear receiver already works around this by prefixing the connection id into eventId
  • webhook_event_id is a plain pointer, not an FK, so ingest.webhook_events stays prunable

automation_runs

Gains trigger_id, event_id, resource_key, and a composite FK. scheduled_for becomes nullable for event runs, which splits automation_runs_dedup_idx into two partial uniques plus a debounce index.

Why 0068 is its own migration

drizzle-kit emits both composite FKs before the unique constraint they reference. Generated as one migration it fails:

ERROR: there is no unique constraint matching given keys for referenced table "automations"

Splitting it means the prerequisite exists first.

Rehearsal

All three applied to a Postgres 16 baseline seeded with the current table shapes and real-shaped rows. After applying:

AssertionResult
ON CONFLICT with predicatededupes
ON CONFLICT without predicateerrors — what #6485 fixes
Event run with null scheduled_foraccepted
Trigger naming another org's automationrejected
kind='github' with config.kind='slack'rejected
Two null-connection events, same idempotency keyrejected
Backfill re-runinserts 0

Backfill output checked column by column: enabled and timezone preserved, dtstart rendered as ISO UTC.

Production data pre-check

Queried the workspace branch before adding anything that validates existing rows: 47,436 runs, 0 org mismatches, 0 orphans, 0 null scheduled_for. Both composite FKs and the partial dedupe index validate against existing data rather than failing mid-apply.

Testing

bun run typecheck 37/37 · bun run lint clean · bun test packages/trpc packages/db apps/api 30 pass

Not in this PR

Dual-write, dispatcher cutover, trigger CRUD, and dropping the legacy columns. Step B of the migration playbook.


Summary by cubic

Adds automation_triggers and automation_events and extends automation_runs to enable multi-source, event-driven automations. No behavior change: the dispatcher still uses automations.rrule/next_run_at; apps/desktop only adds display for new run statuses.

  • automation_triggers: many-per-automation; config stores trigger data; next_run_at stays a column for scheduler indexing; composite FK (automation_id, organization_id); CHECK keeps config.kind and kind in sync; dispatcher and matcher indexes.
  • automation_events: normalized event log; connection-scoped dedupe via UNIQUE NULLS NOT DISTINCT (integration_connection_id, provider, external_event_id); provider is text; webhook_event_id is a non-FK pointer; org/connection FKs and org/received/resource indexes.
  • automation_runs: adds trigger_id, event_id, resource_key, and composite FK; scheduled_for is now nullable; replaces single dedupe with two partial uniques (schedule vs event) and adds an in-flight debounce index.
  • Enums/UI: adds automation_trigger_kind; adds run statuses debounced and rejected; apps/desktop maps both.

Review notes

  • Migrations in packages/db: 0070 adds automations(id, organization_id) unique; 0071 adds enums, new tables, and alters automation_runs; 0072 backfills one schedule trigger per automation (idempotent).
  • Existing data validated: composite FKs and partial indexes succeed; no rows with null scheduled_for.

Rollout

  • Apply migrations in order: 0070 → 0071 → 0072.
  • No services read/write the new tables yet; no config changes required.
  • No migration actions beyond running the SQL; dual-write/cutover and legacy column drops follow later.

Summary by CodeRabbit

  • New Features

    • Added support for automation triggers from schedules, webhooks, GitHub, Slack, Linear, and Sentry.
    • Improved automation event handling, including deduplication and resource tracking.
    • Existing scheduled automations are automatically prepared for the new trigger system.
  • Bug Fixes

    • Automation runs now clearly distinguish between superseded (“debounced”) and blocked (“rejected”) states.
    • These states are no longer incorrectly shown as failures.