skip to content

In Rails, how do timestamped migration versions behave when two branches each add a migration, and how do you resolve the db/schema.rb conflict?

level: middleimportance: should knowfreq 40%

answer

  1. UTC YYYYMMDDHHMMSS from the generator
  2. both branches touch the version line
  3. regenerate by migrating, never hand-merge
  4. versions below the line count as applied
  5. future timestamps rejected since 7.2 defaults

basics

~20 s

Each migration's version is a UTC timestamp, so two branches rarely collide, but both rewrite db/schema.rb's version line and often the same tables. Resolve by migrating the merged code and committing the regenerated file, never by hand-picking lines.

solid answer

~40 s

The generator stamps each file with a UTC `YYYYMMDDHHMMSS` version, so migrations from two branches get different versions and both run: `db:migrate` runs any file without a `schema_migrations` row, whatever its position. The conflict is in `db/schema.rb`: both branches changed its `version:` line and often the same `create_table` block. The fix is to take either side, run `bin/rails db:migrate` on the merged code so both migrations are applied locally, and commit the file the dumper regenerates. Hand-merging is dangerous because loading the file marks **every migration below its version** as applied: a merged file that lost one branch's change silently gives every new database that gap. Since `load_defaults 7.2`, `validate_migration_timestamps` rejects a version more than a day in the future with `ActiveRecord::InvalidMigrationTimestampError`, and duplicate versions or class names raise their own errors.

code

bash · 4 lines
bash
git merge feature/watchlists          # conflict in db/schema.rb
git checkout --theirs db/schema.rb    # either side will do
bin/rails db:migrate                  # runs the other branch's migration
git add db/schema.rb && git commit

go deeper

for a junior

Know that migration versions are timestamps and that a schema.rb conflict is fixed by running db:migrate and committing the result.

for a middle

Explain why the version line conflicts, why both migrations still run, and what loading does with versions below the line.

for a senior

Explain the silent gap a hand-merged schema.rb can create, and check that late-merged migrations do not depend on ones that ran before them elsewhere.

for a principal

Set the merge discipline: who regenerates the schema file, how CI catches a stale one, and how long-lived branches are kept from drifting.

## Versions are timestamps `bin/rails generate migration` names each file after the current **UTC time**: `20261003101500_add_reserve_price_to_auctions.rb`. The 14-digit prefix is the migration's version. If the newest existing file already has a later number, the generator uses that number plus one instead, so versions only increase in one checkout. (`config.active_record.timestamped_migrations = false` switches to plain sequential numbers, which is exactly what makes parallel branches collide.) Because two developers rarely generate migrations in the same second, branches almost never produce the same version. That is the point of timestamps. ## What two branches actually conflict on In the online-auction app, branch A adds `20261003101500_add_reserve_price_to_auctions.rb` and branch B adds `20261005093000_create_watchlists.rb`. The migration files never conflict; they are different files. `db/schema.rb` does: - **The version line.** A's dump says `version: 2026_10_03_101500`, B's says `version: 2026_10_05_093000`. - **Table blocks.** If both changed `auctions`, both added lines to the same block. On Rails 8.1.4, columns are dumped in the database's own order, so two branches that each append a column produce neighbouring changes in the same place. Rails 8.1.0 sorted columns alphabetically partly to reduce this, but 8.1.4 reverted that sort. ## Resolving it 1. Merge, and accept either side of `db/schema.rb`. 2. Run `bin/rails db:migrate`. Your local database already has one branch's migration; the other is pending because it has no row, so it runs now, whatever its timestamp. 3. Rails rewrites `db/schema.rb` from the database, with both changes and the highest version. 4. Commit that file. This is what the Rails migrations guide recommends: run `db:migrate` to regenerate the schema file after a conflict. ## Why hand-merging is dangerous Loading a schema file marks the file's version **and every migration file below it** as applied. Suppose a hand merge keeps B's `version: 2026_10_05_093000` but drops A's `reserve_price_cents` column by mistake: - every database built from the file (tests, new laptops, `db:prepare` on a new server) lacks the column; - `schema_migrations` there still lists `20261003101500`, because it is below the version line; - `db:migrate` therefore never runs A's migration on those databases. Nothing raises. The gap shows up later as a missing-column error in tests or on a new environment. A regenerated file cannot have this problem, because it was dumped from a database that ran both migrations. ## A quick check after resolving 1. The `version:` line equals the highest version among the files in `db/migrate`. 2. `git diff` against the main branch shows **both** branches' table changes in `db/schema.rb`, not just one. 3. `bin/rails db:migrate:status` shows no migration as down. 4. The test suite passes on a database rebuilt from the file, which is what CI and new laptops will use. If any of these fails, migrate again and re-dump rather than editing the file. ## Out-of-order migrations on shared databases Staging may already have run B (October 5) when A (October 3) merges. `db:migrate` still runs A, because "pending" means "no row", not "newer than the current version". The ordering assumption to check is in the migrations themselves: A must not depend on something B changed, since on staging B ran first while on a new database built from the schema both are simply present. ## Long-lived branches The longer a branch lives, the more of these effects pile up: - Its migrations get **older timestamps** than everything merged meanwhile, so on shared databases they run after migrations with higher versions. - Its `schema.rb` falls further behind, making the merge conflict larger. - Rebasing does not change a migration's version; regenerating the migration to get a fresh timestamp is a choice, not a requirement, as long as nothing depends on the order. Merging `main` into the branch often, migrating, and committing the regenerated schema keeps each conflict small. ## Guardrails Rails adds | Situation | What Rails does | |---|---| | Two files with the same version | raises `ActiveRecord::DuplicateMigrationVersionError` | | Two files defining the same class name | raises `ActiveRecord::DuplicateMigrationNameError` | | A version more than a day ahead of now | raises `ActiveRecord::InvalidMigrationTimestampError` when `validate_migration_timestamps` is on, which `load_defaults 7.2` and later set | | A file name that is not `<version>_<name>.rb` | raises `ActiveRecord::IllegalMigrationNameError` | The future-timestamp check matters for hand-written names: a file named `20991231000000_...` would sort after every real migration for decades and break the ordering of everything generated after it.

  • Why does Rails reject a migration named 20991231000000_add_reserve_to_auctions.rb?
    With `validate_migration_timestamps` on (the `load_defaults 7.2` and later setting), a version more than a day ahead of the current UTC time raises `ActiveRecord::InvalidMigrationTimestampError`. A far-future version would sort after every migration generated for decades, so later migrations would run before it on new databases.
  • Two branches both add a migration class called AddIndexToBids with different timestamps. What happens after the merge?
    Rails raises `ActiveRecord::DuplicateMigrationNameError` when it loads the migration list, because each migration file defines a class and two files define the same one. Rename one, for example to `AddAmountIndexToBids`, before merging.

  • db:migrate skips a migration whose version is lower than the newest applied one
  • Resolve the schema.rb conflict by keeping the lower version so nothing is skipped
  • Two migrations may share a class name as long as their timestamps differ
  • Rails numbers migrations 001, 002, 003 by default
  • Rails 8.1.4 sorts columns alphabetically, so branches never conflict inside a table

Assumes Rails 8.1.4 with load_defaults 8.1: validate_migration_timestamps is on since the 7.2 defaults, and schema.rb columns follow database order again after 8.1.4 reverted 8.1.0's alphabetical sort.