This blog has been archived. Our writing has moved to makandra cards.
The blog of , a Ruby on Rails development team

How to warn of pending migrations when deploying with Capistrano

We use Capistrano for automatic deployment. A common problem is that you don't remember whether your current release includes migrations and you therefore have to run cap deploy:migrate to update your remote database after deployment. A small piece of code warns us if there are pending migrations:

namespace :db do

  desc "Warns if there are pending migrations"
  task :warn_if_pending_migrations => :environment do
    if defined? ActiveRecord
      pending_migrations = ActiveRecord::Migrator.new(:up, 'db/migrate').pending_migrations

      puts ""
      puts " * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
      puts " *                                                     *"
      if pending_migrations.any?
        puts " * You have #{pending_migrations.size} pending migrations:"
        pending_migrations.each do |pending_migration|
          puts ' * %4d %s' % [pending_migration.version, pending_migration.name]
        end
        puts " *"
        puts " * Run cap deploy:migrate!"
      else
        puts " *               No pending migrations.                *"
      end
      puts " *                                                     *"
      puts " * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
      puts ""

    end
  end

end

All you need to do is put this snippet into lib/tasks/pending_migrations.rb and add this line end the end of config/deploy.rb in your project:

after "deploy", "db:warn_if_pending_migrations"
Growing Rails Applications in Practice
Check out our e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Recent posts

Our address:
makandra GmbH
Werner-von-Siemens-Str. 6
86159 Augsburg
Germany
Contact us:
+49 821 58866 180
info@makandra.de
Commercial register court:
Augsburg Municipal Court
Register number:
HRB 24202
Sales tax identification number:
DE243555898
Chief executive officers:
Henning Koch
Thomas Eisenbarth