zenstack-db-migration
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseZenStack V3 — Database Migrations
ZenStack V3 — 数据库迁移
ZenStack keeps your database schema in sync with your ZModel data model. Migration is built on top
of Prisma Migrate: the CLI generates a Prisma
schema from your ZModel under the hood and wraps the corresponding Prisma command. If you know Prisma
Migrate, the workflow is identical — just swap for . Existing migration history keeps
working once moved into ZenStack's layout ( → , the default
location next to ).
zenprismazenprisma/migrationszenstack/migrationszenstack/schema.zmodelThis skill assumes a project is already set up (see ). Schema authoring is in
. carries a peer dependency on , installed
automatically when needed.
zenstack-project-setupzenstack-schema-modeling@zenstackhq/cliprismaRunafter schema changes to refresh generated types; migration commands operate on the schema independently of the generated client.zen generate
ZenStack 让数据库 Schema 与你的 ZModel 数据模型保持同步。迁移功能建立在
Prisma Migrate 之上: CLI 会在底层从 ZModel
生成 Prisma Schema,并封装对应的 Prisma 命令。如果你熟悉 Prisma Migrate,工作流程完全相同,
只需将 换成 。迁移到 ZenStack 的目录结构后,现有迁移历史仍然可用
( → ,默认位置与 相邻)。
zenprismazenprisma/migrationszenstack/migrationszenstack/schema.zmodel本 skill 假定项目已经完成设置(参见 )。Schema 建模请参见
。 将 作为 peer dependency,需要时会自动安装。
zenstack-project-setupzenstack-schema-modeling@zenstackhq/cliprismaSchema 更改后运行以刷新生成的类型;迁移命令独立于生成的 Client, 直接对 Schema 进行操作。zen generate
Two ways to evolve the schema
演进 Schema 的两种方式
| Approach | Command | When |
|---|---|---|
| Schema prototyping | | Local dev/experimentation — syncs the DB to the schema with no migration file. Fast, but loses history. Never in production. |
| Tracked migrations | | Anything you ship — generates reviewable, committed SQL migration files applied deterministically across environments. |
Typical loop: prototype with while iterating, then create a migration with
once the shape settles.
db pushmigrate dev| 方法 | 命令 | 使用场景 |
|---|---|---|
| Schema 原型设计 | | 本地开发或实验——将数据库同步至 Schema,不生成迁移文件。速度快,但不会保留历史记录。切勿在生产环境中使用。 |
| 受跟踪的迁移 | | 任何需要发布上线的场景——生成可审查、可提交到源码控制的 SQL 迁移文件,并在各个环境中确定性地应用。 |
典型流程:迭代时使用 进行原型开发,待结构确定后,再使用
创建迁移。
db pushmigrate devCommands
命令
All migration/db commands accept (defaults to ) and
(the directory holding ).
--schema <file>zenstack/schema.zmodel--migrations <path>migrations/所有迁移和数据库命令都接受 (默认为 )和
(存放 的目录)。
--schema <file>zenstack/schema.zmodel--migrations <path>migrations/zen migrate dev
— create + apply a migration (development)
zen migrate devzen migrate dev
— 创建并应用迁移(开发环境)
zen migrate devDiffs the schema against migration history, generates a new timestamped migration file, and applies
it to the dev database. Prompts if a full reset is required (e.g. drift).
bash
zen migrate dev --name add_published_flag- — name the migration (otherwise you're prompted).
-n, --name <name> - — generate the migration file without applying it. Use this to hand-edit SQL, or to implement features the migration engine doesn't generate yet (e.g. database views — create an empty migration and write the SQL manually), then apply later with
--create-only.zen migrate dev
Review the generated SQL before committing it to source control.
将 Schema 与迁移历史进行差异比较,生成一个带时间戳的新迁移文件,并将其应用到开发数据库。
如果需要完全重置(例如存在 Schema drift),会提示你确认。
bash
zen migrate dev --name add_published_flag- — 指定迁移名称(否则会提示输入)。
-n, --name <name> - — 仅生成迁移文件,不应用该文件。如果需要手动编辑 SQL, 或实现迁移引擎尚不能生成的功能(例如数据库视图——创建一个空迁移并手动编写 SQL), 可以使用此选项,之后再通过
--create-only应用。zen migrate dev
将生成的 SQL 提交到源码控制前,请先进行审查。
zen migrate deploy
— apply pending migrations (production)
zen migrate deployzen migrate deploy
— 应用待处理迁移(生产环境)
zen migrate deployApplies all not-yet-applied migrations. Idempotent and non-interactive — this is the command for
your deployment pipeline. It never generates new migrations or resets data.
bash
zen migrate deploy应用所有尚未应用的迁移。该命令具有幂等性且无需交互,是部署流水线应使用的命令。
它不会生成新迁移,也不会重置数据。
bash
zen migrate deployzen migrate status
— inspect migration state
zen migrate statuszen migrate status
— 检查迁移状态
zen migrate statusShows which migrations are applied vs. pending. Useful in CI before deploying.
bash
zen migrate status显示哪些迁移已应用、哪些迁移待应用。在部署前用于 CI 检查非常有用。
bash
zen migrate statuszen migrate resolve
— fix history without touching the schema
zen migrate resolvezen migrate resolve
— 修复迁移历史而不修改 Schema
zen migrate resolveMarks a migration as applied or rolled back — for manual recovery (e.g. a migration applied
out-of-band, or a failed one). Does not change the database schema.
bash
zen migrate resolve --applied 20240101120000_add_users
zen migrate resolve --rolled-back 20240101120000_add_users将迁移标记为已应用或已回滚,用于手动恢复(例如迁移曾被带外应用,或某个迁移执行失败)。
不会更改数据库 Schema。
bash
zen migrate resolve --applied 20240101120000_add_users
zen migrate resolve --rolled-back 20240101120000_add_userszen migrate reset
— wipe and reapply (development)
zen migrate resetzen migrate reset
— 清除并重新应用迁移(开发环境)
zen migrate resetDrops all tables and replays every migration from scratch. Destructive — dev/testing only, never
production.
bash
zen migrate reset --force # --force skips the confirmation promptIf a seed script is configured, it runs after reset (use to skip).
--skip-seed删除所有表,并从头重新执行每个迁移。该操作具有破坏性——仅限开发或测试环境,切勿在
生产环境中使用。
bash
zen migrate reset --force # --force skips the confirmation prompt如果配置了种子脚本,重置后会自动运行该脚本(使用 可跳过)。
--skip-seedzen db push
— sync schema, no migration file (development)
zen db pushzen db push
— 同步 Schema,不生成迁移文件(开发环境)
zen db pushPushes the schema straight to the database. Great for rapid prototyping; no history is recorded.
bash
zen db push
zen db push --accept-data-loss # proceed despite data-loss warnings
zen db push --force-reset # reset the DB before pushing直接将 Schema 推送到数据库。适合快速原型开发;不会记录迁移历史。
bash
zen db push
zen db push --accept-data-loss # proceed despite data-loss warnings
zen db push --force-reset # reset the DB before pushingzen db pull
— introspect an existing database (v3.4.0+, experimental)
zen db pullzen db pull
— 对现有数据库进行内省(v3.4.0+,实验性)
zen db pullLoads the database schema, diffs it against your ZModel, and updates the ZModel to match. Useful for
adopting ZenStack on an existing DB, or re-syncing after out-of-band DB changes. This has its own
implementation (not Prisma's introspection engine).
bash
zen db pullOptions: , (default ),
(default ), (always emit /),
(default ), (default ).
-o, --output <path>--model-casing <pascal|camel|snake|none>pascal--field-casing <...>camel--always-map@map@@map--quote <double|single>single--indent <number>4Warning:can overwrite your schema file. Commit or back it up first.zen db pull
加载数据库 Schema,将其与 ZModel 进行差异比较,并更新 ZModel 以使其保持一致。适用于
在现有数据库上采用 ZenStack,或在数据库发生带外更改后重新同步。该命令使用自己的实现,
而不是 Prisma 的 introspection engine。
bash
zen db pull选项:、(默认为 )、
(默认为 )、(始终生成 /)、
(默认为 )、(默认为 )。
-o, --output <path>--model-casing <pascal|camel|snake|none>pascal--field-casing <...>camel--always-map@map@@map--quote <double|single>single--indent <number>4警告:可能覆盖你的 Schema 文件。请先提交或备份该文件。zen db pull
zen db seed
— seed the database
zen db seedzen db seed
— 初始化数据库种子数据
zen db seedRuns the seed script declared in under :
package.jsonzenstack.seedjson
{ "zenstack": { "seed": "tsx ./zenstack/seed.ts" } }bash
zen db seed
zen db seed -- --users 10 # args after -- are forwarded to the seed script运行 中在 下声明的种子脚本:
package.jsonzenstack.seedjson
{ "zenstack": { "seed": "tsx ./zenstack/seed.ts" } }bash
zen db seed
zen db seed -- --users 10 # args after -- are forwarded to the seed scriptWorkflows
工作流
Development
- Edit .
zenstack/schema.zmodel - to try it instantly while iterating (optionally
zen db push).zen db seed - When the shape is final, to create the migration.
zen migrate dev --name <change> - Review the SQL, commit the migration file.
Production / CI deployment
bash
zen migrate deployRun it in the release pipeline. Optionally gate on first. Do not run
, , or against production.
zen migrate statusdb pushmigrate devmigrate resetAdopt an existing database
zen initurlzen db pullzen generatezenstack-project-setup开发环境
- 编辑 。
zenstack/schema.zmodel - 迭代时运行 以立即应用更改(也可以选择运行
zen db push)。zen db seed - 结构最终确定后,运行 创建迁移。
zen migrate dev --name <change> - 审查 SQL,然后提交迁移文件。
生产环境 / CI 部署
bash
zen migrate deploy在发布流水线中运行该命令。也可以先用 设置部署前的检查关卡。
不要对生产环境运行 、 或 。
zen migrate statusdb pushmigrate devmigrate reset采用现有数据库
zen initurlzen db pullzen generatezenstack-project-setupAdopting from Prisma
从 Prisma 迁移
Because migration wraps Prisma Migrate, your existing migration history keeps working once you move
the directory to (ZenStack's default, next to
) and replace the / scripts with the
equivalents. Full framework migration is covered by .
prisma/migrationszenstack/migrationszenstack/schema.zmodelprisma migrate ...prisma db ...zenzenstack-migrate-from-prisma由于迁移功能封装了 Prisma Migrate,将现有的 目录移动到
(ZenStack 的默认位置,与 相邻),并将
/ 脚本替换为对应的 命令后,现有迁移历史仍然可用。
完整的框架迁移流程参见 。
prisma/migrationszenstack/migrationszenstack/schema.zmodelprisma migrate ...prisma db ...zenzenstack-migrate-from-prismaReference docs
参考文档
Full ZenStack documentation for this topic is bundled under :
references/- migration.md — migration concepts and workflows
- introspection.md — introspection
zen db pull - cli-reference.md — complete /
migratecommand + option referencedb
本主题的完整 ZenStack 文档随附于 :
references/- migration.md — 迁移概念与工作流
- introspection.md — 内省
zen db pull - cli-reference.md — 完整的 /
migrate命令及选项参考db