Skip to main content

如何管理团队中的架构变更

10 min

介绍

¥Introduction

在团队中工作时,管理数据库模式更改可能具有挑战性。本指南将向你展示如何使用 Prisma Migrate 有效地协作进行架构更改,确保所有团队成员都能安全地参与并整合架构更改。

¥When working in a team, managing database schema changes can be challenging. This guide shows you how to effectively collaborate on schema changes using Prisma Migrate, ensuring that all team members can safely contribute to and incorporate schema changes.

先决条件

¥Prerequisites

在开始本指南之前,请确保你已准备好:

¥Before starting this guide, make sure you have:

  • 已安装 Node.js(版本 18 或更高版本)

    ¥Node.js installed (version 18 or higher)

  • 一个已设置好迁移的 Prisma 项目

    ¥A Prisma project set up with migrations

  • 关系数据库(PostgreSQL、MySQL、SQLite、SQL Server 等)

    ¥A relational database (PostgreSQL, MySQL, SQLite, SQL Server, etc.)

  • 了解 Git 基本知识

    ¥Basic understanding of Git

  • 基本了解 Prisma Migrate

    ¥Basic familiarity with Prisma Migrate

警告

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

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

1. 了解迁移基础知识

¥ Understand migration basics

1.1.迁移顺序

¥1.1. Migration order

迁移的应用顺序与创建顺序相同。创建日期是迁移子文件夹名称的一部分 - 例如,20210316081837-updated-fields 是在 2021-03-16-08:18:37 上创建的。

¥Migrations are applied in the same order as they were created. The creation date is part of the migration subfolder name - for example, 20210316081837-updated-fields was created on 2021-03-16-08:18:37.

1.2.源代码控制要求

¥1.2. Source control requirements

你应该将以下文件提交到源代码管理:

¥You should commit the following files to source control:

  • .prisma/migrations 文件夹的内容,包括 migration_lock.toml 文件

    ¥The contents of the .prisma/migrations folder, including the migration_lock.toml file

  • Prisma Schema (schema.prisma)

schema.prisma 文件进行源代码控制还不够 - 你必须包含你的迁移历史记录,因为:

¥Source-controlling the schema.prisma file is not enough - you must include your migration history because:

  • 自定义迁移包含 Prisma 模式无法表示的信息

    ¥Customized migrations contain information that cannot be represented in the Prisma schema

  • prisma migrate deploy 命令仅运行迁移文件

    ¥The prisma migrate deploy command only runs migration files

2. 整合团队变更

¥ Incorporate team changes

2.1.提取最新更改

¥2.1. Pull latest changes

要整合协作者的变更:

¥To incorporate changes from collaborators:

  1. 拉取已更改的 Prisma 架构和 ./prisma/migrations 文件夹

    ¥Pull the changed Prisma schema and ./prisma/migrations folder

  2. 运行迁移命令:

    ¥Run the migrate command:

npx prisma migrate dev

2.2.示例场景

¥2.2. Example scenario

让我们来看一个示例场景,其中三位开发者共享架构更改:

¥Let's walk through a sample scenario with three developers sharing schema changes:

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

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

3. 处理并发更改

¥ Handle concurrent changes

3.1.开发者 A 的更改

¥3.1. Developer A's changes

Ania 添加了一个新字段:

¥Ania adds a new field:

model User {
/* ... */
favoriteColor String?
}

并生成迁移:

¥And generates a migration:

npx prisma migrate dev --name new-field

3.2.开发者 B 的更改

¥3.2. Developer B's changes

Javier 添加了一个新的模型:

¥Javier adds a new model:

model Tag {
tagName String @id
tagCategory Category
}

并生成迁移:

¥And generates a migration:

npx prisma migrate dev --name new-model

3.3.合并变更

¥3.3. Merge changes

迁移历史记录现在包含两个新的迁移:

¥The migration history now has two new migrations:

A diagram showing changes by two separate developers converging in a single migration history.

4. 集成你的更改

¥ Integrate your changes

4.1.提取团队更改

¥4.1. Pull team changes

  1. 拉取最新的更改:

    ¥Pull the most recent changes:

    • 两次新的迁移

      ¥Two new migrations

    • 更新的架构文件

      ¥Updated schema file

  2. 检查合并后的模式:

    ¥Review the merged schema:

model User {
/* ... */
favoriteColor String?
bestPacmanScore Int?
}

model Tag {
tagName String @id
tagCategory Category
posts Post[]
}

4.2.生成你的迁移

¥4.2. Generate your migration

运行迁移命令:

¥Run the migrate command:

npx prisma migrate dev

这将造成:

¥This will:

  1. 应用你团队的迁移

    ¥Apply your team's migrations

  2. 为你的更改创建一个新的迁移文件

    ¥Create a new migration for your changes

  3. 应用你的新迁移

    ¥Apply your new migration

4.3.提交更改

¥4.3. Commit changes

提交:

¥Commit:

  • 合并后的 schema.prisma

    ¥The merged schema.prisma

  • 你的新迁移文件

    ¥Your new migration file

下一步

¥Next steps

现在你已经了解了团队模式管理,你可以:

¥Now that you understand team schema management, you can:

更多信息:

¥For more information:


Stay connected with Prisma

Continue your Prisma journey by connecting with our active community. Stay informed, get involved, and collaborate with other developers:

We genuinely value your involvement and look forward to having you as part of our community!