Skip to main content

生成向下迁移

本指南介绍如何生成反转给定 迁移文件 的向下迁移 SQL 文件。

¥This guide describes how to generate a down migration SQL file that reverses a given migration file.

关于向下迁移

¥About down migrations

生成迁移 SQL 文件时,你可能还希望创建一个 "向下迁移" SQL 文件来反转相应 "向上迁移" 文件中的架构更改。请注意,"向下迁移" 有时也称为 "迁移回滚"。

¥When generating a migration SQL file, you may wish to also create a "down migration" SQL file that reverses the schema changes in the corresponding "up migration" file. Note that "down migrations" are also sometimes called "migration rollbacks".

本指南介绍了如何使用 Prisma Migrate 的 migrate diff 命令 创建向下迁移,以及在向上迁移失败的情况下如何使用 db execute 命令将其应用到生产数据库。

¥This guide explains how to use Prisma Migrate's migrate diff command to create a down migration, and how to apply it to your production database with the db execute command in the case of a failed up migration.

warning

本指南仅适用于为关系数据库生成 SQL 向下迁移。它不适用于 MongoDB。

¥This guide applies to generating SQL down migrations for relational databases only. It does not apply to MongoDB.

info

migrate diffdb execute 命令在 3.9.0 及更高版本的预览版中可用,并且在 3.13.0 及更高版本中普遍可用。

¥The migrate diff and db execute commands are available in Preview in versions 3.9.0 and later, and are generally available in versions 3.13.0 and later.

生成向下迁移时的注意事项

¥Considerations when generating down migrations

生成向下迁移文件时,有一些注意事项需要注意:

¥When generating a down migration file, there are some considerations to be aware of:

  • 向下迁移可用于在使用 如何将向下迁移应用于失败的迁移 中的步骤迁移失败后恢复数据库架构。这需要使用 migrate resolve 命令,该命令只能用于失败的迁移。如果你的向上迁移成功并且你想要恢复它,则需要将 schema.prisma 文件恢复到向上迁移之前的状态,并使用 migrate dev 命令生成新的迁移。

    ¥The down migration can be used to revert your database schema after a failed migration using the steps in How to apply your down migration to a failed migration. This requires the use of the migrate resolve command, which can only be used on failed migrations. If your up migration was successful and you want to revert it, you will instead need to revert your schema.prisma file to its state before the up migration, and generate a new migration with the migrate dev command.

  • 向下迁移将恢复你的数据库架构,但作为向上迁移的一部分执行的对数据和应用代码的其他更改将不会恢复。例如,如果你有一个在迁移期间更改数据的脚本,则当你运行向下迁移时,该数据将不会更改回来。

    ¥The down migration will revert your database schema, but other changes to data and application code that are carried out as part of the up migration will not be reverted. For example, if you have a script that changes data during the migration, this data will not be changed back when you run the down migration.

  • 你将无法使用 migrate diff 恢复迁移文件中手动更改或添加的 SQL。如果你有任何自定义添加项,例如视图或触发器,你将需要:

    ¥You will not be able to use migrate diff to revert manually changed or added SQL in your migration files. If you have any custom additions, such as a view or trigger, you will need to:

    • 创建 下面的说明 之后的向下迁移

      ¥Create the down migration following the instructions below

    • 使用 migrate dev --create-only 创建向上迁移,以便在应用到数据库之前可以对其进行编辑

      ¥Create the up migration using migrate dev --create-only, so that it can be edited before it is applied to the database

    • 手动将自定义 SQL 添加到向上迁移(例如添加视图)

      ¥Manually add your custom SQL to the up migration (e.g. adding a view)

    • 手动将反向自定义 SQL 添加到向下迁移中(例如删除视图)

      ¥Manually add the inverted custom SQL to the down migration (e.g. dropping the view)

如何生成和运行迁移

¥How to generate and run down migrations

本节介绍如何生成向下迁移 SQL 文件以及相应的向上迁移,然后运行它以在生产上向上迁移失败后恢复数据库架构。

¥This section describes how to generate a down migration SQL file along with the corresponding up migration, and then run it to revert your database schema after a failed up migration on production.

例如,以以下 Prisma 架构和 UserPost 模型为起点:

¥As an example, take the following Prisma schema with a User and Post model as a starting point:

