Next.js Discord

Discord Forum

Where to run migrations with DrizzleORM and NextJS 14

Answered
Leafcutting bee posted this in #help-forum
Open in Discord
Leafcutting beeOP
I am trying to follow [this](https://orm.drizzle.team/docs/migrations) documentation but I am unsure where to call the migrate function. The documentation uses a script run manually.

I am deploying to Vercel from a github repo and using Vercel postgres. Ideally I would just like to run the migrations when Vercel deploys a new version of my app.

Thanks
Answered by "use php"
If you want to push on every deployment, You can
npm run migration:push && npm run build
View full answer

30 Replies

@Leafcutting bee I am trying to follow [this](https://orm.drizzle.team/docs/migrations) documentation but I am unsure where to call the migrate function. The documentation uses a script run manually. I am deploying to Vercel from a github repo and using Vercel postgres. Ideally I would just like to run the migrations when Vercel deploys a new version of my app. Thanks
you can add those two scripts to your package.json:
"migration:generate": "drizzle-kit generate",
"migration:push": "drizzle-kit push"

After you done that, the generate will generate the migrations and the push will push the migrations to your database.
Leafcutting beeOP
currently I have the migrations.js script using DrizzleORM and this is my package.json:
"build": "tsx src/db/migrate.ts && next build",
    "start": "next start",
    "lint": "next lint",
    "lint-commit": "devmoji -e --lint",
    "prepare": "husky",
    "generate:migrations": "drizzle-kit generate"
If you want to push on every deployment, You can
npm run migration:push && npm run build
Answer
@"use php" If you want to push on every deployment, You can npm run migration:push && npm run build
Leafcutting beeOP
Ok cool, i'll change it to do that
I was close 😄 I just added it to my npm build command
but your way seems better
im pretty sure migrations are supposed to be done manually. not on every commit.
migrations are only done when there is a need to do so (e.g. when db schema changes)
@joulev im pretty sure migrations are supposed to be done manually. not on every commit.
Leafcutting beeOP
Yeah but it won't run anything if all migrations are up to date
So only pull requests that actually change the schema will have an effect
@"use php" no, prs shouldn't change the schema
Leafcutting beeOP
The schema is defined in the code
And drizzle handles the migrations
If you are using a separate database for preview, then its all good
Leafcutting beeOP
So how would you integrate schema changes into a CI/CD workflow?
Yes, I have a separate db for each env
but still, there's another issue. If there'll be more then a few prs at a time, its also a issue
@"use php" but still, there's another issue. If there'll be more then a few prs at a time, its also a issue
Leafcutting beeOP
Yes but I will add the generate migrations to an action on github
So devs aren't manually generating the migrations
they should only be generated when a feature branch is merged into stage
In My opinion, I don't think its a good idea to run migration on prs.
Leafcutting beeOP
thats my vision anyway
@Leafcutting bee they should only be generated when a feature branch is merged into stage
Yes, it should be pushed when there is a merge
Leafcutting beeOP
Yep
Ok, I'll see what I can do
I just want seamless CI/CD
@"use php" If you want to push on every deployment, You can npm run migration:push && npm run build
You can mark the mark this message as a solution.