From b68df0b24dd3313395fa8ce0224246eb29438bdf Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Sun, 26 Jul 2020 16:03:23 +0800 Subject: [PATCH] fix creation of multiple identical attachments in outer transaction --- app/models/attachment.rb | 3 ++- test/unit/attachment_test.rb | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/models/attachment.rb b/app/models/attachment.rb index b0cc94cb2..112232c64 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -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 diff --git a/test/unit/attachment_test.rb b/test/unit/attachment_test.rb index 0b3ac06bf..90d0dc679 100644 --- a/test/unit/attachment_test.rb +++ b/test/unit/attachment_test.rb @@ -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 -- 2.20.1