schema.prisma
model Post {
id Int @id @default(autoincrement())
title String @db.VarChar(255)
content String?
author User @relation(fields: [authorId], references: [id])
authorId Int
}

model User {
id Int @id @default(autoincrement())
name String?
posts Post[]
}

你需要先创建向下迁移,然后再创建相应的向上迁移。

¥You will need to create the down migration first, before creating the corresponding up migration.

生成迁移

¥Generating the migrations

  1. 编辑你的 Prisma 架构以进行向上迁移所需的更改。在此示例中,你将添加一个新的 Profile 模型:

    ¥Edit your Prisma schema to make the changes you require for your up migration. In this example, you will add a new Profile model:

    schema.prisma
    model Post {
    id Int @id @default(autoincrement())
    title String @db.VarChar(255)
    content String?
    author User @relation(fields: [authorId], references: [id])
    authorId Int
    }

    model Profile {
    id Int @id @default(autoincrement())
    bio String?
    user User @relation(fields: [userId], references: [id])
    userId Int @unique
    }

    model User {
    id Int @id @default(autoincrement())
    name String?
    posts Post[]
    profile Profile?
    }
  2. 生成用于向下迁移的 SQL 文件。为此,你将使用 migrate diff 进行比较:

    ¥Generate the SQL file for the down migration. To do this, you will use migrate diff to make a comparison:

    • 从新编辑的架构

      ¥from the newly edited schema

    • 到上次迁移后架构的状态

      ¥to the state of the schema after the last migration

    并将其输出到 SQL 脚本 down.sql

    ¥and output this to a SQL script, down.sql.

    有两个可能的选项可用于指定 'to' 状态:

    ¥There are two potential options for specifying the 'to' state:

    • 使用 --to-migrations:这与迁移目录中给出的迁移状态进行比较。这是首选选项,因为它更强大,但它需要 影子数据库。要使用此选项,请运行:

      ¥Using --to-migrations: this makes a comparison to the state of the migrations given in the migrations directory. This is the preferred option, as it is more robust, but it requires a shadow database. To use this option, run:

      npx prisma migrate diff \
      --from-schema-datamodel prisma/schema.prisma \
      --to-migrations prisma/migrations \
      --shadow-database-url $SHADOW_DATABASE_URL \
      --script > down.sql
    • 使用 --to-schema-datasource:这与数据库的状态进行比较。这不需要影子数据库,但它确实依赖于具有最新架构的数据库。要使用此选项,请运行:

      ¥Using --to-schema-datasource: this makes a comparison to the state of the database. This does not require a shadow database, but it does rely on the database having an up-to-date schema. To use this option, run:

      npx prisma migrate diff \
      --from-schema-datamodel prisma/schema.prisma \
      --to-schema-datasource prisma/schema.prisma \
      --script > down.sql
  3. 生成并应用名称为 add_profile 的向上迁移:

    ¥Generate and apply the up migration with a name of add_profile:

    npx prisma migrate dev --name add_profile

    这将在 prisma/migrations 目录中创建一个新的 <timestamp>_add_profile 目录,其中包含新的 migration.sql up 迁移文件。

    ¥This will create a new <timestamp>_add_profile directory inside the prisma/migrations directory, with your new migration.sql up migration file inside.

  4. down.sql 文件与向上迁移文件一起复制到新目录中。

    ¥Copy your down.sql file into the new directory along with the up migration file.

如何将向下迁移应用于失败的迁移

¥How to apply your down migration to a failed migration

如果之前的向上迁移失败,你可以通过以下步骤在生产数据库上应用向下迁移:

¥If your previous up migration failed, you can apply your down migration on your production database with the following steps:

要在向上迁移失败后对生产数据库应用向下迁移:

¥To apply the down migration on your production database after a failed up migration:

  1. 使用 db execute 在数据库服务器上运行 down.sql 文件:

    ¥Use db execute to run your down.sql file on the database server:

    npx prisma db execute --file ./down.sql --schema prisma/schema.prisma
  2. 使用 migrate resolve 记录你回滚了名为 add_profile 的上行迁移:

    ¥Use migrate resolve to record that you rolled back the up migration named add_profile:

    npx prisma migrate resolve --rolled-back add_profile