Skip to content

Instantly share code, notes, and snippets.

@duncanbeevers
Created February 29, 2012 11:50
Show Gist options
  • Save duncanbeevers/1940309 to your computer and use it in GitHub Desktop.
Save duncanbeevers/1940309 to your computer and use it in GitHub Desktop.
A set of rake tasks to determine whether migrations need to be run without loading the full app environment
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
task :migrate_unless_uptodate do
Rake::Task['db:migrate'].invoke unless pending_migrations.blank?
end
namespace :schema do
task :purge_cached_tables => :environment do
ActiveRecord::Base.connection.purge_cached_tables
end
task :dump => :purge_cached_tables
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