What's the best way to deploy migrations on production database?
I'm confused as to what is the best way to deploy migration on the production database.
Locally when I test on my local database instance, I use the commands manually to generate and push the migrations
But I want to automate this.
Say, I'm using Vercel and I want to push the migration on every commit.
In prisma to deploy the generated migrations I use the command
prisma migrate deploy
, I add this command to the build command like this: prisma generate && prisma migrate deploy && next build
There are my scripts in package.json
14 Replies
Sorry if this is not what you needed, but here is how I handle migration on live production
I create a docker instance and install the bare necessary dependencies just to run the migration script
Here is the Dockerfile
Here is the script section in my package.json
migration.ts is just a ts file where I define my migrate function.
Here is my docker compose file.
This way, every time you run the docker compose, you start the db, the app and THEN the migration will run, the container will automatically shut down when the migration succeeds (or fails). Since migrations are safe to run when they have already been run, we can start migrations every time we start the app.
@sik did you find a way? i have the same question -> https://stackoverflow.com/q/77877947/6141587
@nqhtrung i dont wanna use dockerfile if it can be simplified. i do think its just running 1 script that can be done with
npm run prestart
... lmk if you have other ways... im thinking of prestart
(see my script on stackoverflow)Stack Overflow
How to run generate & migrate in production on a VPS with SQLite da...
So I have a database in development that I run db:generate & db:migrate on to create the database & then run my app that uses that database.
And it works fine.
But how do I do that in produ...
here is my migration script
This file lives at the root of the project and the migration folder 'drizzle' is also on the same level
i use this on railway and on eb by just appending it to the build step as you have done, works so far for me
i use https://www.jacobparis.com/content/remix-drizzle which is working really well. its in the schema folder itself so anytime you start the app, it runs. i'm gonna test it again to confirm & update my answer bcz i think i've found a simple solution.
I did it like this, simple way and works
In my package json build command, I am using my custom build script
which looks like this
and here is migrate.js file
and thats it, I hope that helps
now i like that. i think i can use bun shell from https://bun.sh instead of
.sh
to write some javascript -> https://bun.sh/blog/the-bun-shell & https://bun.sh/docs/runtime/shellsure, if u will do it with bun you can paste the solution here, I do not have time for this right now haha
so i learned docker & thought it would be easy but migrations was hard. took me a week on this thing due to too many issues working in parallel. however, i finally got a solution which you can use if you have this combination (drizzle + next.js + sqlite + docker) -> https://stackoverflow.com/a/78034626/6141587
Stack Overflow
Cannot find package 'drizzle-orm' in Docker Container even though I...
I install all my dependencies in Dockerfile using npm ci below.
Dockerfile
FROM node:20-alpine AS base
1. Install dependencies only when needed
FROM base AS deps
Check https://github.com/nodejs/