Files
multica/server/migrations/046_agent_unique_name.up.sql
joyanup 3ea6b5c7b8 fix(agent): return 409 on duplicate agent name (#1182)
- Migration 046 adds UNIQUE(workspace_id, name) with dedup (keep most recently updated)
- CreateAgent handler returns 409 Conflict scoped to constraint name agent_workspace_name_unique
- Dedup verified as (0 rows) against worktree DB; rerun against staging/production before applying
- Down migration drops the constraint only; deleted rows and cascaded data are not restored

Co-authored-by: Anup Joy <joyanup@gmail.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-17 13:40:02 +08:00

23 lines
874 B
SQL

-- Migration: 046_agent_unique_name
-- Enforces uniqueness of agent names within a workspace so that the API can
-- return a clear 409 Conflict instead of a silent duplicate or a 500 error.
--
-- Step 1 deduplicates any existing rows that would violate the constraint,
-- keeping the most recently updated agent when names collide.
-- Step 2 adds the constraint so future inserts are rejected at the DB level.
--
-- See: docs/improvements.md PR-3, docs/pr-strategy.md Milestone 1
-- Step 1: delete duplicates, keep the most recently updated one
DELETE FROM agent a
USING (
SELECT id,
ROW_NUMBER() OVER (PARTITION BY workspace_id, name ORDER BY updated_at DESC) AS rn
FROM agent
) ranked
WHERE a.id = ranked.id AND ranked.rn > 1;
-- Step 2: add the constraint
ALTER TABLE agent
ADD CONSTRAINT agent_workspace_name_unique UNIQUE (workspace_id, name);