Skip to content

Instantly share code, notes, and snippets.

@dataday
Created July 11, 2017 17:30
Show Gist options
  • Save dataday/9da12306f47957264d0b0fcacad07de6 to your computer and use it in GitHub Desktop.
Save dataday/9da12306f47957264d0b0fcacad07de6 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'mysql2'
require 'base64'
begin
connection = {
host: 'localhost',
username: 'username',
password: 'password',
database: 'database'
}
client = Mysql2::Client.new(connection)
client.query('show tables').each do |result|
table = result['tabel_name']
puts "Inspecting #{connection[:database]}.#{table}'\n\n"
client.query("select * from #{table}", symbolize_keys: true).each do |row|
key_length = row.keys.max_by(&:length).length
default_line_format = "%-#{key_length}s = %s\n"
binary_line_format = "%-#{key_length}s = %s\n"
puts "#{connection[:database]}.#{table}.#{row.first[1]}\n"
row.each_pair do |key, value|
value_encoding = value.encoding rescue 'NUMBER'
if value_encoding.to_s == "ASCII-8BIT"
file = "#{Base64.decode64(value)}"
printf "#{default_line_format}" % [key, "<<binary#{"#{file}".length > 0 ? '_file' : '_null'}>>"]
else
printf "#{default_line_format}" % [key, value.nil? || value.to_s.empty? ? 'NULL' : "#{value}"]
end
end
puts "\n"
end
end
rescue Mysql2::Error => e
puts e.errno
puts e.error
ensure
client.close if client
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment