Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Add Comment
Please, Sign In to add comment