Skip to content

Commit

Permalink
Manage transaction rollback
Browse files Browse the repository at this point in the history
  When we try to create new active record object into transaction and meet rollback:
    database row will not be created but uploader has created file in folder and doen't remove it.

  I changed behavior: now we store file only after commit action. This true and for update action in transaction.

  Changes in spec/orm/activerecord_spec.rb:
    - Add tests which covers this flows
    - add cleaning 'public_path' folder.Cause we also works with 'spec/public/uploads' folder.
  • Loading branch information
Alexander Koshelapov committed Jul 26, 2017
1 parent f43c068 commit 4ad26be
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
19 changes: 14 additions & 5 deletions lib/carrierwave/orm/activerecord.rb
Expand Up @@ -52,15 +52,24 @@ def mount_base(column, uploader=nil, options={}, &block)
validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
validates_download_of column if uploader_option(column.to_sym, :validate_download)

after_save :"store_#{column}!"
before_save :"write_#{column}_identifier"
after_commit :"remove_#{column}!", :on => :destroy
after_commit :"mark_remove_#{column}_false", :on => :update

after_save :"store_previous_changes_for_#{column}"
after_commit :"remove_previously_stored_#{column}", :on => :update
after_commit :"after_create_#{column}", :on => :create
after_commit :"after_update_#{column}", :on => :update

class_eval <<-RUBY, __FILE__, __LINE__+1
def after_update_#{column}
store_#{column}!
store_previous_changes_for_#{column}
remove_previously_stored_#{column}
mark_remove_#{column}_false
end
def after_create_#{column}
store_#{column}!
store_previous_changes_for_#{column}
end
def #{column}=(new_file)
column = _mounter(:#{column}).serialization_column
if !(new_file.blank? && __send__(:#{column}).blank?)
Expand Down
49 changes: 48 additions & 1 deletion spec/orm/activerecord_spec.rb
Expand Up @@ -648,6 +648,7 @@ def filename
end

after do
FileUtils.rm_rf(public_path("uploads"))
FileUtils.rm_rf(file_path("uploads"))
end

Expand Down Expand Up @@ -735,6 +736,7 @@ def filename
end

after do
FileUtils.rm_rf(public_path("uploads"))
FileUtils.rm_rf(file_path("uploads"))
end

Expand All @@ -758,14 +760,53 @@ def filename
Event.transaction do
@event.image = stub_file('new.jpeg')
@event.save
expect(File.exist?(public_path('uploads/new.jpeg'))).to be_truthy
expect(File.exist?(public_path('uploads/old.jpeg'))).to be_truthy
raise ActiveRecord::Rollback
end
expect(File.exist?(public_path('uploads/old.jpeg'))).to be_truthy
end
end

describe "#mount_uploader into transaction" do
before do
@uploader.version :thumb
reset_class("Event")
Event.mount_uploader(:image, @uploader)
@event = Event.new
end

after do
FileUtils.rm_rf(public_path("uploads"))
FileUtils.rm_rf(file_path("uploads"))
end

it "should not store file during rollback" do
Event.transaction do
@event.image = stub_file('new.jpeg')
@event.save

raise ActiveRecord::Rollback
end

expect(File.exist?(public_path('uploads/new.jpeg'))).to be_falsey
end

it "should not change file during rollback" do
@event.image = stub_file('old.jpeg')
@event.save

Event.transaction do
@event.image = stub_file('new.jpeg')
@event.save

raise ActiveRecord::Rollback
end

expect(File.exist?(public_path('uploads/new.jpeg'))).to be_falsey
expect(File.exist?(public_path('uploads/old.jpeg'))).to be_truthy
end
end

describe '#mount_uploader removing old files with multiple uploaders' do
before do
@uploader = Class.new(CarrierWave::Uploader::Base)
Expand All @@ -783,6 +824,7 @@ def filename
end

after do
FileUtils.rm_rf(public_path("uploads"))
FileUtils.rm_rf(file_path("uploads"))
end

Expand Down Expand Up @@ -826,6 +868,7 @@ def filename
end

after do
FileUtils.rm_rf(public_path("uploads"))
FileUtils.rm_rf(file_path("uploads"))
end

Expand Down Expand Up @@ -1367,6 +1410,7 @@ def filename
end

after do
FileUtils.rm_rf(public_path("uploads"))
FileUtils.rm_rf(file_path("uploads"))
end

Expand Down Expand Up @@ -1444,6 +1488,7 @@ def filename
end

after do
FileUtils.rm_rf(public_path("uploads"))
FileUtils.rm_rf(file_path("uploads"))
end

Expand Down Expand Up @@ -1481,6 +1526,7 @@ def filename
end

after do
FileUtils.rm_rf(public_path("uploads"))
FileUtils.rm_rf(file_path("uploads"))
end

Expand Down Expand Up @@ -1523,6 +1569,7 @@ def filename
end

after do
FileUtils.rm_rf(public_path("uploads"))
FileUtils.rm_rf(file_path("uploads"))
end

Expand Down

0 comments on commit 4ad26be

Please sign in to comment.