]> source.dussan.org Git - redmine.git/commitdiff
When creating more than two identical attachments in a single transaction, the first...
authorGo MAEDA <maeda@farend.jp>
Mon, 28 Sep 2020 14:25:35 +0000 (14:25 +0000)
committerGo MAEDA <maeda@farend.jp>
Mon, 28 Sep 2020 14:25:35 +0000 (14:25 +0000)
Patch by Jens Krämer.

git-svn-id: http://svn.redmine.org/redmine/trunk@20094 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/attachment.rb
test/unit/attachment_test.rb

index b0cc94cb26381554592ab9771c16179e90dd7131..112232c6402076c17e227574947640e6cae050a8 100644 (file)
@@ -470,7 +470,8 @@ class Attachment < ActiveRecord::Base
                       .where(digest: self.digest, filesize: self.filesize)
                       .where('id <> ? and disk_filename <> ?',
                              self.id, self.disk_filename)
-                      .first
+                      .order(:id)
+                      .last
         existing.with_lock do
           original_diskfile = self.diskfile
           existing_diskfile = existing.diskfile
index 0b3ac06bfeb88aa6d200f1a5b92bffe21744a1f2..90d0dc6794f928daf87a1eb4deb043970259efe1 100644 (file)
@@ -235,6 +235,23 @@ class AttachmentTest < ActiveSupport::TestCase
     assert_not_equal a1.diskfile, a2.diskfile
   end
 
+  def test_identical_attachments_created_in_same_transaction_should_not_end_up_unreadable
+    attachments = []
+    Project.transaction do
+      3.times do
+        a = Attachment.create!(
+          :container => Issue.find(1), :author => User.find(1),
+          :file => mock_file(:filename => 'foo', :content => 'abcde')
+        )
+        attachments << a
+      end
+    end
+    attachments.each do |a|
+      assert a.readable?
+    end
+    assert_equal 1, attachments.map(&:diskfile).uniq.size
+  end
+
   def test_filename_should_be_basenamed
     a = Attachment.new(:file => mock_file(:original_filename => "path/to/the/file"))
     assert_equal 'file', a.filename