Skip to content

[Adapters] Stop sku_owners/customer_emails declaring a unique index that leaks onto sibling collections - #297

Merged
vedanshujain merged 1 commit into
mainfrom
fix/sku-owners-index-scope
Sep 22, 2026
Merged

vedanshujain merged 1 commit into
mainfrom
fix/sku-owners-index-scope

Conversation

@vedanshujain

Copy link
Copy Markdown
Contributor

Summary

  • Add-to-cart failed on the second request for any given SKU with a raw D1 UNIQUE constraint failed error, surfacing to shoppers as a generic "Something went wrong."
  • Root cause: sku_owners (and identically customer_emails) declared their natural key as a uniqueIndexes entry, on the belief that "no physical index exists in any tier." On the currently pinned host release that's no longer true — a declared unique index materializes as one physical SQLite index per plugin, keyed (plugin_id, collection, field), with no per-collection scoping. reservation_keys and reservation_index (inventory) also carry a top-level sku field, one row per reserve attempt, many rows legitimately sharing a sku — so those collections collided on sku_owners' declared index the moment a second reserve happened for any SKU: the first add-to-cart for a product worked, every subsequent one threw.
  • Fix: both collections now declare their natural key as a plain (non-unique) indexes entry instead. The create-if-absent compare-and-set against the sku/email-as-document-id was always the real enforcement; the unique index was redundant for its own collection and unsafe for its neighbors. Corrected the stale comments claiming no physical index exists.

Test plan

  • pnpm lint — clean
  • pnpm typecheck — clean
  • pnpm vitest run packages/store-emdash — 744 passed, 0 failed (817 skipped, PG-dialect-gated)
  • pnpm vitest run packages/plugin/test/commerce-storage.test.ts packages/plugin/test/sandbox — 21 passed, 0 failed
  • Root-caused and reproduced locally against the exact production error text before this fix (two independent standalone repros against the host's index-generation code)
  • Post-merge: redeploy and verify add-to-cart end-to-end on the live site

🤖 Generated with Claude Code

https://claude.ai/code/session_01HPywdmMJ9B4V1ME6GrxuDu

…hat leaks onto sibling collections

The host materializes a declared uniqueIndexes entry as one physical index
per plugin, scoped to (plugin_id, collection, field) with no per-collection
WHERE clause. reservation_keys and reservation_index also carry a top-level
sku field (one row per reserve attempt, many rows legitimately sharing a
sku), so they collided on sku_owners' declared unique index the moment a
second reserve happened for any SKU: the first add-to-cart for a product
worked, every later one threw a raw D1 UNIQUE constraint error.

Both collections' own create-if-absent CAS against the sku/email-as-id was
always the real enforcement (the module comments already said so, wrongly
adding "no physical index exists in any tier" as the reason it was safe to
declare unique anyway). Switched both to a plain (non-unique) indexes entry
and corrected the comments.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HPywdmMJ9B4V1ME6GrxuDu
@vedanshujain
vedanshujain merged commit cc8f70b into main Sep 22, 2026
4 checks passed
@vedanshujain
vedanshujain deleted the fix/sku-owners-index-scope branch September 22, 2026 11:11
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.

1 participant