Skip to content

fix: handle OgImage unique constraint conflict for shared URLs (#287)#767

Open
ledgerpilot wants to merge 1 commit intoMerit-Systems:mainfrom
ledgerpilot:ledgerpilot/fix-287
Open

fix: handle OgImage unique constraint conflict for shared URLs (#287)#767
ledgerpilot wants to merge 1 commit intoMerit-Systems:mainfrom
ledgerpilot:ledgerpilot/fix-287

Conversation

@ledgerpilot
Copy link
Copy Markdown

Closes #287

Root Cause

When two resources share the same origin (e.g. https://subdomain.example.com), both registrations call upsertOrigin which tries to create OgImage records for that origin's scraped metadata. If the OG image URL is the same across both calls (e.g. a CDN image used site-wide), the concurrent upsert inside Promise.all hits the unique constraint on (originId, url) — or a pre-existing DB-level constraint on url — and throws.

Fix

Replace the Promise.all of parallel ogImage.upsert calls with a sequential for…of loop where each upsert is wrapped in a .catch(). This ensures:

  1. Duplicate OG image URLs for the same origin are updated (the happy path).
  2. A URL that already exists under a different origin (e.g. CDN image shared across sites) is silently skipped rather than crashing the whole transaction.
  3. The resourceOrigin upsert always succeeds regardless of OG image conflicts.

Changes

  • apps/scan/src/services/db/resources/origin.ts: sequential upserts with per-record error handling instead of Promise.all

@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Apr 9, 2026

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

A member of the Team first needs to authorize it.

// OG image URLs shared across multiple origins do not cause a
// unique-constraint failure on the whole transaction.
for (const { url, height, width, title, description } of origin.ogImages) {
await tx.ogImage
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .catch() block silently swallows ALL errors from OG image upsert, including critical database errors like connection loss or permission denied, preventing error visibility and violating observability principles.

Fix on Vercel

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