Skip to content

Instantly share code, notes, and snippets.

@imownbey
Created January 10, 2009 22:25
Show Gist options
  • Save imownbey/45573 to your computer and use it in GitHub Desktop.
Save imownbey/45573 to your computer and use it in GitHub Desktop.
# From http://blog.jayfields.com/2008/02/rake-task-overwriting.html
class Rake::Task
def abandon
@actions.clear
end
end
OLD_MIGRATION = Rake::Task["db:migrate"]
Rake::Task["db:migrate"].abandon
OLD_REDO = Rake::Task["db:migrate:redo"]
Rake::Task["db:migrate:redo"].abandon
OLD_DOWN = Rake::Task["db:migrate:down"]
Rake::Task["db:migrate:redo"].abandon
OLD_UP = Rake::Task["db:migrate:up"]
Rake::Task["db:migrate:up"].abandon
namespace :db do
task :migrate do
OLD_MIGRATION.invoke
end
namespace :migrate do
task :redo do
ENV['VERSION'] = parse_version(ENV['VERSION'])
OLD_REDO.invoke
end
task :down do
ENV['VERSION'] = parse_version(ENV['VERSION'])
OLD_DOWN.invoke
end
task :up do
ENV['VERSION'] = parse_version(ENV['VERSION'])
OLD_UP.invoke
end
end
end
def parse_version(version)
# If we get passed a VERSION variable, and it is not a number
# we look to see if a filename matches and reset based on that
# number
if version =~ /[^0-9]/
# We have a string!
files = Dir.glob(File.join(Rails.root, 'db', 'migrate', '*.rb')).select {|filename| filename =~ Regexp.new(version)}
if files.length > 1
files.sort!
puts "That version matches a number of files, please pick one:"
files.each_with_index do |filename, key|
puts "#{key + 1}. #{File.basename(filename)}"
end
print "Which version do you want to migrate? "
number = STDIN.gets
file = files[number.chomp.to_i - 1]
elsif files.length == 0
puts "No files matched that string"
exit
else
file = files.first
end
File.basename(file).match(/^(\d+)_/)[1]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment