rails 5: simplify attachment transaction callbacks
refs CNVS-34862 in rails 5, we can't detect a transaction that was elided in favor of joining a parent transaction, so don't try to. instead, always call attachment callbacks immediately in the transaction, except for the one spec that's actually testing that we don't do it in a transaction Change-Id: Ia53730586e305d95c41269b3be643194392a141d Reviewed-on: https://gerrit.instructure.com/103757 Tested-by: Jenkins Reviewed-by: James Williams <jamesw@instructure.com> Product-Review: Cody Cutrer <cody@instructure.com> QA-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
parent
77b7d0d356
commit
7fe9050e84
|
@ -198,32 +198,6 @@ module AttachmentFu # :nodoc:
|
|||
tmp.close
|
||||
end
|
||||
end
|
||||
|
||||
if Rails.env.test?
|
||||
# thanks to test_after_commit, we can use transactional fixtures,
|
||||
# but we still need reach into connection, since we conditionally
|
||||
# use after_transaction_commit based on transaction nesting
|
||||
if CANVAS_RAILS4_2
|
||||
def open_transactions
|
||||
connection.instance_variable_get(:@test_open_transactions)
|
||||
end
|
||||
else
|
||||
# count the number of transactions since the last joinable transaction
|
||||
def open_transactions
|
||||
stack = connection.transaction_manager.instance_variable_get(:@stack)
|
||||
count = 0
|
||||
stack.reverse.each do |transaction|
|
||||
break unless transaction.joinable?
|
||||
count += 1
|
||||
end
|
||||
count
|
||||
end
|
||||
end
|
||||
else
|
||||
def open_transactions
|
||||
connection.open_transactions
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
|
@ -567,10 +541,10 @@ module AttachmentFu # :nodoc:
|
|||
run_callbacks(:save_and_attachment_processing)
|
||||
end
|
||||
|
||||
if self.class.open_transactions == 1 # yes, == 1, not > 0 ... see comment above
|
||||
self.class.connection.after_transaction_commit(&save_and_callbacks)
|
||||
else
|
||||
if Rails.env.test?
|
||||
save_and_callbacks.call()
|
||||
else
|
||||
self.class.connection.after_transaction_commit(&save_and_callbacks)
|
||||
end
|
||||
else
|
||||
run_callbacks(:save_and_attachment_processing)
|
||||
|
|
|
@ -303,10 +303,11 @@ describe Attachment do
|
|||
end
|
||||
|
||||
it "should delay upload until the #save transaction is committed" do
|
||||
allow(Rails.env).to receive(:test?).and_return(false)
|
||||
@attachment.uploaded_data = default_uploaded_data
|
||||
Attachment.connection.expects(:after_transaction_commit).twice
|
||||
@attachment.expects(:touch_context_if_appropriate).never
|
||||
@attachment.expects(:ensure_media_object).never
|
||||
expect(Attachment.connection).to receive(:after_transaction_commit).twice
|
||||
expect(@attachment).to receive(:touch_context_if_appropriate).never
|
||||
expect(@attachment).to receive(:ensure_media_object).never
|
||||
@attachment.save
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue