Skip to main content

从本地环境部署迁移

在两种情况下,你可能会考虑将迁移直接从本地环境部署到生产环境。

¥There are two scenarios where you might consider deploying migrations directly from a local environment to a production environment.

  • 你有本地 CI/CD 管道

    ¥You have a local CI/CD pipeline

  • 你是 baselining 生产环境

    ¥You are baselining a production environment

本页概述了一些示例,说明如何执行此操作以及我们通常不推荐它的原因。

¥This page outlines some examples of how you can do that and why we would generally not recommend it.

本地 CI/CD 管道

¥Local CI/CD pipeline

如果你没有自动化的 CI/CD 流程,你可以通过以下方式从技术上将新的迁移从本地环境部署到生产环境:

¥If you do not have an automated CI/CD process, you can technically deploy new migrations from your local environment to production in the following ways:

  1. 确保你的迁移历史记录是最新的。你可以通过运行 prisma migrate dev 来完成此操作,这将根据所做的最新更改生成迁移历史记录。

    ¥Make sure your migration history is up to date. You can do this through running prisma migrate dev, which will generate a migration history from the latest changes made.

  2. 将本地连接 URL 替换为生产连接 URL

    ¥Swap your local connection URL for your production connection URL

.env
//delete-next-line
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/my_local_database"

//add-next-line
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/my_production_database"
  1. 运行 prisma migrate deploy

    ¥Run prisma migrate deploy

⛔ X1

由于以下原因,我们强烈反对这种解决方案

¥We strongly discourage this solution due to the following reasons

  • 你可能面临将生产数据库连接 URL 暴露给版本控制的风险。

    ¥You risk exposing your production database connection URL to version control.

  • 你可能会意外地使用生产连接 URL,进而覆盖或删除你的生产数据库。

    ¥You may accidentally use your production connection URL instead and in turn override or delete your production database.

We recommend setting up an automated CI/CD pipeline

管道应处理到临时和生产环境的部署,并在管道步骤中使用 migrate deploy。请参阅 部署指南 的示例。

¥The pipeline should handle deployment to staging and production environments, and use migrate deploy in a pipeline step. See the deployment guides for examples.

建立生产数据库的基线

¥Baselining a production database

当你将 Prisma Migrate 添加到现有数据库时,你必须 baseline 生产数据库。基线化执行一次,并且可以从本地实例完成。

¥When you add Prisma Migrate to an existing database, you must baseline the production database. Baselining is performed once, and can be done from a local instance.

Baselining production from local with Prisma ORM