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."
To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.
You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.
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.
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.
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
rake db:version
will accomplish this on Rails 2.
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."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With