Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I run prisma migrate on every app start?

I'm deploying a Node.js project that uses PostgreSQL with Prisma to Kubernetes. I created the Dockerfile and I'm building the docker image to Docker Hub:

FROM node:lts-slim

WORKDIR /app

# Add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
ENV NODE_ENV=production

RUN apt-get update

# Install Chromium
RUN apt-get install chromium -y

# Install yarn
RUN apt-get install yarn -y

COPY package.json /app/package.json
RUN yarn install --silent

# Add app
COPY . /app

# Generate prisma
RUN yarn run generate

# Build the app
RUN yarn build

EXPOSE 4000

# Start the app
CMD ["yarn", "run", "start"]

I want to use CI/CD, so I would need to check if the PostgreSQL is updated. This can be done with npx prisma migrate resolve --preview-feature

I thought on always running the prisma migrate to check if the DB is updated, since if a new build changes the schema, the DB should reflect it.

Since K8s pods are ephemeral, is it right to add the npx prisma migrate resolve --preview-feature to the start script, so every time the app starts, it also checks the DB? I don't think running prisma migrate all the time is good, but what would be the solution?

like image 511
Otavio Bonder Avatar asked Nov 29 '25 03:11

Otavio Bonder


1 Answers

In this case, I would suggest running prisma migrate deploy as mentioned here in your CI/CD step before deploying the application so that you do not need to perform it on every start script which is not ideal in this case.

This migrations just need to be executed once and can be added to your pre-deploy step in your pipeline.

like image 150
Ryan Avatar answered Nov 30 '25 22:11

Ryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!