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"