Created
July 30, 2013 13:57
-
-
Save njvack/6113127 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'mysql2' | |
Mysql2::Client.default_query_options.merge!(:as => :array) | |
conn = Mysql2::Client.new(:host => 'localhost', :username => 'user', | |
:password => 'pw', :database => 'db') | |
tables = conn.query("SHOW TABLES").map {|row| row[0] } | |
# See http://dev.mysql.com/doc/refman/5.0/en/charset-column.html | |
# One might want to include enum and set columns, but I don't | |
TYPES_TO_CONVERT = %w(char varchar text) | |
tables.each do |table| | |
puts "converting #{table}" | |
# Get all the columns and we'll filter for the ones we want | |
columns = conn.query("DESCRIBE #{table}") | |
columns_to_convert = columns.find_all {|row| | |
TYPES_TO_CONVERT.include? row[1].gsub(/\(\d+\)/, '') | |
}.map {|row| row[0]} | |
next if columns_to_convert.empty? | |
query = "UPDATE `#{table}` SET " | |
query += columns_to_convert.map {|col| | |
"`#{col}` = convert(cast(convert(`#{col}` using latin1) as binary) using utf8)" | |
}.join ", " | |
puts query | |
conn.query query | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I adjusted your script with some additions:
https://gist.github.com/pobegov/f8b293fb6eb658a13feb1c318e6c07ed