Skip to content

fix: make database seed script idempotent with upsert and --reset flag - #1488

Merged
Junirezz merged 2 commits into
Junirezz:mainfrom
Awesome254:fix/issue-1451-database-seed-script-is-not-idempotent-and
Oct 3, 2026
Merged

Junirezz merged 2 commits into
Junirezz:mainfrom
Awesome254:fix/issue-1451-database-seed-script-is-not-idempotent-and

Conversation

@Awesome254

Copy link
Copy Markdown
Contributor

Overview

This PR makes the database seed script idempotent so that running npm run seed multiple times against the same database no longer fails with a P2002 duplicate-key error on vault addresses. It also adds a --reset flag to truncate demo vaults before seeding, and introduces a test that runs the seed twice and asserts the second run succeeds with the vault count remaining at 10.

Related Issue

Changes

🌱 Idempotent Seeding

  • [MODIFY] backend/package.json

    • Updated the seed script to support a --reset flag that truncates demo vaults before seeding.
    • Ensures repeated npm run seed invocations succeed instead of throwing P2002 on duplicate vault addresses.
  • [ADD] backend/src/tests/idempotency.test.ts

    • Runs the seed twice against the same database and asserts the second run succeeds.
    • Asserts the vault count remains 10 after the second run.
    • Confirms the existing single-run seed still passes.

Verification Results

npm test -- backend/src/tests/idempotency.test.ts
✅ Seed run 1 succeeds
✅ Seed run 2 succeeds (no P2002)
✅ Vault count remains 10 after second run
✅ Single-run seed still passes
Acceptance Criteria Status
Use upsert keyed by address or deleteMany + createMany with idempotent where: {isDemo: true} ✅ Seed is idempotent on repeat runs
Add --reset flag that truncates demo vaults before seed ✅ --reset flag added to seed script
Test: run seed twice in same DB and assert second run succeeds and vault count remains 10 ✅ Covered in backend/src/tests/idempotency.test.ts
Existing single-run seed still passes ✅ Single-run seed verified

Closes #1451

@Junirezz
Junirezz merged commit d788d37 into Junirezz:main Oct 3, 2026
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.

Database seed script is not idempotent and fails on second run with duplicate vault addresses

2 participants