Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'yaml'
- require 'rake'
- require 'rbconfig'
- require 'fileutils'
- LIMIT_FILESIZE = 1000000000
- def os
- @os ||= (
- host_os = RbConfig::CONFIG['host_os']
- case host_os
- when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
- :windows
- when /darwin|mac os/
- :macosx
- when /linux/
- :linux
- when /solaris|bsd/
- :unix
- else
- raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
- end
- )
- end
- def file_count(file_size)
- return 1 if file_size < LIMIT_FILESIZE
- 4.step(Float::INFINITY, 4) do |i|
- next if (file_size / i) > LIMIT_FILESIZE
- return i
- end
- end
- TABLES = %w[table_names].freeze
- config = YAML.load_file('config/database.yml')['production']
- conf_file = File.dirname(__FILE__) + '/tmp/my.conf'
- outputs_dir = File.dirname(__FILE__) + '/txts/outputs'
- splits_dir = File.dirname(__FILE__) + '/txts/splits'
- FileUtils.rm(Dir.glob(outputs_dir + '/*'))
- FileUtils.rm(Dir.glob(splits_dir + '/*'))
- File.open(conf_file, 'w') do |f|
- f.puts "[client]"
- f.puts "user=#{config['username']}"
- f.puts "password=#{config['password']}"
- end
- TABLES.each do |table|
- sh "mysql --defaults-file=tmp/my.conf -h\"#{config['host']}\" -D \"#{config['database']}\" -e \"select * from #{table}\" -s -N > txts/outputs/#{table}.txt", verbose: false
- file_size = File.size("txts/outputs/#{table}.txt")
- rows = `wc -l txts/outputs/#{table}.txt`.match(/\s*(\d*)/)[1].to_i
- @file_count = file_count(file_size)
- if os == :macosx
- # MAC OSでは、`-d`オプションが利用できない。使いたい場合は、brew install coreutils
- sh "gsplit -d -l #{(rows / @file_count) + 4} txts/outputs/#{table}.txt txts/splits/#{table}.txt. &>/dev/null", verbose: false
- else
- sh "split -d -l #{(rows / @file_count) + 4} txts/outputs/#{table}.txt txts/splits/#{table}.txt. &>/dev/null", verbose: false
- end
- end
- FileUtils.rm(conf_file)
Add Comment
Please, Sign In to add comment