Skip to content

Instantly share code, notes, and snippets.

@edward
Created May 15, 2009 18:36
Show Gist options
  • Save edward/112366 to your computer and use it in GitHub Desktop.
Save edward/112366 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -wKU
# Usage:
# $ cat master\ no\ multivariant_2009-01-13_13.42.23.sql | ack "INSERT INTO .*?orders" | ./restore_note_attributes.rb > update_note_attributes.sql
# $ mysql your_db -u your_user < update_note_attributes.sql
require 'rubygems'
require 'fastercsv'
def extract_note_attributes(note)
#note = "Ok, this is a note.!\n\n\tCOLOURS: This is a colour?\n\tTHINGS: Ok, this is a size?"
note_attributes = {}
note.to_s.scan(/^\t([^\:]+)\:\ (.*)$/) do |matches|
note_attributes[matches[0]] = matches[1].to_s.strip
end
note_attributes # => {"COLOURS"=>"This is a colour?", "THINGS"=>"Ok, this is a size?"}
end
STDIN.each_line(";\n") do |line|
line =~ /(VALUES\s+)\((.+)\)/m
values = $2.parse_csv
order_id = values[0]
note_attributes = extract_note_attributes(values[-1])
puts "UPDATE orders SET note_attributes=#{note_attributes.to_yaml.inspect} WHERE id=#{order_id.to_i}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment