daily pastebin goal
61%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env ruby
  2.  
  3. require 'mysql2'
  4.  
  5. DATABASE, PREFIX = ARGV
  6.  
  7. client = Mysql2::Client.new(username: 'root', database: DATABASE)
  8. tables = client.query("select table_name from information_schema.tables " +
  9.                       "where table_schema='#{DATABASE}' and table_name like '#{PREFIX}%'",
  10.                       as: :array).map(&:first)
  11. tables.each do |table|
  12.   puts table
  13.   columns = client.query("select column_name from information_schema.columns " +
  14.                          "where table_schema='#{DATABASE}' and table_name = '#{table}' " +
  15.                          "and character_set_name != 'utf8'",
  16.                          as: :array).map(&:first);
  17.  
  18.   client.query("alter table #{table} convert to character set utf8")
  19.  
  20.   updates = columns.map do |c|
  21.     "#{c} = convert(cast(convert(#{c} using latin1) as binary) using utf8)"
  22.   end.join(', ')
  23.  
  24.   next if updates.empty?
  25.  
  26.   puts "\t" + columns.join(', ')
  27.  
  28.   client.query("update #{table} set #{updates}");
  29. end
RAW Paste Data
Top