Is there a rake task that shows the pending migrations in a rails app?
Is there a rake task that shows the pending migrations in a rails app?
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.
This works for rails 5.2
ActiveRecord::Base.connection.migration_context.needs_migration?
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
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."
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