建立数据库基线
基线化是初始化数据库迁移历史记录的过程:
¥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.
为什么需要基线
¥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:
你可以编辑初始迁移以包含无法在 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:
但是,当你 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.
基线通过告诉 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:
-
如果你有
prisma/migrations
文件夹,请删除、移动、重命名或存档该文件夹。¥If you have a
prisma/migrations
folder, delete, move, rename, or archive this folder. -
运行以下命令以你喜欢的名称在其中创建
migrations
目录。此示例将使用0_init
作为迁移名称:¥Run the following command to create a
migrations
directory inside with your preferred name. This example will use0_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. -
生成迁移并使用
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 -
为每个应忽略的迁移运行
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:
-
跳过标记为 'applied' 的所有迁移,包括基线迁移
¥Skips all migrations marked as 'applied', including the baseline migration
-
应用基线迁移之后发生的任何新迁移
¥Applies any new migrations that come after the baseline migration