In Rails, how do db:migrate VERSION=, db:migrate:up, db:migrate:down, db:migrate:redo and db:migrate:status differ when you work on one migration?
answer
- VERSION= as a target, both directions
- up and down touch one migration
- VERSION is required for up and down
- redo is rollback plus migrate
- status prints up, down and NO FILE
basics
~20 sdb:migrate VERSION=x moves the database to x, rolling back everything above it or migrating up to it. db:migrate:up and :down run one migration's single direction, redo rolls back and re-runs, and status lists each version as up or down.
solid answer
~40 s`db:migrate VERSION=x` treats `x` as a **target**: if the database is above it, every applied migration newer than `x` runs down (newest first) and `x` stays applied; otherwise pending migrations up to `x` run. `db:migrate:up VERSION=x` and `db:migrate:down VERSION=x` touch **only** migration `x`, and do nothing if it is already in that state; both require `VERSION`. `db:migrate:redo` runs `db:rollback` then `db:migrate`, honouring `STEP=`, or with `VERSION=` runs that migration down and up, which is how you test a fresh migration's down path. `db:migrate:status` prints each version as `up` or `down` with its humanised name, and `********** NO FILE **********` for a row with no matching file. In a multi-database environment the unnamed `:up`, `:down` and `:redo` tasks raise and you use the `:primary`-style variants.
code
bash · 5 linesbin/rails db:migrate VERSION=20261001090000 # roll back everything above it
bin/rails db:migrate:down VERSION=20261002143000
bin/rails db:migrate:up VERSION=20261002143000
bin/rails db:migrate:redo # rollback + migrate
bin/rails db:migrate:statusgo deeper
Know that db:migrate:status shows which migrations ran and that db:migrate:redo checks a new migration's down path.
Explain VERSION= as a target that can go both ways, why up and down need VERSION, and what each status label means.
Use these tasks without corrupting shared history: no down on old migrations under newer ones, roll back branch-only migrations before switching, and use the namespaced variants on multi-database apps.
Decide what the team expects to work locally (redo on every new migration) and what is never done on shared databases.
## Five tasks, one migration in focus While writing or debugging a migration in a dental-practice app, you rarely want "run everything" or "undo the last one". You want to aim at one version. Rails gives several tasks for that, and the differences are worth knowing exactly. | Task | Scope | What it runs | |---|---|---| | `db:migrate VERSION=x` | every migration between now and `x` | down for applied migrations above `x`, or up for pending ones up to `x` | | `db:migrate:up VERSION=x` | migration `x` only | its up direction, skipped if already applied | | `db:migrate:down VERSION=x` | migration `x` only | its down direction, skipped if not applied | | `db:migrate:redo` | the last `STEP` migrations (default 1) | `db:rollback` then `db:migrate` | | `db:migrate:redo VERSION=x` | migration `x` only | down then up | | `db:migrate:status` | everything | a report, no changes | ## db:migrate VERSION=x With `VERSION`, `db:migrate` stops being "run pending" and becomes "move to this version": - **Target below the current version.** Rails runs the down direction of every applied migration with a higher version, newest first. Migration `x` itself is **kept**. - **Target above the current version.** Rails runs pending migrations up to and including `x`, and leaves later ones pending. - **`VERSION=0`** rolls back every migration. The version must look like a migration version; a malformed value raises "Invalid format of target version", and a version with no matching file raises `ActiveRecord::UnknownMigrationVersionError`. ## db:migrate:up and db:migrate:down These run **exactly one** migration in one direction, which is what you want when only one file needs attention: ```bash bin/rails db:migrate:down VERSION=20261002143000 bin/rails db:migrate:up VERSION=20261002143000 ``` - `VERSION` is mandatory. `db:migrate:down` without it fails with a message telling you to use `db:rollback` to go down one migration. - Later migrations are left alone, so `db:migrate:down` on an older version can leave the schema in a state the next migrations never saw. Use it on recent work, not on old history. - If migration `x` is already in the requested state, the task does nothing. ## db:migrate:redo `redo` exists to answer one question: does this migration's down path actually work? 1. Without `VERSION`, it runs `db:rollback` (honouring `STEP`) and then `db:migrate`. 2. With `VERSION=x`, it runs `db:migrate:down VERSION=x` and then `db:migrate:up VERSION=x`. Running `bin/rails db:migrate:redo` right after `db:migrate` on a new migration catches an `ActiveRecord::IrreversibleMigration` or a wrong `down` on your machine, instead of on a teammate's. Remember that redo runs real code: if the down path drops a column, its data is gone before up recreates it empty. ## db:migrate:status ``` Status Migration ID Migration Name -------------------------------------------------- up 20250101000000 ********** NO FILE ********** up 20260915080000 Create patients up 20261001090000 Create appointments down 20261002143000 Add allergy notes to patients ``` - **up** means a row exists in `schema_migrations` for that file. - **down** means a file exists with no row: it is pending. - **NO FILE** means a row exists but no file matches it, usually because old migrations were deleted or because a branch with that migration was switched away. It reads only the database and `db/migrate`; it does not look at the schema file. If `schema_migrations` does not exist yet, it aborts with "Schema migrations table does not exist yet." ## A working loop for a new migration 1. Generate and write the migration. 2. `bin/rails db:migrate` to apply it, and read the printed schema calls. 3. `bin/rails db:migrate:redo` to prove the down path and apply it again. 4. If the migration needs another edit, `bin/rails db:migrate:down VERSION=<its version>`, edit, then `bin/rails db:migrate:up VERSION=<its version>`. 5. `bin/rails db:migrate:status` before committing, to make sure nothing unexpected is still down. 6. Commit the migration together with the regenerated schema file. Editing is safe at step 4 only because the migration has not left your machine. Once it has run elsewhere, the fix is a new migration. ## Pitfalls - `db:rollback VERSION=x` is not a thing: it raises and points you to `db:migrate:down`. - After switching git branches, `status` may show `NO FILE` rows for migrations that exist only on the other branch; roll those back **before** switching if you need a clean database. - In a multi-database environment, the unnamed `db:migrate:up`, `db:migrate:down` and `db:migrate:redo` raise and list the namespaced tasks, such as `db:migrate:down:primary`.
- What is the difference between db:migrate VERSION=x and db:migrate:down VERSION=x when x is below the current version?`db:migrate VERSION=x` moves the database to `x`: every applied migration with a higher version runs down, and `x` stays applied. `db:migrate:down VERSION=x` runs the down direction of `x` alone and leaves every later migration applied.
- Why might db:migrate:status show NO FILE right after switching git branches?Your database still has `schema_migrations` rows for migrations that exist only on the branch you left, and the current checkout has no files for them. Rails cannot run their down code without the files, so roll those migrations back before switching, or rebuild the development database.
- db:migrate VERSION=x can only move the database forward
- db:migrate:down without VERSION rolls back the last migration
- db:migrate:up VERSION=x runs the migration again even if it is applied
- db:migrate:redo runs only the up direction a second time
- db:migrate:status decides up or down by reading db/schema.rb