Skip to content

fix(api): UNIQUE 違反を事前検証し 409 で返す(schedules 同名 / memos position)(#1351)#1385

Merged
Kewton merged 2 commits into
developfrom
fix/1351-unique-409
Jul 17, 2026
Merged

fix(api): UNIQUE 違反を事前検証し 409 で返す(schedules 同名 / memos position)(#1351)#1385
Kewton merged 2 commits into
developfrom
fix/1351-unique-409

Conversation

@Kewton

@Kewton Kewton commented Jul 17, 2026

Copy link
Copy Markdown
Owner

概要

Closes #1351

事前チェックが UNIQUE 制約を覆っておらず不透明な 500 になっていた2箇所を修正。

  • schedules: UNIQUE(worktree_id, name) の同名重複を事前チェックせず生 INSERT → 一律 500
  • memos: UNIQUE(worktree_id, position) の position 明示指定分岐が未検査

変更

両者に事前チェックを追加し 409 で明示エラーを返す。あわせて INSERT 側でも UNIQUE 違反を捕捉してエラーコード変換(external-apps/db.ts の DUPLICATE 変換に倣う)。

品質(exit code 実測)

lint 0 / tsc 0 / CI=true test:unit 0 / build 0

🤖 Generated with Claude Code

2つの POST 経路が DB の UNIQUE 制約を事前チェックせず生 INSERT していたため、
制約違反が原因不明の 500 として表面化していた。

- schedules: scheduled_executions は UNIQUE(worktree_id, name)。
  POST /api/worktrees/:id/schedules は同名重複を検査せず INSERT していたため、
  catch の一律 500 に落ちていた。INSERT 前に重複を検証し 409 DUPLICATE_NAME を返す。
- memos: worktree_memos は UNIQUE(worktree_id, position)。
  POST /api/worktrees/:id/memos は position 明示指定の分岐だけ空きチェックを
  通っておらず(自動採番分岐のみ usedPositions を参照)、UNIQUE 違反が 500 に
  なっていた。明示 position を事前検証し 409 DUPLICATE_POSITION を返す。

あわせて INSERT 側でも UNIQUE 違反を捕捉してエラーコードへ変換する多重防御を
追加した(createMemo は src/lib/external-apps/db.ts の ExternalAppDbError('DUPLICATE')
に倣い MemoDbError('DUPLICATE_POSITION') を throw、schedules 経路は catch 内で
UNIQUE constraint を検出して 409 化)。事前チェックと INSERT の間の競合でも
500 に戻らない。制約は worktree スコープのため、別 worktree での同名・同 position は
従来どおり許可される。FOREIGN KEY 等その他の DB エラーは素通しで 500 のまま。

回帰テスト:
- tests/integration/api/schedules.test.ts(新規): 同名で 409 DUPLICATE_NAME、
  happy path 201、別 worktree での同名許可
- tests/integration/api/memos.test.ts: 明示 position 重複で 409 DUPLICATE_POSITION、
  未指定時の自動採番回帰
- src/lib/__tests__/db-memo.test.ts: createMemo が MemoDbError(DUPLICATE_POSITION) を throw

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Kewton Kewton added the bug Something isn't working label Jul 17, 2026
@Kewton
Kewton merged commit a54301e into develop Jul 17, 2026
9 checks passed
@Kewton
Kewton deleted the fix/1351-unique-409 branch July 17, 2026 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant