Last active
April 15, 2020 21:54
-
-
Save scotchi/0823048d923ab506db858dd5e90a5dc9 to your computer and use it in GitHub Desktop.
Convert Wordpress tables to UTF-8 (when they already have UTF-8 data stored as Latin1)
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 'mysql2' | |
DATABASE, PREFIX = ARGV | |
client = Mysql2::Client.new(username: 'root', database: DATABASE) | |
tables = client.query("select table_name from information_schema.tables " + | |
"where table_schema='#{DATABASE}' and table_name like '#{PREFIX}%'", | |
as: :array).map(&:first) | |
tables.each do |table| | |
puts table | |
columns = client.query("select column_name from information_schema.columns " + | |
"where table_schema='#{DATABASE}' and table_name = '#{table}' " + | |
"and character_set_name != 'utf8'", | |
as: :array).map(&:first); | |
client.query("alter table #{table} convert to character set utf8") | |
updates = columns.map do |c| | |
"#{c} = convert(cast(convert(#{c} using latin1) as binary) using utf8)" | |
end.join(', ') | |
next if updates.empty? | |
puts "\t" + columns.join(', ') | |
client.query("update #{table} set #{updates}"); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for the script.
I was getting:
After some googling, I had it fixed by adding:
on top of the script (after client was created).