解决数据库迁移文件冲突
核对分支迁移记录与已执行环境,生成兼容修复并在临时数据库验证部署顺序。
完整提示词
<context>
Two branches that each add a migration often merge without a textual conflict and then break: Django reports conflicting leaf nodes, Alembic reports multiple heads, Rails and Flyway have two migrations whose ordering assumptions differ, Flyway refuses a version lower than one already applied, Prisma's migration history no longer matches. The fix depends on the tool's model: graph-based tools (Django, Alembic) need a merge migration that joins the two leaves, or one branch's migration re-parented onto the other; timestamp or version-ordered tools (Rails, Flyway, Liquibase changelogs, EF Core) need renumbering or reordering so the newer migration really runs after the other. The one thing that decides what is safe is where each migration has already run: a migration applied in a shared or production database must never be edited, renamed or renumbered, because the database records it by name or checksum. Also check for a semantic clash: both migrations altering the same table or column, which merges cleanly but fails or loses data at run time.
</context>
<task>
<situation>
{{context}}
</situation>
1. Identify the tool and read both branches' new migrations in full, plus the tool's current state (`showmigrations`, `alembic heads`, `rails db:migrate:status`, `flyway info`, `prisma migrate status`, the EF Core migrations list).
2. Ask, if not stated, which environments (developer databases, staging, production) have run each migration. Migrations that ran anywhere shared are frozen.
3. Check for a semantic clash: same table, column, index or constraint touched by both; dependent data migrations; and ordering assumptions (one adds a column the other's data migration reads).
4. Fix by the tool's model:
- Django: `makemigrations --merge` and review the generated merge migration, or re-point the unapplied migration's dependencies.
- Alembic: `alembic merge` the heads, or set the unapplied revision's `down_revision` to the other head.
- Rails, Flyway, Liquibase, EF Core: renumber, re-timestamp or regenerate only the migration that has not run anywhere shared, so it runs last; regenerate model snapshots where the tool keeps them (EF Core, Prisma). A conflicted Rails `schema.rb` or `structure.sql` is regenerated by running the merged migrations, not hand-merged.
- Prisma: resolve schema.prisma, then create a new migration from the merged schema rather than editing history.
5. Run the migrations from scratch on a disposable database and, if the tool supports it, from the current state of a copy of the target branch's schema. Run the tests.
6. Report which files changed and what each environment will do on next deploy.
</task>
<constraints>
- Never modify, rename or renumber a migration that has run on a shared or production database.
- Never run migrations against a shared or production database; use a local or disposable one.
- Do not merge migrations by hand-editing generated state files when the tool can generate the merge.
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>
<output_format>
## The clash
Tool, the two migrations, where each has run.
## Semantic conflicts
Shared tables or ordering assumptions, or "None".
## Fix
Files changed and commands run.
## Check
Migration run results from scratch and from the target state, and tests.
## On next deploy
What each environment will run, in order.
</output_format>可替换内容
- {{context}}
- {{> guardrails/investigate-before-answering}}
- {{> guardrails/verify-before-done}}
分类与标签
编程开发、数据库迁移、分支合并、迁移顺序、临时数据库验证