Skip to main content

建立数据库基线

基线化是初始化数据库迁移历史记录的过程:

¥Baselining is the process of initializing a migration history for a database that:

  • ✔ 在你开始使用 Prisma Migrate 之前就已存在

    ¥✔ Existed before you started using Prisma Migrate

  • ✔ 包含必须维护的数据(如生产),这意味着数据库无法重置

    ¥✔ Contains data that must be maintained (like production), which means that the database cannot be reset

基线告诉 Prisma Migrate 假设已应用一项或多项迁移。这可以防止生成的迁移在尝试创建已存在的表和字段时失败。

¥Baselining tells Prisma Migrate to assume that one or more migrations have already been applied. This prevents generated migrations from failing when they try to create tables and fields that already exist.

注意:我们假设重置和种子开发数据库是可以接受的。

¥Note: We assume it is acceptable to reset and seed development databases.

基线是 将 Prisma Migrate 添加到具有现有数据库的项目 的一部分。

¥Baselining is part of adding Prisma Migrate to a project with an existing database.

warning

本指南不适用于 MongoDB。
使用 db push,而不是 migrate deploy,用于 MongoDB

¥This guide does not apply for MongoDB.
Instead of migrate deploy, db push is used for MongoDB.

为什么需要基线

¥Why you need to baseline

当你将 Prisma Migrate 添加到现有项目时,你的初始迁移包含在开始使用 Prisma Migrate 之前重新创建数据库状态所需的所有 SQL:

¥When you add Prisma Migrate to an existing project, your initial migration contains all the SQL required to recreate the state of the database before you started using Prisma Migrate:

The image shows a database labelled 'Existing database', and a list of existing database features next to it - 24 tables, 13 relationships, 92 fields, 3 indexes. An arrow labelled 'represented by' connects the database features list to a box that represents a migration. The existing databases's features are represented by a single migration.

提示

你可以编辑初始迁移以包含无法在 Prisma 架构中表示的架构元素 - 例如存储过程或触发器。

¥You can edit the initial migration to include schema elements that cannot be represented in the Prisma schema - such as stored procedures or triggers.

你需要此初始迁移来创建和重置开发环境:

¥You need this initial migration to create and reset development environments:

The image shows a migration history with three migrations. Each migration is represented by a file icon and a name, and all migrations are surrounded by a box labelled 'migration history'. The first migration has an additional label: "State of database before Prisma Migrate", and the two remaining migrations are labelled "Generated as part of the Prisma Migrate workflow". An arrow labelled "prisma migrate dev" connects the migration history box to a database labelled "new development database", signifying that all three migrations are applied to the development database - none are skipped.

但是,当你 prisma migrate deploy 迁移到已存在且无法重置的数据库时 - 比如生产 - 你不想包含初始迁移。

¥However, when you prisma migrate deploy your migrations to databases that already exist and cannot be reset - such as production - you do not want to include the initial migrations.

目标数据库已包含初始迁移创建的表和列,尝试再次创建这些元素很可能会导致错误。

¥The target database already contains the tables and columns created by the initial migration, and attempting to create these elements again will most likely result in an error.

A migration history represented by three migration files (file icon and name), surrounded by a a box labelled 'migration history'. The first migration is marked 'do not apply', and the second two migrations are marked 'apply'. An arrow labelled with the command 'prisma migrate deploy' points from the migration history to a database labelled 'production'.

基线通过告诉 Prisma Migrate 假装已应用初始迁移来解决此问题。

¥Baselining solves this problem by telling Prisma Migrate to pretend that the initial migration(s) have already been applied.

建立数据库基线

¥Baselining a database

要创建基线迁移:

¥To create a baseline migration:

  1. 如果你有 prisma/migrations 文件夹,请删除、移动、重命名或存档该文件夹。

    ¥If you have a prisma/migrations folder, delete, move, rename, or archive this folder.

  2. 运行以下命令以你喜欢的名称在其中创建 migrations 目录。此示例将使用 0_init 作为迁移名称:

    ¥Run the following command to create a migrations directory inside with your preferred name. This example will use 0_init for the migration name:

    mkdir -p prisma/migrations/0_init
    信息

    那么 0_ 很重要,因为 Prisma Migrate 在 字典顺序 中应用迁移。你可以使用不同的值,例如当前时间戳。

    ¥Then 0_ is important because Prisma Migrate applies migrations in a lexicographic order. You can use a different value such as the current timestamp.

  3. 生成迁移并使用 prisma migrate diff 将其保存到文件中

    ¥Generate a migration and save it to a file using prisma migrate diff

    npx prisma migrate diff \
    --from-empty \
    --to-schema-datamodel prisma/schema.prisma \
    --script > prisma/migrations/0_init/migration.sql
  4. 为每个应忽略的迁移运行 prisma migrate resolve 命令:

    ¥Run the prisma migrate resolve command for each migration that should be ignored:

    npx prisma migrate resolve --applied 0_init

此命令将目标迁移添加到 _prisma_migrations 表并将其标记为已应用。当你运行 prisma migrate deploy 以应用新迁移时,Prisma Migrate:

¥This command adds the target migration to the _prisma_migrations table and marks it as applied. When you run prisma migrate deploy to apply new migrations, Prisma Migrate:

  1. 跳过标记为 'applied' 的所有迁移,包括基线迁移

    ¥Skips all migrations marked as 'applied', including the baseline migration

  2. 应用基线迁移之后发生的任何新迁移

    ¥Applies any new migrations that come after the baseline migration