Skip to content

7.27 Prisma Migration & Database Sync#330

Merged
Om7035 merged 43 commits intoQuoteVote:mainfrom
shubham-01-star:7.27-prisma-migration-sync
Apr 23, 2026
Merged

7.27 Prisma Migration & Database Sync#330
Om7035 merged 43 commits intoQuoteVote:mainfrom
shubham-01-star:7.27-prisma-migration-sync

Conversation

@shubham-01-star
Copy link
Copy Markdown
Collaborator

Summary

  • Aligns Prisma schema with the live MongoDB instance via db push (Prisma's migrate dev is SQL-only and does not support MongoDB)
  • Verifies Prisma Client CRUD + relationship traversal across all 24 models
  • Adds a Mongoose ↔ Prisma parity script (field-mapping + functional populate() vs include() check)
  • Documents the MongoDB migration strategy, including the replica-set requirement

Acceptance Criteria

AC Status Notes
prisma migrate dev --name init completes ⚠️ Intentional deviation Prisma migrate dev is SQL-only and errors out on MongoDB. Correct equivalent pnpm prisma:push is wired. See docs/prisma-migration-strategy.md.
Prisma CRUD across all models pnpm prisma:test covers all 24 Prisma models
Prisma data matches Mongoose shape pnpm prisma:parity verifies both field mappings and functional query parity
Integrity checks (FKs + indexes) Unique-constraint tests for Roster, Typing, BotReport, UserReputation

What changed

New files

  • docs/prisma-migration-strategy.md — why migrate dev doesn't work for MongoDB, the db push workflow, backfill patterns, replica-set requirements, and when to re-sync
  • quotevote-backend/scripts/check-replica-set.ts — preflight guard; detects non-replica-set MongoDB and fails fast with an actionable hint instead of cryptic P2031
  • quotevote-backend/scripts/prisma-mongoose-parity.ts — bidirectional round-trips + functional populate() vs include() comparison

Modified

  • quotevote-backend/package.jsonprisma:studio (previously ran db push) → prisma:push; added real prisma:studio, prisma:sync, prisma:parity; test:prisma now gated by the replica-set preflight
  • quotevote-backend/scripts/prisma-crud-test.ts — rewritten from 4-model smoke test to full 24-model CRUD + deep traversal
  • quotevote-backend/__tests__/integration/prisma-dependent-models.test.ts — added 10 missing model suites (VoteLog, DirectMessage, Notification, UserInvite, BotReport, UserReputation, Domain, Creator, Content, Collection), 3-level deep traversal, 24-model delegate smoke test, and a beforeAll replica-set guard
  • quotevote-backend/scripts/prisma-health-check.ts — fix stale PostMessage reference (renamed to Message in 7.26)

24 models covered

User, Group, Post, Comment, Quote, Vote, VoteLog, Reaction, Message, DirectMessage, MessageRoom, Notification, Activity, Roster, Presence, Typing, UserInvite, UserReport, BotReport, UserReputation, Domain, Creator, Content, Collection

Deep traversal coverage

  • 2-level: user → posts → comments/votes/quotes; room → messages → reactions
  • 3-level: user → posts → comments → commenter; room → messages → reactions → reactor

Type safety

  • Zero any across all new/modified files
  • Zero eslint-disable comments
  • All delegate maps are strongly typed (PrismaDelegate, PrismaModelKey, MongooseModel, MongooseModelKey)

Test plan

CI-safe (no live DB required)

  • pnpm exec prisma validate — schemas valid
  • pnpm type-check — clean
  • pnpm lint — clean
  • pnpm build — clean
  • pnpm test — 700/700 passing

Live-DB (requires MongoDB replica set)

  • pnpm prisma:push — apply schema to local/staging MongoDB
  • pnpm prisma:health — confirms connectivity + 24-model count
  • pnpm prisma:test — full CRUD + traversal across all 24 models
  • pnpm test:prisma — Jest integration suite (preflight-guarded)
  • pnpm prisma:parity — Mongoose ↔ Prisma alignment, field mappings, functional populate-vs-include parity

If the reviewer's MongoDB is not already a replica set, any of the DB-dependent commands will print an actionable hint with Docker / local mongod / Atlas fixes. See docs/prisma-migration-strategy.md.

shubham-01-star and others added 30 commits March 21, 2026 11:25
Add shared test helpers (_helpers.ts) for Mongoose schema validation
testing and update jest.config.ts modulePathIgnorePatterns for proper
test isolation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add comprehensive schema tests covering required fields, default values,
enum validation, and field types for the core domain models.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Cover vote type enum validation, soft-delete defaults, required fields,
and VoteLog token/description constraints.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Cover message text requirement, soft-delete defaults, readBy/deliveredTo
subdocs, room messageType enum, and isDirect defaults.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…nce, Typing

Cover emoji requirement, roster status enum (pending/accepted/declined/blocked),
presence status enum, and typing TTL expiry behavior.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ontent, Creator, Domain

Cover activity event type enum, collection name requirement, content title
requirement, creator fields, and domain key requirement.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…, UserInvite, UserReport, UserReputation, Group

Cover notification type enum, report reason/status/severity enums,
invite expiry, reputation score defaults, and group privacy enum.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Cover SolidConnection CRUD operations and schema validation including
webId/issuer requirements, tokens object, and connection status.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove unnecessary mock function declarations to align with the
standardized Mongoose mock pattern used across all model tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add createdAt timestamp, simplify Quote relation from dual
(quoter/quoted) to single userId to match current Mongoose model.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Quote: replace quoter/quoted with single userId to match Mongoose
- Post, Comment, Vote: add createdAt timestamps for Mongoose parity
- Reaction: add createdAt/updatedAt and compound userId+messageId index

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
shubham-01-star and others added 11 commits March 31, 2026 08:57
- PostMessage: add createdAt, compound index on messageRoomId+created
- MessageRoom: add createdAt for Mongoose timestamps parity

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…yping

- Presence: add createdAt/updatedAt, lastHeartbeat index, defaults
- Typing: add createdAt/updatedAt, expiresAt field, isTyping default true

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add integration test suite covering 13 model types with 28 tests:
- User, Post, Comment, Quote, Vote CRUD and relations
- MessageRoom, PostMessage, Reaction relations
- Presence, Roster, Typing with unique constraint checks
- UserReport bidirectional relations
- Deep nested traversal (user -> posts -> comments/votes/quotes)
- Deep messaging traversal (room -> messages -> reactions)
- Activity model relations

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The Message model was renamed from PostMessage in 7.26, but the hardcoded
model list in the health-check script still referenced the old name. Update
it so the output matches the actual generated Prisma Client.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Prisma's MongoDB connector uses transactions internally for create/update/delete
operations, which requires MongoDB to run as a replica set. A standalone mongod
fails with a cryptic P2031 error mid-run.

This preflight script probes the database with a tiny user.create/delete round
trip and either exits 0 (replica set detected) or exits 1 with a boxed, actionable
hint covering Docker, local mongod, and Atlas fixes — so integration tests never
surface raw P2031 from the middle of a suite.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…st:prisma

- Rename misleading prisma:studio (which actually ran db push) to prisma:push
- Add real prisma:studio for the GUI
- Add prisma:sync (push + generate) as a one-shot convenience
- Add prisma:parity wired to scripts/prisma-mongoose-parity.ts
- Gate pnpm test:prisma behind scripts/check-replica-set.ts so the suite
  fails fast with a readable message instead of mid-suite P2031 if the
  local MongoDB is not running as a replica set

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…aversal

Replaces the previous 4-model smoke test with a full CRUD + relationship
exercise across every Prisma model:

User, Group, Post, Comment, Quote, Vote, VoteLog, Reaction, Message,
DirectMessage, MessageRoom, Notification, Activity, Roster, Presence,
Typing, UserInvite, UserReport, BotReport, UserReputation, Domain,
Creator, Content, Collection (24 total).

Includes 2-level deep traversal (user → posts → comments/votes/quotes;
room → messages → reactions; user → activities/notifications/collections)
and a 24-model COUNT smoke test to verify every delegate is callable.

LIFO cleanup wrapped in try/finally so partial failures don't leave
orphan test data. Replica-set errors are detected and produce the same
friendly hint as the preflight script.

All delegate maps are fully typed — no `any`, no eslint-disable.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… check

Because Mongoose (primary ORM) and Prisma (migration target) read/write the
same MongoDB collections, the risk is silent drift between @Map() directives
and Mongoose field names. This CLI script detects drift at runtime.

For each critical model (User, Post, MessageRoom), runs two bidirectional
round-trips:
  - Mongoose -> Prisma: create via Mongoose, read via Prisma, assert mapped
    fields (admin/isAdmin, _followingId/followingIds, _followersId/followerIds,
    _wallet/wallet, enable_voting/enableVoting, users/userIds) surface correctly
  - Prisma -> Mongoose: create via Prisma, read raw via Mongoose, assert the
    MongoDB document uses the expected legacy field names

Also adds a functional parity check comparing the same logical query shape
via Mongoose's populate() against Prisma's include() — asserts post title,
populated author id, author username, and countDocuments match prisma.count.

LIFO cleanup wrapped in finally; replica-set errors produce the friendly
preflight hint. Fully typed — no `any`, no eslint-disable.

Exits 0 on full alignment, 1 with a diff report on any mismatch.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…rsal

Expands the Prisma integration suite so every model is tested:

New suites: VoteLog, DirectMessage, Notification, UserInvite, BotReport,
UserReputation, Domain, Creator, Content, Collection — each with CRUD
plus relation reads and, where the schema has one, a unique-constraint
assertion (BotReport reporterId+userId, UserReputation userId).

New 3-level deep traversal tests:
  - user -> posts -> comments -> commenter (back to User)
  - user -> rooms -> messages -> reactions -> reactor

New smoke test: count() on every one of the 24 models in a single
Promise.all, asserting the entire delegate surface is callable.

Added a beforeAll replica-set probe that throws a readable error instead
of letting Prisma's cryptic P2031 leak out of the suite when MongoDB is
not running as a replica set.

Expanded the typed deleteFns map to cover all newly tested models for
LIFO cleanup.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…uirement

Ticket 7.27 asks to run `prisma migrate dev --name init`, but that command
is SQL-only and hard-errors on MongoDB. The correct workflow is db push.

This doc captures:
  - Why migrate dev does not work for MongoDB
  - The db push / db generate / db sync workflow
  - The @Map()-based bridging between Mongoose and Prisma field names
  - A backfill script pattern (placed under scripts/migrations/) for any
    future field renames or required-field additions
  - How pnpm prisma:parity verifies Mongoose <-> Prisma alignment (both
    field-mapping and functional populate-vs-include parity)
  - A table of integrity check commands (validate / health / test /
    test:prisma / parity) and what each covers
  - The MongoDB replica-set requirement (Prisma uses transactions for
    write operations) with three ways to satisfy it: Docker one-liner,
    local mongod config, MongoDB Atlas
  - When to re-run prisma:sync

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 19, 2026

@shubham-01-star is attempting to deploy a commit to the Louis Girifalco's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Om7035 Om7035 merged commit d45179e into QuoteVote:main Apr 23, 2026
2 of 3 checks passed
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.

2 participants