Report abuse


			
#!/usr/bin/ruby
#
# Increments the number in Rails migrations from a given starting index. Run 
# this in the db/migrations directory only. 
#
# Example:
#
# increment_migrations 3 RUN
#
# this would add 1 to to all your migrations from 003_create_XXXXX.rb onwards

case ARGV.size
when 1, 2
  from_index = ARGV[0].to_i
  dir = Dir.new('.')
  file_list = dir.to_a.sort

  file_list.each do |fn|
    if fn =~ /(\d{3})(.*\.rb)/
      index = $1.to_i
      if index >= from_index
        index += 1
        new_name = case index.to_s.length
        when 1
          "00#{index}#{$2}"
        when 2
          "0#{index}#{$2}"
        else
          "#{index}#{$2}"
        end
        puts "Converting '#{fn}' to '#{new_name}'"
        File.rename fn, new_name if ARGV[1] == "RUN"
      else
        puts "Skipping '#{fn}'"
      end
    else
      puts "Ignoring '#{fn}'"
    end
  end
else
  puts "Usage: #{$0}  [RUN]"
  puts "   where"
  puts "      number - migration number to start from (mandatory)"
  puts "      RUN - add to run to make the changes (default does not rename)"
end