From f94e4ffdcb7c5cea78169a88fbf39d23633acb47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Sat, 15 Jun 2013 11:29:20 +0200 Subject: [PATCH] added 'uploads:backfill_shas' rake task --- lib/tasks/uploads.rake | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 lib/tasks/uploads.rake diff --git a/lib/tasks/uploads.rake b/lib/tasks/uploads.rake new file mode 100644 index 000000000..51cd613f1 --- /dev/null +++ b/lib/tasks/uploads.rake @@ -0,0 +1,20 @@ +require "digest/sha1" + +task "uploads:backfill_shas" => :environment do + RailsMultisite::ConnectionManagement.each_connection do |db| + puts "Backfilling #{db}" + Upload.select([:id, :sha, :url]).find_each do |u| + if u.sha.nil? + putc "." + path = "#{Rails.root}/public/#{u.url}" + sha = Digest::SHA1.file(path).hexdigest + begin + Upload.update_all ["sha = ?", sha], ["id = ?", u.id] + rescue ActiveRecord::RecordNotUnique + # not a big deal if we've got a few duplicates + end + end + end + end + puts "done" +end