fix(db,desktop): cascade host's workspaces on host delete

Merged
KI
4mo ago
+6591-6

Problem

Deleting a host from Settings → Hosts fails whenever the host still owns workspace rows. v2_workspaces_host_fk was defined without an onDelete clause (defaults to NO ACTION), so the DELETE FROM v2_hosts in v2Host.delete is rejected by Postgres and the raw FK-violation message lands in the error toast.

This is unavoidable for stale duplicate devices: repo-root (main) workspaces can't be deleted through the normal workspace-delete action, so they pin the dead host forever.

Reported by a user (support, 2026-07-16) with a stale host left behind by an uninstalled build; they confirmed cascade-deleting the dead host's workspaces is the desired behavior.

Fix

  • Add .onDelete("cascade") to v2_workspaces_host_fk (matches the existing cascade on v2_users_hosts_host_fk). Migration 0061_v2_workspaces_host_fk_cascade drops and re-adds the constraint.
  • Update the delete-host dialog copy, which previously promised "Workspaces, files, conversations, and automations stay" — impossible under the old FK and wrong under the new behavior. It now says the host's workspaces are removed and files on the machine stay.

Downstream is safe: chat_sessions.v2_workspace_id is SET NULL, and nothing else hard-references v2_workspaces.

Notes

  • Migration generated with drizzle-kit generate; no hand-edits to packages/db/drizzle/.
  • Should be applied via the API/cloud deploy before any desktop build leans on it (standard ordering).

Verification

  • bun run typecheck passes; Biome clean on changed files.
  • Root bun run lint currently fails on unrelated pre-existing nested .conductor/* Biome root configs, not on this change.

Open in Stage

Summary by cubic

Fixes host deletion by cascading the host’s v2_workspaces on delete. Updates the desktop delete-host dialog to clarify that the host’s workspaces are removed and files on the machine stay.

  • Migration
    • Apply 0061_v2_workspaces_host_fk_cascade before shipping desktop; it recreates v2_workspaces_host_fk with ON DELETE CASCADE.
    • Downstream is safe: chat_sessions.v2_workspace_id uses SET NULL.

Summary by CodeRabbit

  • User Experience

    • Updated host deletion messaging to clearly explain that deleting a host also removes its access and associated workspaces.
    • Clarified that files remain on the machine after deletion.
    • Added a warning that a running host may reappear and that deletion cannot be undone.
  • Bug Fixes

    • Ensured associated workspaces are automatically removed when their host is deleted, keeping host and workspace data consistent.