Rebasing Migrations in GitHub Stacked Pull Requests
In a stack of pull requests on
GitHub, each pull request targets the branch of the one below it. When a migration lands on main, or on
a lower pull request, the atlas.sum file of every pull request above it conflicts. GitHub's cascading
rebase can't resolve atlas.sum, so someone has to rebase every pull request in the stack by hand.
With stack: true, the
migrate/autorebase action does this
restack for you. It checks the stacks on every push. When a stack's migrations conflict, it rebases each
pull request onto the one below it, moves that pull request's migration files on top, and pushes all the
branches of the stack at once.
This guide sets up the workflow and follows it on a demo repository with a stack of three pull requests.
A teammate's migration lands on main first, then a lower pull request adds a migration, then a restack
fails on a conflict the action can't resolve, and finally the stack merges. The logs are copied from
those runs.
The stack input requires
atlas-action v1.19.0 or later. To build
stacks and to restack locally when the action refuses, install the
gh stack extension.
Why Stacks Conflict on atlas.sum
atlas.sum stores a hash of each migration file, and its first line is a hash of the whole directory
(how the integrity file works). Two branches that each add a
migration both rewrite that first line, and Git can't merge them.
The demo stack has three pull requests. Each one adds a table:
Stack #4: main <- #1 (orders) <- #2 (order-items) <- #3 (shipments)
| Branch | Base | Migration it adds |
|---|---|---|
orders (#1) | main | 20261202110636_create_orders.sql |
order-items (#2) | orders | 20261202110649_create_order_items.sql |
shipments (#3) | order-items | 20261202110651_create_shipments.sql |
Every layer starts from the branch below it and adds its migration the same way:
git switch -c orders main
atlas migrate new create_orders --dir file://migrations
# Write the CREATE TABLE statement into the new file, then:
atlas migrate hash --dir file://migrations
git add migrations
git commit -m "migrations: create orders"
Push the branches, open a pull request for each, and link them into a stack, for example with
gh stack link from the
gh stack extension.
While the stack was in review, a teammate merged #5, which adds 20261202110702_add_user_profiles.sql.
Its version is newer than all three migrations in the stack. Merging main into orders now stops on
atlas.sum:
$ git merge origin/main
Auto-merging migrations/atlas.sum
CONFLICT (content): Merge conflict in migrations/atlas.sum
Automatic merge failed; fix conflicts and then commit the result.
GitHub marks #1 as conflicting, and its pull_request workflows stop running. Fixing it by hand takes
three rounds: rebase orders onto main, move create_orders after add_user_profiles with
atlas migrate rebase, run atlas migrate hash, and push. Then do the same for order-items on the new
orders, and for shipments on the new order-items.
Set Up the Workflow
1. Choose the token that pushes
The action pushes the restacked branches with the token you give actions/checkout. The token decides
which workflows run on them afterwards.
- With the default
GITHUB_TOKEN, the pushes start nopushworkflows, and thepull_requestworkflows they start wait for approval. This token also can't push a restack that creates a new version of a workflow file, which happens when a pull request changes a workflow file that also changed below it. - With a personal access token, CI runs on the restacked pull requests. Each branch the action pushes also starts one more run of the restack workflow, which finds the stack up to date and does nothing.
The workflow runs on a push to any branch, so anyone who can push a branch can read the token. Use a fine-grained personal access token or a GitHub App token, limited to this repository, with Contents set to read and write. Add Workflows read and write only if your stacks change workflow files. Use an identity that can't bypass branch protection.
2. Add the secrets
Add two repository secrets:
PAT: the token from the previous step.ATLAS_TOKEN: an Atlas Cloud bot token, whichariga/setup-atlasuses to log in.
3. Add the workflow
Add .github/workflows/atlas-restack.yaml:
name: Restack Atlas Migrations
on:
# Pull request workflows do not run while a pull request has conflicts, and a
# stack needs a restack when the branch it targets moves. Run on every push.
push:
branches: ['**']
jobs:
migrate-auto-rebase:
# Restacks must not overlap. All runs share one group and wait in line.
concurrency:
group: atlas-autorebase
queue: max
permissions:
contents: write # push the restacked branches
pull-requests: read # read the pull requests and their stacks
runs-on: ubuntu-latest
steps:
- uses: ariga/setup-atlas@v0
with:
cloud-token: ${{ secrets.ATLAS_TOKEN }}
- uses: actions/checkout@v4
with:
# Pushes made with this token start CI on the restacked pull requests.
token: ${{ secrets.PAT }}
# The restack reads the history of every branch in the stack.
fetch-depth: 0
- name: Configure git
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
- uses: ariga/atlas-action/migrate/autorebase@v1
with:
dir: file://migrations
stack: true
env:
GITHUB_TOKEN: ${{ github.token }}
Each run restacks the stack of the pushed branch, plus every open stack that targets it, so a push to
main covers all the stacks built on main. Pull requests that aren't stacked and target the base
branch get the same rebase as without stack, so one workflow handles both kinds.
The action renames files without checking whether the schema changes still work in their new order, so
keep migrate/lint in your pull request
workflow. In the demo, a pull_request workflow that runs atlas migrate validate ran on every pull
request the action restacked.
Restack After a Migration Merges to main
Merging #5 pushed to main. The run of that push found stack #4 targeting main and restacked it:
auto-rebase stacks with git version 2.56.1
Stack #4: main <- #1 (orders) <- #2 (order-items) <- #3 (shipments)
#1 (orders): migrations conflict with main
#1 (orders): replaying 1 commit(s) onto main (b8bf4cc)
#1 (orders): renamed 20261202110636_create_orders.sql to 20261202110858_create_orders.sql, to place them on top of main
#2 (order-items): replaying 1 commit(s) onto #1 (orders) (08bef50)
#2 (order-items): renamed 20261202110649_create_order_items.sql to 20261202110859_create_order_items.sql, to place them on top of #1 (orders)
#3 (shipments): replaying 1 commit(s) onto #2 (order-items) (fffbf09)
#3 (shipments): renamed 20261202110651_create_shipments.sql to 20261202110900_create_shipments.sql, to place them on top of #2 (order-items)
Pushed orders (3c05e11..08bef50), order-items (42db800..fffbf09), shipments (85a655b..18daf29)
Starting at the bottom, the action replays only the commits of each pull request onto the new head of the
one below it. It renames that pull request's migrations with atlas migrate rebase so they sort after
everything below, and records the renames in a migrations: rebase migration files commit. The three
branches go out in one push with --force-with-lease, about 20 seconds after the merge.
The top of the stack now holds every migration in order:
$ git ls-tree --name-only origin/shipments migrations/
migrations/20261202110625_create_users.sql
migrations/20261202110702_add_user_profiles.sql
migrations/20261202110858_create_orders.sql
migrations/20261202110859_create_order_items.sql
migrations/20261202110900_create_shipments.sql
migrations/atlas.sum
$ git log --oneline origin/shipments
18daf29 migrations: rebase migration files
7ff92d7 migrations: create shipments
fffbf09 migrations: rebase migration files
cd78c75 migrations: create order_items
08bef50 migrations: rebase migration files
fd7f451 migrations: create orders
b8bf4cc migrations: add user profiles (#5)
ab9478d migrations: create users
Because the pushes used a personal access token, CI started on #1, #2, and #3, and each pushed branch started one more restack run. All three found nothing to do:
Stack #4: main <- #1 (orders) <- #2 (order-items) <- #3 (shipments)
Migrations of all the pull requests are up to date, nothing to restack
The action rewrote the remote branches, so update your local copy before you commit again:
$ git switch orders
$ git pull --rebase
Successfully rebased and updated refs/heads/orders.
Restack After a Lower Pull Request Adds a Migration
Next, the author of #1 added 20261202111035_add_order_status.sql to orders and pushed. That file is
newer than the migrations of #2 and #3, so both of them now conflicted with orders. The run of that
push restacked the stack from #2 up and left #1 alone:
Stack #4: main <- #1 (orders) <- #2 (order-items) <- #3 (shipments)
#2 (order-items): migrations conflict with #1 (orders)
#2 (order-items): replaying 2 commit(s) onto #1 (orders) (ffc5b40)
#2 (order-items): renamed 20261202110859_create_order_items.sql to 20261202111058_create_order_items.sql, to place them on top of #1 (orders)
#3 (shipments): replaying 2 commit(s) onto #2 (order-items) (16f5b93)
#3 (shipments): renamed 20261202110900_create_shipments.sql to 20261202111059_create_shipments.sql, to place them on top of #2 (order-items)
Pushed order-items (fffbf09..16f5b93), shipments (18daf29..fe0047c)
Each pull request still has a single rebase migration files commit. While that commit is the last one
on the branch, later restacks amend it instead of adding another:
$ git log --oneline origin/shipments
fe0047c migrations: rebase migration files
99a4df1 migrations: create shipments
16f5b93 migrations: rebase migration files
f1b11d6 migrations: create order_items
ffc5b40 migrations: add order status
08bef50 migrations: rebase migration files
fd7f451 migrations: create orders
b8bf4cc migrations: add user profiles (#5)
ab9478d migrations: create users
Restack Locally When the Action Refuses
atlas.sum is the only file the action resolves, so the demo set up a conflict in another file. A
teammate merged #6, which adds an audit_log table and a README.md that lists the tables, and the
action restacked the stack onto it. Then #1 added an orders line to the end of that list.
Another teammate then merged #7, which adds a roles table and a roles line at the same spot. The new
migration on main meant #1 needed a restack, and replaying #1 stopped on README.md:
Stack #4: main <- #1 (orders) <- #2 (order-items) <- #3 (shipments)
#1 (orders): migrations conflict with main
#1 (orders): replaying 5 commit(s) onto main (0d0f58a)
Error: stack #4: #1 (orders): conflicts with main in README.md. Nothing was pushed. Restack it locally with `gh stack rebase`, then push it with `gh stack push`
The run failed and pushed nothing, so the branches were exactly as their authors left them. The action also refuses, with the reason in the error, when a pull request:
- doesn't target the branch of the pull request below it,
- has a merge commit with changes of its own, which a rebase would drop,
- edits migration files of the pull request below it after a restack changed them,
- has commits the pull request below it no longer has,
- adds a checkpoint file.
To fix it, restack locally as the error says.
gh stack rebase
pauses at each conflict. Resolve README.md as usual. Resolve atlas.sum the same way as for a single
pull request: run atlas migrate rebase on the branch's migration versions so they sort last, then
atlas migrate hash. Push with gh stack push. In the demo, after the fixed branches were pushed, the
three runs they started reported nothing to restack.
Merge the Stack
Merging #1 on its own moved #2 onto main, and GitHub rebased #2 and #3. Their migrations didn't
conflict, so the action left the branches as GitHub pushed them:
Stack #4: main <- #2 (order-items) <- #3 (shipments)
Migrations of all the pull requests are up to date, nothing to restack
When a stack is only behind its base, with no migration conflict, the action leaves it to GitHub, which can rebase it from the pull request page.
Merging #3 then merged #2 with it. main ends with one linear history, the stack's migrations after the
ones that merged while it was in review:
$ git ls-tree --name-only origin/main migrations/
migrations/20261202110625_create_users.sql
migrations/20261202110702_add_user_profiles.sql
migrations/20261202111326_create_audit_log.sql
migrations/20261202111546_create_roles.sql
migrations/20261202111833_create_orders.sql
migrations/20261202111834_add_order_status.sql
migrations/20261202111854_create_order_items.sql
migrations/20261202111903_create_shipments.sql
migrations/atlas.sum
Limitations
stack: trueworks only on GitHub Actions, and can't be combined withbase-shaorforce-rebase.- The action resolves only the
atlas.sumof thedirinput. If a stack and its base both change another migration directory, restack it locally. - In formats other than Atlas's, such as
golang-migrateorgoose, the action replays the commits but can't rename the files. A pull request whose migrations don't already sort after the ones below it is left as it is, and the run fails. - The action recreates the commits it replays, without signatures. Branches that require signed commits reject the push unless the workflow sets up commit signing.
- Every push starts a job. Most find nothing to restack, but each one pays for a full-history checkout. The runs go one at a time: up to 100 wait in line, GitHub cancels the rest, and restacks fall behind when pushes come faster than the runs finish.
- Unlike
gh stack push, which skips queued branches, the action also restacks pull requests in the merge queue.
Upgrade an Existing Workflow
If you already run migrate/autorebase, turning on stacks means changing the workflow to match the one
above: run on pushes to every branch including main, add the concurrency group and
pull-requests: read, pass GITHUB_TOKEN to the step, and set stack: true. Without stack, the
action behaves as it did before.