Is there a rake task that shows the pending migrations in a rails app?
10 Answers
rake db:migrate:status
(Rails 3 to 5) or rails db:migrate:status
(Rails 5) will accomplish this. See this commit.
up
means the migration has been run. down
means the migration has not been run.

- 6,703
- 3
- 37
- 49

- 32,230
- 28
- 81
- 120
-
I'm getting `Don't know how to build task 'db:migrate:status'` on rails 3.2.8 – Peter Ehrlich Oct 10 '12 at 21:00
-
1@PeterEhrlich: Does it not show up in `rake -T` but the other ones do? – jrdioko Oct 11 '12 at 00:37
-
2bundle exec rake db:migrate:status – Nadeem Yasin Mar 22 '13 at 11:09
-
2How do you read the output of this command? If the status of a row is "down", does that mean it's a pending migration? – Dennis Feb 13 '15 at 15:00
-
13`up` means the migration has been run. `down` means the migration has not been run. – Josh Aug 02 '15 at 19:55
-
check out this gist for more rake tasks not in `rake -T` @jrdioko https://gist.github.com/amejiarosario/2950888 – mswieboda Feb 17 '16 at 18:38
There is rake db:abort_if_pending_migrations
(at least in Rails 2.3.3, not sure when it was introduced). The description says 'Raises an error if there are pending migrations'. This seems to be used more as a prerequisite for other tasks, but I'm guessing you could use it for your purposes.
EDIT: Here is an example of the output after having just generated and not run a 'test' migration
rails_project theIV$ rake db:abort_if_pending_migrations
(in /Users/theIV/Sites/rails_project/)
You have 1 pending migrations:
20090828200602 Test
Run "rake db:migrate" to update your database then try again.

- 25,434
- 5
- 54
- 58
-
I love adding this to orchestrate dependencies between rails containers – Vincent De Smet Mar 28 '17 at 09:42
-
-
This command will list all migrations with their status (UP
or DOWN
)
Rails 3 and 4
rake db:migrate:status
Rails 5 and above
rake db:migrate:status
# Or
rails db:migrate:status
This will return something like
database: blog_development
Status Migration ID Migration Name
--------------------------------------------------
up 20210131124730 Create articles
down 20210131124801 Create users

- 22,834
- 10
- 68
- 88
This works for rails 5.2
ActiveRecord::Base.connection.migration_context.needs_migration?

- 2,708
- 1
- 25
- 34
If you want to see how much migration is done or pending you can see using below command.
rails db:migrate:status
More on this link: Rails Active Record Migration

- 483
- 5
- 12
If you need a bash one-liner to determine whether to run a migration or not (e.g., only migrate in a Heroku release phase command when there is a pending migration), this works:
(rails db:migrate:status | grep "^\s*down") && rails db:migrate || echo "No pending migrations found."

- 390
- 4
- 16
Try rake -h (help) and have a look at rake -n (= rake --dry-run). So probably something like rake -n db:migrate should get you what you want.

- 3,787
- 29
- 27
-
3That won't work. It just prints: ** Invoke db:migrate (first_time) ** Invoke environment (first_time) ** Execute (dry run) environment ** Execute (dry run) db:migrate – jrdioko Jan 24 '11 at 19:25
Might not quite be what OP is asking for, but if you just need to quickly check if any migrations are pending for use in a rake task, without resorting to
rake db:migrate:status | grep down (might not work if you're on Windows)
ActiveRecord::Migration.check_pending! (raises ActiveRecord::PendingMigrationError that you need to rescue)
you can use needs_migration? method: https://apidock.com/rails/v4.0.2/ActiveRecord/Migrator/needs_migration%3F/class

- 757
- 8
- 15
Following command to check migration status:
rake db:migrate:status
OR
when you run your server, it will display a message to run your pending migration first.

- 4,341
- 2
- 21
- 27