Note that there are some explanatory texts on larger screens.

plurals
  1. POefficient bulk update rails database
    primarykey
    data
    text
    <p>I'm trying to build a rake utility that will update my database every so often.</p> <p>This is the code I have so far:</p> <pre><code>namespace :utils do # utils:update_ip # Downloads the file frim &lt;url&gt; to the temp folder then unzips it in &lt;file_path&gt; # Then updates the database. desc "Update ip-to-country database" task :update_ip =&gt; :environment do require 'open-uri' require 'zip/zipfilesystem' require 'csv' file_name = "ip-to-country.csv" file_path = "#{RAILS_ROOT}/db/" + file_name url = 'http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip' #check last time we updated the database. mod_time = '' mod_time = File.new(file_path).mtime.httpdate if File.exists? file_path begin puts 'Downloading update...' #send conditional GET to server zipped_file = open(url, {'If-Modified-Since' =&gt; mod_time}) rescue OpenURI::HTTPError =&gt; the_error if the_error.io.status[0] == '304' puts 'Nothing to update.' else puts 'HTTPError: ' + the_error.message end else # file was downloaded without error. Rails.logger.info 'ip-to-coutry: Remote database was last updated: ' + zipped_file.meta['last-modified'] delay = Time.now - zipped_file.last_modified Rails.logger.info "ip-to-country: Database was outdated for: #{delay} seconds (#{delay / 60 / 60 / 24 } days)" puts 'Unzipping...' File.delete(file_path) if File.exists? file_path Zip::ZipFile.open(zipped_file.path) do |zipfile| zipfile.extract(file_name, file_path) end Iptocs.delete_all puts "Importing new database..." # TODO: way, way too heavy find a better solution. CSV.open(file_path, 'r') do |row| ip = Iptocs.new( :ip_from =&gt; row.shift, :ip_to =&gt; row.shift, :country_code2 =&gt; row.shift, :country_code3 =&gt; row.shift, :country_name =&gt; row.shift) ip.save end #CSV puts "Complete." end #begin-resuce end #task end #namespace </code></pre> <p>The problem I'm having is that this takes a few minutes to enter the 100 thousand plus entries. I'd like to find a more efficient way of updating my database. Ideally this will remain independent of the database type, but if not my production server will be running on MySQL.</p> <p>Thank you for any insight.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload