fix(db): drop stale automations v2_project_id FK blocking automation creation

Merged
SA
4mo ago
+6571-3

Problem

Creating an automation fails with a raw Failed query: insert into "automations" ... error in the create dialog (reported via Discord).

Since #5731, projects are local-first — host.db is authoritative and cloud v2_projects sync is retired, so most projects have no cloud row. That PR relaxed verifyProjectInOrg to treat a missing cloud row as fine, but automations.v2_project_id still carried a hard FK to v2_projects. Validation passes, then the insert dies on automations_v2_project_id_v2_projects_id_fk — so automation creation is broken for every local-first project.

Fix

Drop the FK (column stays NOT NULL). This mirrors #5741, which dropped the analogous workspace-pin FK for the same reason: the cloud table is a best-effort mirror, the host is authoritative, and the pin is metadata — a dangling pin surfaces as a readable host-side error at dispatch.

Verification

On a fresh Neon branch from production (br-snowy-band-afgc8x2r):

  • Confirmed the FK present, applied 0062_drop_automations_v2_project_fk.sql via drizzle-kit migrate, confirmed the FK gone.
  • Reproduced the failing case: an automations insert pinned to a project id with no v2_projects row now succeeds (rolled back after).

Lint and typecheck clean.

https://claude.ai/code/session_0152X97bwXahNYQtSZLDS5zK


Summary by cubic

Drop the stale automations.v2_project_id → v2_projects FK to unblock automation creation in local-first projects. The column remains NOT NULL; the project pin is validated on the host.

  • Bug Fixes
    • Added migration packages/db/drizzle/0062_drop_automations_v2_project_fk.sql to drop automations_v2_project_id_v2_projects_id_fk.
    • Updated packages/db/src/schema/schema.ts to remove .references(() => v2Projects.id) from v2ProjectId.
    • Automations can now be created even when no cloud v2_projects row exists.

Summary by CodeRabbit

  • Bug Fixes

    • Updated automation project relationships to prevent automatic deletion when a referenced project is removed.
    • Preserved the requirement that every automation has an associated project identifier.
  • Chores

    • Updated database migration metadata and schema history to reflect the relationship change.