Monday, December 21, 2009

Converting Rails Numeric Migrations to Time Stamp

I looked on the web and couldn't find a good script to rename numeric migrations to timestamp ones when upgrading an application to a more recent rails (in this case so I can make it an engine).

files = Dir['./db/migrate/*.rb'].sort
now = Time.now.utc
time = Time.utc(now.year, now.month, now.day)
files.each do |full_file|
file = File.basename(full_file)
parts = file.split('_')
raise "probably illegal file name #{file}" if parts.size < 2
if parts[0].size < 7
cmd = "svn move #{full_file} #{full_file.gsub(parts[0], time.to_i.to_s)}"
puts cmd
system cmd
time += 1
end
end