Skip to content

Instantly share code, notes, and snippets.

@duncanbeevers
Created February 22, 2010 23:49
Show Gist options
  • Save duncanbeevers/311675 to your computer and use it in GitHub Desktop.
Save duncanbeevers/311675 to your computer and use it in GitHub Desktop.
Check whether migrations need to be run without requiring the whole app
namespace :db do
task :uptodate do
if pending_migrations.blank?
puts "Migrations are up-to-date"
else
puts "The following migrations are pending:"
puts pending_migrations.map { |mp| "%s\t\t%s" % [ mp.version, mp.name ] }.join("\n")
end
end
# Only run this from a single host
task :migrate_unless_uptodate do
Rake::Task['db:migrate'].invoke unless pending_migrations.blank?
end
def pending_migrations
return @pending_migrations if @pending_migrations
require 'active_record'
ActiveRecord::Base.configurations = YAML.load_file('config/database.yml')
ActiveRecord::Base.establish_connection
@pending_migrations = ActiveRecord::Migrator.new(:up, 'db/migrate').pending_migrations
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment