Permalink
Browse files

Merge pull request #452 from janko-m/downloads

A Rake task for updating gems' download counts
  • Loading branch information...
2 parents 1202b36 + 582210d commit c93e65c165ef0f9cfec4e00c372c9b8b105bb4a9 @sferik sferik committed Nov 12, 2012
Showing with 53 additions and 0 deletions.
  1. +14 −0 lib/tasks/gemcutter.rake
  2. +39 −0 test/rake/gemcutter_test.rb
@@ -57,6 +57,20 @@ namespace :gemcutter do
end
end
end
+
+ desc "Update the download counts for all gems."
+ task :update_download_counts => :environment do
+ case_query = Rubygem.pluck(:name)
+ .map { |name| "WHEN '#{name}' THEN #{$redis["downloads:rubygem:#{name}"].to_i}" }
+ .join("\n ")
+
+ ActiveRecord::Base.connection.execute <<-SQL.strip_heredoc
+ UPDATE rubygems
+ SET downloads = CASE name
+ #{case_query}
+ END
+ SQL
+ end
end
desc "Move all but the last 2 days of version history to SQL"
@@ -0,0 +1,39 @@
+require "test_helper"
+
+class GemcutterTest < ActiveSupport::TestCase
+ setup do
+ Rake.application.rake_require "lib/tasks/gemcutter", [Rails.root.to_s]
+ Rake::Task.define_task(:environment)
+ end
+
+ def run_rake_task(task_name)
+ Rake::Task["gemcutter:#{task_name}"].reenable
+ Rake.application.invoke_task "gemcutter:#{task_name}"
+ end
+
+ context "rubygems" do
+ setup do
+ @rubygems = create_list(:rubygem_with_downloads, 3, downloads: 0)
+ end
+
+ def update_download_counts
+ run_rake_task("rubygems:update_download_counts")
+ end
+
+ should "update download counts for all gems" do
+ @rubygems.each_with_index do |rubygem, download_count|
+ $redis.incrby "downloads:rubygem:#{rubygem.name}", download_count
+ end
+
+ update_download_counts
+
+ assert_equal 0, @rubygems[0].reload["downloads"]
+ assert_equal 1, @rubygems[1].reload["downloads"]
+ assert_equal 2, @rubygems[2].reload["downloads"]
+ end
+
+ teardown do
+ @rubygems.each(&:destroy)
+ end
+ end
+end

0 comments on commit c93e65c

Please sign in to comment.