admin管理员组文章数量:1026989
I’m working on a Nextjs application using Prisma as ORM, where the Docker container is built via GitHub Actions and deployed to Kubernetes across environments for multiple user anizations (let’s call them "clients").
My question is : How would you apply Prisma migrations to the client databases during upgrades?
Prisma's documentation suggests applying migrations through the CI/CD pipeline. However, in our case, this is not an option because:
- The client databases are neither known nor accessible at build time.
- The environments may not be upgraded simultaneously, making build-time migration inappropriate.
With other ORMs like Python/Alembic, I’ve handled this by integrating migrations into Kubernetes deployment args to overwrite the Docker entrypoint (which is fine as long as several containers don't try to start concurrently).
apiVersion: apps/v1
kind: Deployment
spec:
containers:
- name: container-name
args:
- bash
- -c
- alembic upgrade heads && uvicorn app.main:app ...
However, in the case of Prisma, with a multi-stage build that only embeds the Prisma client (discarding build-time dependencies), this approach isn’t viable. Embedding the entire Prisma library would result in a bloated container image. Should migrations be deployed separately via a dedicated Job? Or do you recommend other approaches?
I’m working on a Nextjs application using Prisma as ORM, where the Docker container is built via GitHub Actions and deployed to Kubernetes across environments for multiple user anizations (let’s call them "clients").
My question is : How would you apply Prisma migrations to the client databases during upgrades?
Prisma's documentation suggests applying migrations through the CI/CD pipeline. However, in our case, this is not an option because:
- The client databases are neither known nor accessible at build time.
- The environments may not be upgraded simultaneously, making build-time migration inappropriate.
With other ORMs like Python/Alembic, I’ve handled this by integrating migrations into Kubernetes deployment args to overwrite the Docker entrypoint (which is fine as long as several containers don't try to start concurrently).
apiVersion: apps/v1
kind: Deployment
spec:
containers:
- name: container-name
args:
- bash
- -c
- alembic upgrade heads && uvicorn app.main:app ...
However, in the case of Prisma, with a multi-stage build that only embeds the Prisma client (discarding build-time dependencies), this approach isn’t viable. Embedding the entire Prisma library would result in a bloated container image. Should migrations be deployed separately via a dedicated Job? Or do you recommend other approaches?
Share Improve this question edited Nov 16, 2024 at 12:17 David Maze 161k46 gold badges250 silver badges291 bronze badges asked Nov 16, 2024 at 6:34 JdproJdpro 731 silver badge4 bronze badges1 Answer
Reset to default 0This is how I did it in one of my projects:
- Inside the multi-staged
Dockerfile
, ensure theprisma
folder isCOPY
-ed over into the final stage:
# Assuming /builder/ is WORKDIR of the builder stage
COPY --from=builder /builder/prisma ./prisma
- Modify the
package.json
file to call prisma migration command inside thestart
command:
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "npx prisma migrate deploy && next start",
"lint": "next lint"
},
- Make the
CMD
command inDockerfile
callnpm start
:
CMD ["npm", "start"]
If you prefer to use an init container to perform the migration, just modify step 2 to bind something like npm migrate
script to the npx prisma migrate deploy
command and override the args of that init container to call npm migrate
instead of npm start
:
"scripts": {
"dev": "next dev",
"build": "next build",
"migrate": "npx prisma migrate deploy",
"start": "next start",
"lint": "next lint"
},
I’m working on a Nextjs application using Prisma as ORM, where the Docker container is built via GitHub Actions and deployed to Kubernetes across environments for multiple user anizations (let’s call them "clients").
My question is : How would you apply Prisma migrations to the client databases during upgrades?
Prisma's documentation suggests applying migrations through the CI/CD pipeline. However, in our case, this is not an option because:
- The client databases are neither known nor accessible at build time.
- The environments may not be upgraded simultaneously, making build-time migration inappropriate.
With other ORMs like Python/Alembic, I’ve handled this by integrating migrations into Kubernetes deployment args to overwrite the Docker entrypoint (which is fine as long as several containers don't try to start concurrently).
apiVersion: apps/v1
kind: Deployment
spec:
containers:
- name: container-name
args:
- bash
- -c
- alembic upgrade heads && uvicorn app.main:app ...
However, in the case of Prisma, with a multi-stage build that only embeds the Prisma client (discarding build-time dependencies), this approach isn’t viable. Embedding the entire Prisma library would result in a bloated container image. Should migrations be deployed separately via a dedicated Job? Or do you recommend other approaches?
I’m working on a Nextjs application using Prisma as ORM, where the Docker container is built via GitHub Actions and deployed to Kubernetes across environments for multiple user anizations (let’s call them "clients").
My question is : How would you apply Prisma migrations to the client databases during upgrades?
Prisma's documentation suggests applying migrations through the CI/CD pipeline. However, in our case, this is not an option because:
- The client databases are neither known nor accessible at build time.
- The environments may not be upgraded simultaneously, making build-time migration inappropriate.
With other ORMs like Python/Alembic, I’ve handled this by integrating migrations into Kubernetes deployment args to overwrite the Docker entrypoint (which is fine as long as several containers don't try to start concurrently).
apiVersion: apps/v1
kind: Deployment
spec:
containers:
- name: container-name
args:
- bash
- -c
- alembic upgrade heads && uvicorn app.main:app ...
However, in the case of Prisma, with a multi-stage build that only embeds the Prisma client (discarding build-time dependencies), this approach isn’t viable. Embedding the entire Prisma library would result in a bloated container image. Should migrations be deployed separately via a dedicated Job? Or do you recommend other approaches?
Share Improve this question edited Nov 16, 2024 at 12:17 David Maze 161k46 gold badges250 silver badges291 bronze badges asked Nov 16, 2024 at 6:34 JdproJdpro 731 silver badge4 bronze badges1 Answer
Reset to default 0This is how I did it in one of my projects:
- Inside the multi-staged
Dockerfile
, ensure theprisma
folder isCOPY
-ed over into the final stage:
# Assuming /builder/ is WORKDIR of the builder stage
COPY --from=builder /builder/prisma ./prisma
- Modify the
package.json
file to call prisma migration command inside thestart
command:
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "npx prisma migrate deploy && next start",
"lint": "next lint"
},
- Make the
CMD
command inDockerfile
callnpm start
:
CMD ["npm", "start"]
If you prefer to use an init container to perform the migration, just modify step 2 to bind something like npm migrate
script to the npx prisma migrate deploy
command and override the args of that init container to call npm migrate
instead of npm start
:
"scripts": {
"dev": "next dev",
"build": "next build",
"migrate": "npx prisma migrate deploy",
"start": "next start",
"lint": "next lint"
},
本文标签: Apply Prisma Migrations to Multiple Environments with KubernetesStack Overflow
版权声明:本文标题:Apply Prisma Migrations to Multiple Environments with Kubernetes - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745664157a2162076.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论