status: accepted
Checkout restores what was applied, but does not apply anything new
When switching branches with uncommitted migration changes, checkout reverts what the transition requires, switches the branch, restores the stash, and then re-applies the migrations it reverted, including local-only ones that were applied before. It does not apply migrations that are new on the target branch unless --and-up is given.
Two competing principles
Two intuitions pull in different directions when a local-only migration was applied before checkout:
- "The target branch rules." After checkout, the deployment should match the target branch. A local-only migration is an addition and should stay reverted without
--and-up. - "What I had, I want back." A migration that was applied and is still present after the stash is restored should be applied again.
We chose the second. The developer's intent is to continue where they left off: restore what was rolled out, but do not pull in extra work they did not ask for. A local-only migration is technically an addition, but from the developer's view it was reverted by checkout and is still there, so it comes back.
The transition is planned from commit to commit
The plan compares the current commit with the target commit, never the working tree with the target. Local changes are reconciled separately before and after. The sequence with --allow-dirty is:
- Revert migrations whose local files differ from the current commit.
- Stash local changes.
- Re-apply the committed versions, so the deployment matches the current commit.
- Revert what the transition from the current to the target commit requires.
- Check out the target.
- Restore the stash.
- Re-apply everything reverted, from the final files. With
--and-up, also apply new migrations.
Without --allow-dirty, a dirty working tree is an error and only steps 4, 5 and 7 run.
Steps 1 to 3 look redundant, but a working-tree-based plan breaks in this case: the current branch has v2 of a migration applied, the target branch has v1, and the developer has edited the file back to v1 locally. The working tree then equals the target, so no change is detected and the deployment stays at v2 while the file says v1. Going through the current commit first makes the v2-to-v1 transition visible. A regression test covers this case.