Show pending migrations in rails

Ruby on-RailsRails Migrations

Ruby on-Rails Problem Overview


Is there a rake task that shows the pending migrations in a rails app?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

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.

Solution 3 - Ruby on-Rails

This command will list all migrations with their status (UP or DOWN)

Rails 3 and 4

rake db:migrate:status

Rails 5

rake db:migrate:status

# Or

rails db:migrate:status

Solution 4 - Ruby on-Rails

rake db:version will accomplish this on Rails 2.

Solution 5 - Ruby on-Rails

This works for rails 5.2

ActiveRecord::Base.connection.migration_context.needs_migration?

Solution 6 - Ruby on-Rails

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

Solution 7 - Ruby on-Rails

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."

Solution 8 - Ruby on-Rails

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.

Solution 9 - Ruby on-Rails

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

Solution 10 - Ruby on-Rails

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionreadonlyView Question on Stackoverflow
Solution 1 - Ruby on-RailsjrdiokoView Answer on Stackoverflow
Solution 2 - Ruby on-RailstheIVView Answer on Stackoverflow
Solution 3 - Ruby on-RailsDeepak MahakaleView Answer on Stackoverflow
Solution 4 - Ruby on-RailsgerrysterView Answer on Stackoverflow
Solution 5 - Ruby on-RailsSairamView Answer on Stackoverflow
Solution 6 - Ruby on-RailsForamView Answer on Stackoverflow
Solution 7 - Ruby on-RailsniborgView Answer on Stackoverflow
Solution 8 - Ruby on-RailsJohn LockwoodView Answer on Stackoverflow
Solution 9 - Ruby on-Railswondersz1View Answer on Stackoverflow
Solution 10 - Ruby on-Railspuneet18View Answer on Stackoverflow