Show pending migrations in rails

前端 未结 10 1010
北海茫月
北海茫月 2020-12-22 22:25

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

相关标签:
10条回答
  • 2020-12-22 22:34

    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

    0 讨论(0)
  • 2020-12-22 22:37

    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
    
    0 讨论(0)
  • 2020-12-22 22:41

    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

    0 讨论(0)
  • 2020-12-22 22:43

    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.
    
    0 讨论(0)
  • 2020-12-22 22:43

    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.

    0 讨论(0)
  • 2020-12-22 22:44

    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."
    
    0 讨论(0)
提交回复
热议问题