Skip to content

Instantly share code, notes, and snippets.

@jinzhu
Created November 9, 2009 03:03
Show Gist options
  • Save jinzhu/229644 to your computer and use it in GitHub Desktop.
Save jinzhu/229644 to your computer and use it in GitHub Desktop.
schema = File.read('/home/jinzhu/GIT/joblet/db/schema.rb')
schema.scan(/(^\s*create_table.*?)(?=\n\s*create_table)/m).map do |x|
content = x[0]
content =~ /create_table\s+"(.*?)"/
filename = Time.now.utc.strftime("%Y%m%d%H%M%S") + '_create_' + $1 + '.rb'
filename = File.join('/home/jinzhu/GIT/joblet/db/migrate',filename)
content = "class Create" + $1.capitalize.gsub(/_(\w)/) { $1.to_s.upcase } + " < ActiveRecord::Migration\n def self.up\n" + content.sub(/\A\s*\n/,'') + "\n end\n\n def self.down\n end\nend"
puts content
f = File.new(filename,'w+')
f << content
f.close
# `vim #{filename} --cmd 'Rinvert' --cmd 'x'`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment