Fix OG image unique constraint bug #410
Open
+63
−41
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.
Fix OgImage unique-URL constraint breaking resource registration (Issue #287)
Registering a second resource whose origin shares an OG image URL with an existing origin currently fails with:
Root cause:
OgImage.urlis globally unique, but multiple origins can legitimately share the same OG image URL (e.g. shared CDN, redirects). The originalupsertOriginalso keyed OgImages only byurl, which conflicts with that global uniqueness.This PR makes OgImages unique per origin, not globally, and refactors origin upsert logic to be safe and idempotent.
Changes
DB schema
OgImage.urlis no longer globally unique:schema.prisma, changed:url: String @unique->url: String@@unique([originId, url]), so each origin can still only have one OgImage per URL.OgImage_url_keyunique index in the database.Origin upsert logic
Refactored
upsertOriginto use an explicit transaction and composite key:ResourceOriginbyoriginand returns a stableoriginId.OgImagerows for that origin whose URL is no longer present in the current scrape:originId_url:ResourceOriginincluding itsogImages.This allows the same OG image URL to be associated with multiple origins, while keeping OgImages idempotent and clean per origin.
Behavior
ResourceOriginrows can now safely reference the same OG image URL.No external API changes; the
upsertOriginsignature and return type remain the same.Tests / Verification
OgImage_url_keyunique index is dropped while the compositeoriginId_urlconstraint remains.https://subdomain.example.comthat scrapes OG imagehttps://cdn.example.com/shared.png.https://example.comthat scrapes the same OG image URL.url = https://cdn.example.com/shared.png.Ready for review.