From ae3543872c3415e66fd38dcf519f603d84d1d5c9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9gis=20Hanol?= <regis@hanol.fr>
Date: Mon, 17 Jun 2013 22:16:14 +0200
Subject: [PATCH] renamed the `sha` column to the proper `sha1`

---
 app/models/upload.rb                           | 18 +++++++++---------
 db/migrate/20130617180009_rename_sha_column.rb | 15 +++++++++++++++
 lib/local_store.rb                             |  2 +-
 lib/s3.rb                                      |  4 ++--
 spec/models/upload_spec.rb                     |  2 +-
 5 files changed, 28 insertions(+), 13 deletions(-)
 create mode 100644 db/migrate/20130617180009_rename_sha_column.rb

diff --git a/app/models/upload.rb b/app/models/upload.rb
index 277b5af3e96..869e69fb582 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -38,9 +38,9 @@ class Upload < ActiveRecord::Base
 
   def self.create_for(user_id, file)
     # compute the sha
-    sha = Digest::SHA1.file(file.tempfile).hexdigest
+    sha1 = Digest::SHA1.file(file.tempfile).hexdigest
     # check if the file has already been uploaded
-    upload = Upload.where(sha: sha).first
+    upload = Upload.where(sha1: sha1).first
 
     # otherwise, create it
     if upload.blank?
@@ -53,7 +53,7 @@ class Upload < ActiveRecord::Base
         user_id: user_id,
         original_filename: file.original_filename,
         filesize: File.size(file.tempfile),
-        sha: sha,
+        sha1: sha1,
         width: width,
         height: height,
         url: ""
@@ -61,7 +61,7 @@ class Upload < ActiveRecord::Base
       # make sure we're at the beginning of the file (FastImage is moving the pointer)
       file.rewind
       # store the file and update its url
-      upload.url = Upload.store_file(file, sha, image_info, upload.id)
+    upload.url = Upload.store_file(file, sha1, image_info, upload.id)
       # save the url
       upload.save
     end
@@ -69,9 +69,9 @@ class Upload < ActiveRecord::Base
     upload
   end
 
-  def self.store_file(file, sha, image_info, upload_id)
-    return S3.store_file(file, sha, image_info, upload_id) if SiteSetting.enable_s3_uploads?
-    return LocalStore.store_file(file, sha, image_info, upload_id)
+  def self.store_file(file, sha1, image_info, upload_id)
+    return S3.store_file(file, sha1, image_info, upload_id) if SiteSetting.enable_s3_uploads?
+    return LocalStore.store_file(file, sha1, image_info, upload_id)
   end
 
   def self.uploaded_regex
@@ -105,11 +105,11 @@ end
 #  url               :string(255)      not null
 #  created_at        :datetime         not null
 #  updated_at        :datetime         not null
-#  sha               :string(255)
+#  sha1              :string(40)
 #
 # Indexes
 #
-#  index_uploads_on_sha      (sha) UNIQUE
+#  index_uploads_on_sha1     (sha1) UNIQUE
 #  index_uploads_on_user_id  (user_id)
 #
 
diff --git a/db/migrate/20130617180009_rename_sha_column.rb b/db/migrate/20130617180009_rename_sha_column.rb
new file mode 100644
index 00000000000..c621a485deb
--- /dev/null
+++ b/db/migrate/20130617180009_rename_sha_column.rb
@@ -0,0 +1,15 @@
+class RenameShaColumn < ActiveRecord::Migration
+  def up
+    remove_index :uploads, :sha
+    rename_column :uploads, :sha, :sha1
+    change_column :uploads, :sha1, :string, limit: 40
+    add_index :uploads, :sha1, unique: true
+  end
+
+  def down
+    remove_index :uploads, :sha1
+    change_column :uploads, :sha1, :string, limit: 255
+    rename_column :uploads, :sha1, :sha
+    add_index :uploads, :sha, unique: true
+  end
+end
diff --git a/lib/local_store.rb b/lib/local_store.rb
index 928d38dc02f..11ae1a6ea8d 100644
--- a/lib/local_store.rb
+++ b/lib/local_store.rb
@@ -1,6 +1,6 @@
 module LocalStore
 
-  def self.store_file(file, sha, image_info, upload_id)
+  def self.store_file(file, sha1, image_info, upload_id)
     clean_name = Digest::SHA1.hexdigest("#{Time.now.to_s}#{file.original_filename}")[0,16] + ".#{image_info.type}"
     url_root = "/uploads/#{RailsMultisite::ConnectionManagement.current_db}/#{upload_id}"
     path = "#{Rails.root}/public#{url_root}"
diff --git a/lib/s3.rb b/lib/s3.rb
index 5df38f0de86..7ef07e0e63e 100644
--- a/lib/s3.rb
+++ b/lib/s3.rb
@@ -1,13 +1,13 @@
 module S3
 
-  def self.store_file(file, sha, image_info, upload_id)
+  def self.store_file(file, sha1, image_info, upload_id)
     raise Discourse::SiteSettingMissing.new("s3_upload_bucket")     if SiteSetting.s3_upload_bucket.blank?
     raise Discourse::SiteSettingMissing.new("s3_access_key_id")     if SiteSetting.s3_access_key_id.blank?
     raise Discourse::SiteSettingMissing.new("s3_secret_access_key") if SiteSetting.s3_secret_access_key.blank?
 
     @fog_loaded = require 'fog' unless @fog_loaded
 
-    remote_filename = "#{upload_id}#{sha}.#{image_info.type}"
+    remote_filename = "#{upload_id}#{sha1}.#{image_info.type}"
 
     options = S3.generate_options
     directory = S3.get_or_create_directory(SiteSetting.s3_upload_bucket, options)
diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb
index d20464a539d..68d47f72089 100644
--- a/spec/models/upload_spec.rb
+++ b/spec/models/upload_spec.rb
@@ -34,7 +34,7 @@ describe Upload do
         upload.user_id.should == user_id
         upload.original_filename.should == logo.original_filename
         upload.filesize.should == File.size(logo.tempfile)
-        upload.sha.should == Digest::SHA1.file(logo.tempfile).hexdigest
+        upload.sha1.should == Digest::SHA1.file(logo.tempfile).hexdigest
         upload.width.should == 244
         upload.height.should == 66
         upload.url.should == url