public
Created

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#!/usr/bin/env ruby
 
# Simple migration navigator for terminal.
#
# Install
# 1) Throw this code into script/migrator
# 2) chmod +x script/migrator
#
# Use
# script/migrator => show 10 latest migrations, choose one
# script/migrator 15 => show 15 latest migrations
# script/migrator -5 => show 5 oldest migrations
# script/migrator foo => grep all migrations using regexp /foo/
#
# After each command you can:
# — type the # of migration to migrate to it
# — type exit/quit/abort/stop (or press ctrl+c) to exit
 
require 'time'
 
migrations_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'db', 'migrate'))
globbed_migrations = File.join(migrations_dir, "*.rb")
 
migrations = []
 
Dir.glob(globbed_migrations).each do |entry|
stamp, title = File.basename(entry).split('_', 2)
title.chomp!('.rb')
title.gsub!(/_/, ' ')
title.capitalize!
migrations << [stamp, title]
end
 
command = ARGV.join(' ')
 
run_migration = lambda do |stamp|
system("rake db:migrate VERSION=#{stamp}")
end
 
print_results = lambda do |results|
unless results.empty?
puts "\n"
results.each_with_index do |result, i|
created = Time.parse(result[0]).strftime("%b %d, %y")
puts "#{i + 1}. #{result[1]}. (#{created})"
end
puts "\n"
end
end
 
validate_choice = lambda do |results, choice|
unless (1..results.size).include?(choice.to_i)
puts "Exiting."
exit(0)
end
end
 
find = lambda do
results = migrations.select{|el| el[1] =~ Regexp.new(command)}
 
puts "Found #{results.size} migration(s):"
print_results.call(results)
 
unless results.empty?
print "Enter # to migrate to: "
choice = STDIN.gets.to_i
validate_choice.call(results, choice)
puts "Migrating to \"#{results[choice-1][1]}\""
run_migration.call(results[choice-1][0])
end
end
 
first = lambda do |number|
results = migrations.first(number)
 
puts "Showing oldest #{number} migrations:"
print_results.call(results)
 
unless results.empty?
print "Enter # to migrate to: "
choice = STDIN.gets.to_i
validate_choice.call(results, choice)
puts "Migrating to \"#{results[choice-1][1]}\""
run_migration.call(results[choice-1][0])
end
end
 
last = lambda do |number|
results = migrations.last(number)
 
puts "Showing latest #{number} migrations:"
print_results.call(results)
 
unless results.empty?
print "Enter # to migrate to: "
choice = STDIN.gets.to_i
validate_choice.call(results, choice)
puts "Migrating to \"#{results[choice-1][1]}\""
run_migration.call(results[choice-1][0])
end
end
 
case command
when /^\s*$/: last.call(10)
when /^(\d+)$/: last.call($1.to_i)
when /^-(\d+)$/: first.call($1.to_i)
else find.call
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.