fix: make prisma seed idempotent (#1094) - #1147
Open
Killerjunior wants to merge 3 commits into
Open
Conversation
- Replace prisma.streamEvent.create() with upsert() keyed on the @@unique([transactionHash, eventType]) compound constraint - All three models (User, Stream, StreamEvent) now use upsert(), making `npx prisma db seed` safe to re-run against a populated DB - Document the idempotency guarantee in backend/README.md
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.
Description
Re-running
npx prisma db seedagainst an already-seeded database previously crashed with a unique-constraint violation (or silently created duplicate rows) becauseprisma.streamEvent.create()was used instead ofupsert(). This PR makes the seed script fully idempotent by converting that call toupsert()keyed on the existing@@unique([transactionHash, eventType])compound constraint, and documents the guarantee in the backend README.Type of Change
Related Issues
Closes #1094
Changes Made
backend/prisma/seed.ts— Replacedprisma.streamEvent.create()withprisma.streamEvent.upsert(), keyed on the@@unique([transactionHash, eventType])compound constraint. All three seeded models now useupsert():UserpublicKeyStreamstreamIdStreamEventtransactionHash+eventType← fixedbackend/README.md— Added a "Database Seeding" section documenting thenpx prisma db seedcommand and the idempotency guarantee with the table above.Testing
Test Coverage
Test Steps
npx prisma migrate deploynpx prisma db seed— confirm it completes without errors.npx prisma db seed— confirm it completes without errors and no duplicate rows appear inUser,Stream, orStreamEvent.npx prisma studioor a direct SQL query).Breaking Changes
None. This change only affects the local development seed script. No schema changes, no API changes, no data migrations required.
Checklist
Additional Notes
The
StreamEventupsert is keyed on thetransactionHash_eventTypePrisma-generated name for the@@unique([transactionHash, eventType])compound constraint already present inschema.prisma— no schema changes are needed. Theupdate: {}body is intentional: on conflict, the existing row is left unchanged, which is the correct behaviour for immutable on-chain event records.