Skip to content

Instantly share code, notes, and snippets.

@jeremyf
Created May 8, 2012 18:34
Show Gist options
  • Save jeremyf/2638310 to your computer and use it in GitHub Desktop.
Save jeremyf/2638310 to your computer and use it in GitHub Desktop.
Converting latin1 to UTF8 the hard way
ActiveRecord::Base.connection.select_all(%(show table status where collation like '%latin1%')).each do |options|
table_name = options['Name']
puts "Started Processing\t#{table_name}"
FileUtils.mkdir_p(Rails.root.join('tmp/db-conversion'))
filename_pre_transform = Rails.root.join("tmp/db-conversion/conductor.#{table_name}.pre.sql").to_s
filename_post_transform = Rails.root.join("tmp/db-conversion/conductor.#{table_name}.post.sql").to_s
commands = []
commands << %(mysqldump -uroot --opt --skip-set-charset --default-character-set=latin1 --skip-extended-insert conductor_dev --tables #{table_name} > #{filename_pre_transform})
commands << %(perl -i -pe 's/DEFAULT CHARSET=latin1/DEFAULT CHARSET=utf8/' #{filename_pre_transform})
commands << %(iconv -f LATIN1 -t UTF8 #{filename_pre_transform} > #{filename_post_transform})
commands << %(mysql -uroot conductor_dev < #{filename_post_transform})
system(commands.join(';'))
puts "Finished Processing\t#{table_name}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment