Files
loki/drizzle/0006_project_states_align_schema.sql
T
g-butandClaude Opus 4.7 3075f7a06c fix(db): align project_states with declared schema (composite PK + missing cols)
The daemon's PATCH /api/project-states/<tab> was returning HTTP 500 against
production with "insert into project_states (...lock_at, agent_running,
tab_open, active_agents...)" — none of those columns existed on prod, plus
the PK was still the legacy single-column form, so the daemon could never
record agent lifecycle (readyAt/closingAt/closedAt) for cloud-mode
deployments. The agent stop hook's patch_project_state was a silent no-op.

Migration adds the four missing runtime-state columns with sane defaults
(agent_running=false, tab_open=false, active_agents='{}'::text[], nullable
lock_at), re-applies the 0002 composite-PK swap idempotently, and verified
against the live Neon DB:

  HTTP 200 {"success":true,"data":{...,"agentRunning":false,"tabOpen":false,
  "activeAgents":[],"readyAt":"...","lockAt":null,...}}

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-19 21:27:49 +02:00

30 lines
1.5 KiB
SQL

-- Schema drift fix: src/db/schema/project-states.ts declares agent_running,
-- tab_open, active_agents, lock_at as part of the live runtime state pushed
-- by the daemon every few seconds, but no migration ever added them. As a
-- result, every PATCH /api/project-states/<tab> from the local stop hook
-- returns 500 ("insert into project_states (...lock_at, agent_running...)"
-- on the prod instance) and the agent lifecycle is never recorded.
--
-- 0002 was supposed to switch project_states to a composite PK
-- (user_id, project_key) — this is idempotent re-application of that step
-- for instances where it never ran.
-- 1. Drop orphan rows so the composite-PK switch can succeed.
DELETE FROM "project_states" WHERE "user_id" IS NULL;
-- 2. user_id must participate in the composite PK.
ALTER TABLE "project_states" ALTER COLUMN "user_id" SET NOT NULL;
-- 3. Swap the PK to the composite shape Drizzle expects.
ALTER TABLE "project_states" DROP CONSTRAINT IF EXISTS "project_states_pkey";
ALTER TABLE "project_states"
ADD CONSTRAINT "project_states_pkey" PRIMARY KEY ("user_id", "project_key");
-- 4. Add the runtime-state columns that the daemon push payload writes.
-- NOT NULL with sane defaults so existing rows backfill harmlessly.
ALTER TABLE "project_states"
ADD COLUMN IF NOT EXISTS "agent_running" boolean NOT NULL DEFAULT false,
ADD COLUMN IF NOT EXISTS "tab_open" boolean NOT NULL DEFAULT false,
ADD COLUMN IF NOT EXISTS "active_agents" text[] NOT NULL DEFAULT '{}',
ADD COLUMN IF NOT EXISTS "lock_at" timestamp with time zone;