Skip to content

Instantly share code, notes, and snippets.

@pedromtavares
Created May 31, 2010 13:27
Show Gist options
  • Save pedromtavares/419836 to your computer and use it in GitHub Desktop.
Save pedromtavares/419836 to your computer and use it in GitHub Desktop.
desc "Moves all generated paths into different tables depending on their source (human or bots/spiders)."
task :path_moving => :environment do
database = Path.configurations[RAILS_ENV]['database']
filetemp = "tmp/temp.sql"
last_id = nil
spiders = /[B|b]ot|[B|b]aidu|[S|s]pider|pingdom|xenu|sitemaps|cuil|msn|shopwiki|entireweb|tokenizer|ysearch/
File.open(filetemp, 'w') do |file|
Path.paginated_each(:per_page => 200) do |path|
type = path.user_agent =~ spiders ? 'spiders' : 'humans'
file.puts "insert into `#{type}_paths` (#{path.attribute_names.map{|attr| "`#{attr}`"}.join(', ')})
values(#{path.attribute_names.map{|attr| "#{path.send(attr).blank? ? 'NULL' : "'#{path.send(attr).to_s.gsub(/[']/, '\\\\\'')}'"}"}.join(', ')});\n"
last_id = path.id
end
file.puts "delete from paths where id <= #{last_id}"
end
system("mysql -u root #{database} < #{filetemp}")
File.delete(filetemp)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment