fix(api): handle FK violation in version restore endpoint#1115
Merged
sw-factory-automations merged 1 commit intoMay 16, 2026
Conversation
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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Collaborator
Author
|
✅ UI verification skipped — this PR contains no UI changes (only API route handler and tests). |
Collaborator
Author
|
✅ Post-merge verification passed. E2E suite (403 tests against The 20 failures are pre-existing flaky tests unrelated to this PR. This PR only modified
Ad-hoc smoke tests — all passed:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The version restore endpoint (
POST /api/pages/[pageId]/versions/[versionId]) was missingisForeignKeyViolationErrorhandling when inserting a pre-restore snapshot intopage_versions. When the referenced page was deleted between the auth check and the insert (race condition), PostgreSQL rejected the insert with a23503FK constraint violation, which was sent to Sentry viacaptureSupabaseErrorinstead 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 withisForeignKeyViolationError, returning 404. The restore endpoint was missing the same check in both thesnapshotErrorhandler and the outercatchblock.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: AddedisForeignKeyViolationErrorto imports. Added FK violation check beforecaptureSupabaseErrorin 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: AddedisForeignKeyViolationErrorto the sentry mock. Added regression test for FK violation during restore returning 404.Verification
pnpm lint— 0 errorspnpm typecheck— cleanpnpm test— 142 files, 1912 tests passedCloses #1114