Skip to content

fix: handle OgImage unique constraint conflict for shared URLs across origins#822

Open
artylobos wants to merge 1 commit into
Merit-Systems:mainfrom
artylobos:fix/ogimage-unique-constraint-287
Open

fix: handle OgImage unique constraint conflict for shared URLs across origins#822
artylobos wants to merge 1 commit into
Merit-Systems:mainfrom
artylobos:fix/ogimage-unique-constraint-287

Conversation

@artylobos
Copy link
Copy Markdown

Summary

Fixes #287

Problem

When registering a new resource, registration fails with a database unique constraint error if two different origins share the same OG image URL (e.g., via shared CDN or redirect).

Root Cause

  1. Within a single origin: Duplicate og:image tags with the same URL cause parallel upserts with the same composite key, creating a race condition.
  2. Across origins: If migration 20260115100000 hasn't been applied in production, a legacy unique index on OgImage.url still exists, causing P2002 errors when a URL is already stored under another origin.

Fix

  • Deduplicate OG images by URL within each origin before upsert (prevents race condition from duplicate tags)
  • Wrap upsert in try/catch for Prisma P2002 unique violation
  • Gracefully skip if OgImage URL already exists for another origin (no data loss - the image is already stored)

Changes

Modified apps/scan/src/services/db/resources/origin.ts:

  • Deduplicate ogImages by URL using Map before upsert
  • Wrap each ogImage upsert in try/catch
  • Handle P2002 Prisma error code for legacy url unique constraint
  • Skip silently when image URL exists under another origin

Testing

This fix handles both scenarios described in #287:

  1. Multiple origins sharing the same OG image URL
  2. Duplicate og:image tags within a single origin's scraped metadata

… origins

Fixes Merit-Systems#287

The registration crash occurs when two different origins share the same
OG image URL (e.g. via shared CDN or redirect). The root cause is twofold:

1. Within a single origin: duplicate og:image tags with the same URL
   cause parallel upserts with the same composite key, creating a race.

2. Across origins: if migration 20260115100000 hasn't been applied in
   production, a legacy unique index on OgImage.url still exists,
   causing P2002 errors when a URL is already stored under another origin.

Fix:
- Deduplicate OG images by URL within each origin (prevents race)
- Wrap upsert in try/catch for Prisma P2002 unique violation
- Gracefully skip if OgImage URL already exists for another origin
  (no data loss — the image is already stored)
@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 4, 2026

@artylobos is attempting to deploy a commit to the Merit Systems Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Unable to register resource due to OgImage unique constraint conflict

1 participant