Skip to content

Instantly share code, notes, and snippets.

@jschoolcraft
Created May 12, 2009 17:06
Show Gist options
  • Save jschoolcraft/110604 to your computer and use it in GitHub Desktop.
Save jschoolcraft/110604 to your computer and use it in GitHub Desktop.
#!/opt/local/bin/ruby
# make sure that ^^^ points to your ruby
def seen_db?(db)
@seen_dbs ||= []
seen_it?(db, @seen_dbs)
end
def seen_table?(table)
@seen_tables ||= []
seen_it?(table, @seen_tables)
end
def seen_it?(item, seen)
unless seen.include?(item)
seen << item
false
else
true
end
end
# you'll want to replace this with your mysql binary
mysql = "/opt/local/bin/mysql"
dtcs = %x{#{mysql} -Bse "select table_schema, table_name, column_name from information_schema.columns;"}
dtcs.each do |row|
db,table,column = row.split(/\s+/, 3)
db.chomp!
table.chomp!
column.chomp!
db_command = %Q{#{mysql} -Bse "ALTER DATABASE #{db} CHARACTER SET utf8"}
puts "executing: #{db_command}" unless seen_db?(db)
puts %x{#{db_command}} unless seen_db?(db)
unless seen_table?("#{db}-#{table}")
table_command = %Q{#{mysql} -Bse "ALTER TABLE #{db}.#{table} CHARACTER SET utf8"}
puts "executing: #{table_command}"
puts %x{#{table_command}}
convert_command = %Q{#{mysql} -Bse "ALTER TABLE #{db}.#{table} CONVERT TO CHARACTER SET utf8"}
puts "executing: #{convert_command}"
puts %x{#{convert_command}}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment