Created
May 31, 2010 13:27
-
-
Save pedromtavares/419836 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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