使用 Prisma Migrate 部署数据库更改
要将待处理的迁移应用到暂存、测试或生产环境,请在 CI/CD 管道中运行 migrate deploy
命令:
¥To apply pending migrations to staging, testing, or production environments, run the migrate deploy
command as part of your CI/CD pipeline:
npx prisma migrate deploy
确切何时运行 prisma migrate deploy
取决于你的平台。例如,简化的 Heroku 工作流程包括:
¥Exactly when to run prisma migrate deploy
depends on your platform. For example, a simplified Heroku workflow includes:
-
确保
./prisma/migration
文件夹位于源代码管理中¥Ensuring the
./prisma/migration
folder is in source control -
在 发布阶段 期间运行
prisma migrate deploy
¥Running
prisma migrate deploy
during the release phase
理想情况下,migrate deploy
应该是自动化 CI/CD 管道的一部分,我们通常不建议在本地运行此命令来部署对生产数据库的更改(例如,通过临时更改 DATABASE_URL
环境变量)。通常认为在本地存储生产数据库 URL 并不是一个好的做法。
¥Ideally, migrate deploy
should be part of an automated CI/CD pipeline, and we do not generally recommend running this command locally to deploy changes to a production database (for example, by temporarily changing the DATABASE_URL
environment variable). It is not generally considered good practice to store the production database URL locally.
请注意,为了运行 prisma migrate deploy
命令,你需要访问通常添加到 devDependencies
的 prisma
依赖。某些平台(例如 Vercel)会在构建过程中删除开发依赖,从而阻止你调用该命令。可以通过将 prisma
设为生产依赖,将其移至 package.json
中的 dependencies
来解决此问题。有关 migrate deploy
命令的更多信息,请参阅:
¥Beware that in order to run the prisma migrate deploy
command, you need access to the prisma
dependency that is typically added to the devDependencies
. Some platforms like Vercel, prune development dependencies during the build, thereby preventing you from calling the command. This can be worked around by making the prisma
a production dependency, by moving it to dependencies
in your package.json
.
For more information about the migrate deploy
command, see:
使用 GitHub Actions 部署数据库更改
¥Deploying database changes using GitHub Actions
作为 CI/CD 的一部分,你可以将 prisma migrate deploy
作为管道的一部分运行,以将待处理的迁移应用到生产数据库。
¥As part of your CI/CD, you can run prisma migrate deploy
as part of your pipeline to apply pending migrations to your production database.
以下是一个示例操作,它将针对你的数据库运行迁移:
¥Here is an example action that will run your migrations against your database:
name: Deploy
on:
push:
paths:
- prisma/migrations/**
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Setup Node
uses: actions/setup-node@v3
- name: Install dependencies
run: npm install
- name: Apply all pending migrations to the database
run: npx prisma migrate deploy
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
高亮的行表明此操作仅在 prisma/migrations
目录发生更改时才会运行,因此 npx prisma migrate deploy
仅在迁移更新时才会运行。
¥The highlighted line shows that this action will only run if there is a change in the prisma/migrations
directory, so npx prisma migrate deploy
will only run when migrations are updated.
确保你有 DATABASE_URL
变量 在你的存储库中设置为秘密,连接字符串周围没有引号。
¥Ensure you have the DATABASE_URL
variable set as a secret in your repository, without quotes around the connection string.