serverpod-migrations
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseServerpod Migrations
Serverpod Migrations
Serverpod has a migration system that generates SQL for changes to models with in . The migrations are applied:
table.spy.yaml- When the tool is called via the
apply_migrationsMCP. Typically during development with a runningserverpod.serverpod start - When the server is started with flag. Typically when running the server in production.
dart run bin/main.dart --apply-migrations
Serverpod 拥有一套迁移系统,可为 中标记为 的模型变更生成 SQL。迁移的应用场景如下:
.spy.yamltable- 通过 MCP 调用
serverpod工具时。通常在开发环境中运行apply_migrations期间使用。serverpod start - 使用 标志启动服务器时。通常在生产环境中运行服务器时使用。
dart run bin/main.dart --apply-migrations
When migrations are needed
何时需要迁移
- Added, removed, or renamed models or fields in
table..spy.yaml - Changed relation fields that alter generated foreign keys.
- Added, removed, or changed indexes.
Migrations are not needed when the project has no database configured (e.g. with no section).
config/<runMode>.yamldatabase- 在 中添加、移除或重命名
.spy.yaml模型或字段时。table - 修改关联字段导致生成的外键发生变化时。
- 添加、移除或修改索引时。
如果项目未配置数据库(例如 中没有 部分),则无需迁移。
config/<runMode>.yamldatabaseStandard flow
标准流程
The standard flow for creating and applying migrations is simplified when a is running.
serverpod start当 运行时,创建和应用迁移的标准流程会被简化。
serverpod startWith a running serverpod start
and MCP server
serverpod start运行 serverpod start
并连接 MCP 服务器时
serverpod startWhen the server is running from use the MCP to:
serverpod startserverpod- Create a migration using the tool.
create_migration - Apply the migration using the tool.
apply_migrations
ALWAYS use the MCP server if it is available.
当服务器通过 运行时,使用 MCP 执行以下操作:
serverpod startserverpod- 使用 工具创建迁移。
create_migration - 使用 工具应用迁移。
apply_migrations
如果 MCP 服务器可用,请务必使用它。
ONLY if MCP server fails to connect
仅当 MCP 服务器连接失败时
When the server is not running from use the CLI commands to:
serverpod start- Ensure the code is generated by running .
serverpod generate - Create a migration using the command.
serverpod create-migration
当服务器未通过 运行时,使用 CLI 命令执行以下操作:
serverpod start- 运行 确保代码已生成。
serverpod generate - 使用 命令创建迁移。
serverpod create-migration
Editing a generated migration
编辑生成的迁移
A migration directory holds , and the , and files.
migration.sqldefinition.sqldefinition.jsondefinition_project.jsonmigration.jsonmigration.sql- Adding a data transformation, so existing rows are migrated along with the schema.
- Turning a destructive change into a non-destructive one, by reaching the same end state through intermediate steps — for example add the new column, backfill it from the old one, then drop the old column, instead of dropping and recreating.
Never edit the other files in the directory. is the full schema, and the files are what the next diffs against, so changing them corrupts every migration created afterwards.
definition.sql*.jsonserverpod create-migrationTwo rules follow from how migrations are applied:
- A database that has no migrations installed is created from the latest alone and never runs
definition.sql. An existing database applies each newermigration.sqlin order. So the schema an editedmigration.sqlends up with must stay identical tomigration.sql, and data transformations in it only affect databases that upgrade through that version.definition.sql - Editing a migration that has already been applied does nothing to the databases that ran it. Create a new migration for those.
On the client side (models with or ), the same applies to the migration SQL inside the version's ; its definition SQL and JSON files are equally off limits.
database: clientdatabase: allmigration.dart迁移目录包含 、 以及 、 和 文件。
migration.sqldefinition.sqldefinition.jsondefinition_project.jsonmigration.json创建迁移后,可以手动编辑 。常见的两种原因:
migration.sql- 添加数据转换,使现有数据行随架构一起迁移。
- 将破坏性变更转换为非破坏性变更,通过中间步骤达到相同的最终状态——例如先添加新列,从旧列回填数据,再删除旧列,而不是直接删除并重新创建。
切勿编辑目录中的其他文件。 是完整的架构文件, 文件是下一次 进行对比的基准,修改它们会破坏后续创建的所有迁移。
definition.sql*.jsonserverpod create-migration根据迁移的应用方式,有两条规则需要遵守:
- 未安装任何迁移的数据库仅会从最新的 创建,不会运行
definition.sql。已存在的数据库会按顺序应用每个较新的migration.sql。因此,编辑后的migration.sql最终生成的架构必须与migration.sql完全一致,其中的数据转换仅影响通过该版本升级的数据库。definition.sql - 编辑已应用的迁移对已运行该迁移的数据库没有任何作用。如需修改,请创建新的迁移。
在客户端(标记为 或 的模型),版本的 中的迁移 SQL 同样适用上述规则;其定义 SQL 和 JSON 文件同样不可修改。
database: clientdatabase: allmigration.dartRepair migrations
修复迁移
If the database is in an inconsistent state, a repair migration brings it back to a consistent state. It is created by reading the live schema and diffing it against a target migration version, so the database must be reachable.
如果数据库处于不一致状态,修复迁移可使其恢复到一致状态。它通过读取实时架构并与目标迁移版本进行对比来创建,因此数据库必须可访问。
With a running serverpod start
and MCP server
serverpod start运行 serverpod start
并连接 MCP 服务器时
serverpod start- Create the repair migration using the tool. Optional arguments:
create_repair_migration(target migration version, defaults to the latest),version, andtag(required for destructive changes, or when no drift is detected).force - Apply it using the tool, which applies both pending and repair migrations without restarting the server.
apply_migrations
ALWAYS use the MCP server if it is available.
- 使用 工具创建修复迁移。可选参数:
create_repair_migration(目标迁移版本,默认为最新版本)、version和tag(破坏性变更或未检测到差异时需要)。force - 使用 工具应用修复迁移,该工具无需重启服务器即可应用待处理的迁移和修复迁移。
apply_migrations
如果 MCP 服务器可用,请务必使用它。
ONLY if MCP server fails to connect
仅当 MCP 服务器连接失败时
bash
undefinedbash
undefinedUse the --mode
flag to specify the run mode
--mode使用 --mode
标志指定运行模式
--modeUse the --version
flag to specify the target version
--version使用 --version
标志指定目标版本
--versionUse the --force
flag to create a migration with destructive changes
--force使用 --force
标志创建包含破坏性变更的迁移
--forceUse the --tag
flag to name the migration
--tag使用 --tag
标志为迁移命名
--tagserverpod create-repair-migration [--mode production] [--version <name>] [--force] [--tag <tag>]
Apply the repair migration by restarting the server with `dart run bin/main.dart --apply-repair-migration`. Ask the user to run this command.serverpod create-repair-migration [--mode production] [--version <name>] [--force] [--tag <tag>]
通过运行 `dart run bin/main.dart --apply-repair-migration` 重启服务器来应用修复迁移。请告知用户执行此命令。