P
Prisma4mo ago
antoine

Recommendation for data migration with prisma and mongodb

Hello, for data migration with prisma and mongodb, do you recommend the “No breaking changes updates” method, where you put the new optional fields (but if you need to update values, it doesn't work because the value is null) or the “”All-at-once“ updates:” method, where you add the new fields with a default value. In the latter case, what is recommended for migration scripts? Are we supposed to have a migration script that will automatically create the value thanks to the script? What should this script look like? If prisma's migrate method doesn't work with mongodb, do you have to write it manually?
Would this work for exemple : // scripts/addNewFields.js import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function runMigration() { try { // Example for the Customer model: add loyaltyPoints and isVIP const result = await prisma.customer.updateMany({ data: { loyaltyPoints: 0, isVIP: false, }, }); } catch (error) { console.error('Migration error:', error); process.exit(1); } finally { await prisma.$disconnect(); } } runMigration(); /* === package.json UPDATE === "scripts": { "build": "remix vite:build", "setup": "prisma generate && prisma db push", "migrate:data": "node scripts/addNewFields.js", "docker-start": "npm run setup && npm run migrate:data && npm run start", ... }
FROM node:20-alpine

EXPOSE 3000
WORKDIR /app
COPY . .
ENV NODE_ENV=production

# Install OpenSSL for Prisma
RUN apk add --no-cache openssl

# Install dependencies
RUN npm install

# Build application
RUN npm run build

CMD ["npm", "run", "docker-start"]
FROM node:20-alpine

EXPOSE 3000
WORKDIR /app
COPY . .
ENV NODE_ENV=production

# Install OpenSSL for Prisma
RUN apk add --no-cache openssl

# Install dependencies
RUN npm install

# Build application
RUN npm run build

CMD ["npm", "run", "docker-start"]
Thanks a lot
3 Replies
Prisma AI Help
Prisma AI Help4mo ago
Ahoy, knowledge seeker! I'm the Prisma AI Help Bot. Do you want a dev's response that might take a hot second, or an AI answer that's ready before your next coffee sip? Either way, your question is important to us.
jonfanz
jonfanz4mo ago
For mongo, since there’s no true migration support, you’d need to push the new schema and then update with a script like you supplied, yes.
antoine
antoineOP4mo ago
Thank you

Did you find this page helpful?