自定义迁移
在某些情况下,您需要在应用迁移文件之前对其进行编辑。例如,要更改一对一关系的方向(将外键从一侧移动到另一侧)而不会丢失数据,您需要将数据作为迁移的一部分进行移动——此 SQL 不属于默认迁移,必须手动编写。
本指南解释了如何编辑迁移文件,并给出了一些您可能需要这样做的用例示例。
如何编辑迁移文件
要在应用迁移文件之前对其进行编辑,一般过程如下:
-
进行需要自定义 SQL 的模式更改(例如,为了保留现有数据)
-
使用以下命令创建草稿迁移:
npx prisma migrate dev --create-only -
修改生成的 SQL 文件。
-
通过运行以下命令应用修改后的 SQL:
npx prisma migrate dev
示例:重命名字段
默认情况下,在模式中重命名字段会导致以下迁移:
CREATE一个新列(例如,fullname)DROP现有列(例如,name)和该列中的数据
要在生产环境中运行迁移时实际重命名字段并避免数据丢失,您需要在将其应用到数据库之前修改生成的迁移 SQL。考虑以下模式片段——biograpy 字段拼写错误。
model Profile {
id Int @id @default(autoincrement())
biograpy String
userId Int @unique
user User @relation(fields: [userId], references: [id])
}
要将 biograpy 字段重命名为 biography:
-
在模式中重命名字段
model Profile {
id Int @id @default(autoincrement())
biograpy String
biography String
userId Int @unique
user User @relation(fields: [userId], references: [id])
} -
运行以下命令创建草稿迁移,您可以在将其应用到数据库之前进行编辑:
npx prisma migrate dev --name rename-migration --create-only -
按照所示编辑草稿迁移,将
DROP/DELETE更改为单个RENAME COLUMN- 之前
- 之后
./prisma/migrations/20210308092620_rename_migration/migration.sqlALTER TABLE "Profile" DROP COLUMN "biograpy",
ADD COLUMN "biography" TEXT NOT NULL;./prisma/migrations/20210308092620_rename_migration/migration.sqlALTER TABLE "Profile"
RENAME COLUMN "biograpy" TO "biography"对于 SQL Server,您应该使用存储过程
sp_rename而不是ALTER TABLE RENAME COLUMN。./prisma/migrations/20210308092620_rename_migration/migration.sqlEXEC sp_rename 'dbo.Profile.biograpy', 'biography', 'COLUMN'; -
保存并应用迁移
npx prisma migrate dev
您可以使用相同的技术重命名 model——编辑生成的 SQL 以重命名表,而不是删除并重新创建它。
示例:使用扩展和收缩模式在不停机的情况下演进模式
对现有字段进行模式更改(例如,重命名字段)可能导致停机。这发生在应用修改现有字段的迁移与部署使用修改后字段的新版应用程序代码之间。
您可以通过将更改字段所需的步骤分解为一系列旨在逐步引入更改的离散步骤来防止停机。这种模式被称为扩展和收缩模式。
该模式涉及两个组件:访问数据库的应用程序代码和您打算更改的数据库模式。
使用扩展和收缩模式,将字段 bio 重命名为 biography 在 Prisma 中将如下所示:
-
将新的
biography字段添加到您的 Prisma 模式并创建迁移model Profile {
id Int @id @default(autoincrement())
bio String
biography String
userId Int @unique
user User @relation(fields: [userId], references: [id])
} -
扩展:更新应用程序代码并同时写入
bio和biography字段,但继续从bio字段读取,并部署代码 -
创建空迁移并将现有数据从
bio复制到biography字段npx prisma migrate dev --name copy_biography --create-onlyprisma/migrations/20210420000000_copy_biography/migration.sqlUPDATE "Profile" SET biography = bio; -
验证数据库中
biography字段的完整性 -
更新应用程序代码以从新的
biography字段读取 -
更新应用程序代码以停止写入
bio字段 -
收缩:从 Prisma 模式中删除
bio,并创建迁移以删除bio字段model Profile {
id Int @id @default(autoincrement())
bio String
biography String
userId Int @unique
user User @relation(fields: [userId], references: [id])
}npx prisma migrate dev --name remove_bio
通过使用这种方法,您可以避免更改应用程序代码中使用的现有字段可能导致的潜在停机,并减少应用迁移和部署更新的应用程序代码之间所需的协调量。
请注意,此模式适用于涉及更改具有数据并正在被应用程序代码使用的列的任何情况。示例包括将两个字段合并为一个,或将 1:n 关系转换为 m:n 关系。
要了解更多信息,请查阅 Data Guide 关于扩展和收缩模式的文章
示例:更改一对一关系的方向
要更改一对一关系的方向:
-
在模式中进行更改
model User {
id Int @id @default(autoincrement())
name String
posts Post[]
profile Profile? @relation(fields: [profileId], references: [id])
profileId Int @unique
}
model Profile {
id Int @id @default(autoincrement())
biography String
user User
} -
运行以下命令创建草稿迁移,您可以在将其应用到数据库之前进行编辑:
npx prisma migrate dev --name rename-migration --create-only显示CLI结果⚠️ There will be data loss when applying the migration:
• The migration will add a unique constraint covering the columns `[profileId]` on the table `User`. If there are existing duplicate values, the migration will fail. -
按照所示编辑草稿迁移
- 之前
- 之后
-- DropForeignKey
ALTER TABLE "Profile" DROP CONSTRAINT "Profile_userId_fkey";
-- DropIndex
DROP INDEX "Profile_userId_unique";
-- AlterTable
ALTER TABLE "Profile" DROP COLUMN "userId";
-- AlterTable
ALTER TABLE "User" ADD COLUMN "profileId" INTEGER NOT NULL;
-- CreateIndex
CREATE UNIQUE INDEX "User_profileId_unique" ON "User"("profileId");
-- AddForeignKey
ALTER TABLE "User" ADD FOREIGN KEY ("profileId") REFERENCES "Profile"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- DropForeignKey
ALTER TABLE "Profile" DROP CONSTRAINT "Profile_userId_fkey";
-- DropIndex
DROP INDEX "Profile_userId_unique";
-- AlterTable
ALTER TABLE "User" ADD COLUMN "profileId" INTEGER;
UPDATE "User"
SET "profileId" = "Profile".id
FROM "Profile"
WHERE "User".id = "Profile"."userId";
ALTER TABLE "User" ALTER COLUMN "profileId" SET NOT NULL;
-- AlterTable
ALTER TABLE "Profile" DROP COLUMN "userId";
-- CreateIndex
CREATE UNIQUE INDEX "User_profileId_unique" ON "User"("profileId");
-- AddForeignKey
ALTER TABLE "User" ADD FOREIGN KEY ("profileId") REFERENCES "Profile"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-
保存并应用迁移
npx prisma migrate dev