Skip to main content

开发生产

本页面介绍如何在开发和生产环境中使用 Prisma Migrate 命令。

¥This page explains how to use Prisma Migrate commands in development and production environments.

开发环境

¥Development environments

在开发环境中,使用 migrate dev 命令生成并应用迁移:

¥In a development environment, use the migrate dev command to generate and apply migrations:

npx prisma migrate dev

创建并应用迁移

¥Create and apply migrations

danger

migrate dev 是开发命令,切勿在生产环境中使用。

¥migrate dev is a development command and should never be used in a production environment.

这个命令:

¥This command:

  1. 重新运行 影子数据库 中的现有迁移历史记录,以检测架构漂移(编辑或删除迁移文件,或对数据库架构进行手动更改)

    ¥Reruns the existing migration history in the shadow database in order to detect schema drift (edited or deleted migration file, or a manual changes to the database schema)

  2. 将待处理的迁移应用到影子数据库(例如,同事创建的新迁移)

    ¥Applies pending migrations to the shadow database (for example, new migrations created by colleagues)

  3. 如果它检测到 Prisma 架构发生更改,则会根据这些更改生成新的迁移

    ¥If it detects changes to the Prisma schema, it generates a new migration from these changes

  4. 将所有未应用的迁移应用到开发数据库并更新 _prisma_migrations

    ¥Applies all unapplied migrations to the development database and updates the _prisma_migrations table

  5. 触发工件的生成(例如,Prisma Client)

    ¥Triggers the generation of artifacts (for example, Prisma Client)

在以下情况下,migrate dev 命令会提示你重置数据库:

¥The migrate dev command will prompt you to reset the database in the following scenarios:

重置开发数据库

¥Reset the development database

你还可以通过运行以下命令自行 reset 数据库以撤消手动更改或 db push 实验:

¥You can also reset the database yourself to undo manual changes or db push experiments by running:

npx prisma migrate reset
warning

migrate reset 是开发命令,切勿在生产环境中使用。

¥migrate reset is a development command and should never be used in a production environment.

这个命令:

¥This command:

  1. 如果可能的话,删除数据库/模式 1,或者如果环境不允许删除数据库/模式 1 则执行软重置

    ¥Drops the database/schema¹ if possible, or performs a soft reset if the environment does not allow deleting databases/schemas¹

  2. 如果数据库/架构 被删除,则创建一个具有相同名称的新数据库/架构 1

    ¥Creates a new database/schema¹ with the same name if the database/schema¹ was dropped

  3. 应用所有迁移

    ¥Applies all migrations

  4. 运行种子脚本

    ¥Runs seed scripts

1 对于 MySQL 和 MongoDB,这指的是数据库,对于 PostgreSQL 和 SQL Server 来说,指的是架构,对于 SQLite 来说,指的是数据库文件。

¥¹ For MySQL and MongoDB this refers to the database, for PostgreSQL and SQL Server to the schema, and for SQLite to the database file.

注意:如需根据需要在开发数据库中重新创建数据的简单且集成的方法,请查看我们的 播种指南

¥Note: For a simple and integrated way to re-create data in your development database as often as needed, check out our seeding guide.

自定义迁移

¥Customizing migrations

有时,你需要在应用迁移之前对其进行修改。例如:

¥Sometimes, you need to modify a migration before applying it. For example:

  • 你想要引入重大重构,例如将博客文章标签从 String[] 更改为 Tag[]

    ¥You want to introduce a significant refactor, such as changing blog post tags from a String[] to a Tag[]

  • 你想要 重命名字段(默认情况下,Prisma Migrate 将删除现有字段)

    ¥You want to rename a field (by default, Prisma Migrate will drop the existing field)

  • 你想要 改变 1-1 关系的方向

    ¥You want to change the direction of a 1-1 relationship

  • 你想要添加无法用 Prisma Schema Language 表示的功能 - 例如部分索引或存储过程。

    ¥You want to add features that cannot be represented in Prisma Schema Language - such as a partial index or a stored procedure.

--create-only 命令允许你创建迁移而不应用它:

¥The --create-only command allows you to create a migration without applying it:

npx prisma migrate dev --create-only

要应用编辑后的迁移,请再次运行 prisma migrate dev

¥To apply the edited migration, run prisma migrate dev again.

请参阅 自定义迁移 的示例。

¥Refer to Customizing migrations for examples.

团队开发

¥Team development

看:使用 Prisma Migrate 进行团队开发 .

¥See: Team development with Prisma Migrate .

生产和测试环境

¥Production and testing environments

在生产和测试环境中,使用 migrate deploy 命令应用迁移:

¥In production and testing environments, use the migrate deploy command to apply migrations:

npx prisma migrate deploy

注意:migrate deploy 通常应该是自动化 CI/CD 管道的一部分,我们不建议在本地运行此命令来将更改部署到生产数据库。

¥Note: migrate deploy should generally be part of an automated CI/CD pipeline, and we do not recommend running this command locally to deploy changes to a production database.

这个命令:

¥This command:

  1. 将应用的迁移与迁移历史记录进行比较,并警告是否有任何迁移已被修改:

    ¥Compares applied migrations against the migration history and warns if any migrations have been modified:

    WARNING The following migrations have been modified since they were applied:
    20210313140442_favorite_colors
  2. 应用待处理的迁移

    ¥Applies pending migrations

migrate deploy 命令:

¥The migrate deploy command:

  • 如果迁移历史记录中缺少已应用的迁移,则不会触发警告

    ¥Does not issue a warning if an already applied migration is missing from migration history

  • 不检测漂移(生产数据库架构与迁移历史结束状态不同 - 例如,由于修补程序)

    ¥Does not detect drift (production database schema differs from migration history end state - for example, due to a hotfix)

  • 不重置数据库或生成工件(例如 Prisma Client)

    ¥Does not reset the database or generate artifacts (such as Prisma Client)

  • 不依赖影子数据库

    ¥Does not rely on a shadow database

也可以看看:

¥See also:

咨询锁定

¥Advisory locking

当你运行生产命令时,Prisma Migrate 使用建议锁定,例如:

¥Prisma Migrate makes use of advisory locking when you run production commands such as:

  • prisma migrate deploy

  • prisma migrate dev

  • prisma migrate resolve

此保护措施可确保多个命令不能同时运行 - 例如,如果你快速连续合并两个拉取请求。

¥This safeguard ensures that multiple commands cannot run at the same time - for example, if you merge two pull requests in quick succession.

咨询锁定有 10 秒超时(不可配置),并使用底层提供程序中可用的默认咨询锁定机制:

¥Advisory locking has a 10 second timeout (not configurable), and uses the default advisory locking mechanism available in the underlying provider:

Prisma Migrate 实现建议锁定纯粹是为了避免灾难性错误 - 如果你的命令超时,你将需要再次运行它。

¥Prisma Migrate's implementation of advisory locking is purely to avoid catastrophic errors - if your command times out, you will need to run it again.

5.3.0 起,可以使用 PRISMA_SCHEMA_DISABLE_ADVISORY_LOCK 环境变量 禁用建议锁定

¥Since 5.3.0, the advisory locking can be disabled using the PRISMA_SCHEMA_DISABLE_ADVISORY_LOCK environment variable