feat(db): add automation trigger and event tables
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.
0068 | automations_id_org_unique |
0069 | trigger kind enum, +debounced +rejected, both new tables, automation_runs columns, index swap |
0070 | backfill 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.
provideristext, notintegration_provider, sowebhookandsupersetevents have a home- dedupe is
UNIQUE NULLS NOT DISTINCT (integration_connection_id, provider, external_event_id)— connection-scoped, because a customer-chosenIdempotency-Keycan legitimately collide across orgs. The Linear receiver already works around this by prefixing the connection id intoeventId webhook_event_idis a plain pointer, not an FK, soingest.webhook_eventsstays 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:
| Assertion | Result |
|---|---|
ON CONFLICT with predicate | dedupes |
ON CONFLICT without predicate | errors — what #6485 fixes |
Event run with null scheduled_for | accepted |
| Trigger naming another org's automation | rejected |
kind='github' with config.kind='slack' | rejected |
| Two null-connection events, same idempotency key | rejected |
| Backfill re-run | inserts 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;configstores trigger data;next_run_atstays a column for scheduler indexing; composite FK(automation_id, organization_id); CHECK keepsconfig.kindandkindin sync; dispatcher and matcher indexes.automation_events: normalized event log; connection-scoped dedupe viaUNIQUE NULLS NOT DISTINCT (integration_connection_id, provider, external_event_id);provideristext;webhook_event_idis a non-FK pointer; org/connection FKs and org/received/resource indexes.automation_runs: addstrigger_id,event_id,resource_key, and composite FK;scheduled_foris 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 statusesdebouncedandrejected;apps/desktopmaps both.
Review notes
- Migrations in
packages/db:0070addsautomations(id, organization_id)unique;0071adds enums, new tables, and altersautomation_runs;0072backfills 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.