Skip to content

fix(api): handle FK violation in version restore endpoint#1115

Merged
sw-factory-automations merged 1 commit into
mainfrom
fix/sentry-memo-2f-fk-violation-restore
May 16, 2026
Merged

fix(api): handle FK violation in version restore endpoint#1115
sw-factory-automations merged 1 commit into
mainfrom
fix/sentry-memo-2f-fk-violation-restore

Conversation

@sw-factory-automations
Copy link
Copy Markdown
Collaborator

Summary

The version restore endpoint (POST /api/pages/[pageId]/versions/[versionId]) was missing isForeignKeyViolationError handling when inserting a pre-restore snapshot into page_versions. When the referenced page was deleted between the auth check and the insert (race condition), PostgreSQL rejected the insert with a 23503 FK constraint violation, which was sent to Sentry via captureSupabaseError instead of returning a clean 404.

Sentry issue: https://ona-2j.sentry.io/issues/118825153/ (MEMO-2F) — 3 events, 2 users.

Root cause

The sibling creation endpoint (POST /api/pages/[pageId]/versions) already handled this correctly with isForeignKeyViolationError, returning 404. The restore endpoint was missing the same check in both the snapshotError handler and the outer catch block.

All 3 Sentry events originated from E2E test sessions where workspace teardown cascaded to page deletion mid-restore, but the same race condition could occur if a user deletes a page while another tab attempts a restore.

Changes

  • src/app/api/pages/[pageId]/versions/[versionId]/route.ts: Added isForeignKeyViolationError to imports. Added FK violation check before captureSupabaseError in the snapshot-insert error handler (returns 404). Added same check in the outer catch block.
  • src/app/api/pages/[pageId]/versions/[versionId]/route.test.ts: Added isForeignKeyViolationError to the sentry mock. Added regression test for FK violation during restore returning 404.

Verification

  • pnpm lint — 0 errors
  • pnpm typecheck — clean
  • pnpm test — 142 files, 1912 tests passed

Closes #1114

The restore endpoint (POST /api/pages/[pageId]/versions/[versionId])
was missing isForeignKeyViolationError handling when inserting a
snapshot into page_versions. If the page was deleted between auth
check and insert (race condition during E2E teardown or concurrent
user sessions), the FK constraint violation was sent to Sentry
instead of returning 404.

The sibling creation endpoint already handled this correctly.

Closes #1114

Co-authored-by: Ona <no-reply@ona.com>
@sw-factory-automations sw-factory-automations added the bug Something isn't working label May 16, 2026
@vercel
Copy link
Copy Markdown

vercel Bot commented May 16, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
memo Ready Ready Preview, Comment May 16, 2026 5:15am

Request Review

@sw-factory-automations sw-factory-automations merged commit 82045b6 into main May 16, 2026
9 checks passed
@sw-factory-automations sw-factory-automations deleted the fix/sentry-memo-2f-fk-violation-restore branch May 16, 2026 05:32
@sw-factory-automations
Copy link
Copy Markdown
Collaborator Author

✅ UI verification skipped — this PR contains no UI changes (only API route handler and tests).

@sw-factory-automations
Copy link
Copy Markdown
Collaborator Author

✅ Post-merge verification passed.

E2E suite (403 tests against memo.software-factory.dev): 376 passed, 20 failed (pre-existing), 7 did not run.

The 20 failures are pre-existing flaky tests unrelated to this PR. This PR only modified src/app/api/pages/[pageId]/versions/[versionId]/route.ts (version restore FK violation handling). None of the failing tests touch version restore:

Area Failing tests Count
Account deletion full deletion flow, sole-owner blocking 2
Database bulk select select-all, bulk delete, clear selection 3
Database column reorder draggable, drag reorder, persist after reload 3
Database CSV export export via toolbar button 1
Database row page open row, properties header, select property, editor, breadcrumb, navigate back 6
Database search search filters rows 1
Favorites add to favorites from context menu 1
Search no matches empty state 1
Trash restore page from trash 1
Workspace settings change name and verify sidebar 1

Ad-hoc smoke tests — all passed:

  • ✅ Landing page (200, has title)
  • ✅ Sign-in page (email input present)
  • /api/health (200, not down)
  • ✅ Authenticated login flow (redirects to workspace)
  • ✅ Workspace page loads after login
  • ✅ No console errors (unauthenticated or authenticated)
  • Skipped: /dashboard (not built), editor navigation (no page buttons matched ago pattern in smoke test)

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.

bug: Version restore endpoint missing FK violation handling sends noise to Sentry (Sentry MEMO-2F)

1 participant