Commit e780b4e7 by Emanuel Evans

Don't try to add to null transactions

Fixes https://github.com/rubysherpas/paranoia/issues/274

It looks like ActiveRecord refactored the Transaction class hierarchy in
a non-backwards-compatible
way (https://github.com/rails/rails/pull/16363), which breaks
`add_to_transaction` for null transactions (I think it probably
qualifies as a bug). The workaround is to not try to use
`add_to_transaction` in null transactions.
parent 3502f452
...@@ -93,7 +93,7 @@ module Paranoia ...@@ -93,7 +93,7 @@ module Paranoia
if persisted? if persisted?
# if a transaction exists, add the record so that after_commit # if a transaction exists, add the record so that after_commit
# callbacks can be run # callbacks can be run
add_to_transaction add_to_transaction unless self.class.connection.current_transaction.closed?
update_columns(paranoia_destroy_attributes) update_columns(paranoia_destroy_attributes)
elsif !frozen? elsif !frozen?
assign_attributes(paranoia_destroy_attributes) assign_attributes(paranoia_destroy_attributes)
......
...@@ -88,6 +88,14 @@ class ParanoiaTest < test_framework ...@@ -88,6 +88,14 @@ class ParanoiaTest < test_framework
assert_equal to_param, model.to_param assert_equal to_param, model.to_param
end end
def test_paranoid_model_delete_outside_transaction
model = ParanoidModel.new
model.save!
model.delete
assert model.to_param
end
def test_destroy_behavior_for_plain_models def test_destroy_behavior_for_plain_models
model = PlainModel.new model = PlainModel.new
assert_equal 0, model.class.count assert_equal 0, model.class.count
...@@ -762,7 +770,7 @@ class ParanoiaTest < test_framework ...@@ -762,7 +770,7 @@ class ParanoiaTest < test_framework
parent1 = ParentModel.create parent1 = ParentModel.create
pt1 = ParanoidModelWithTimestamp.create(:parent_model => parent1) pt1 = ParanoidModelWithTimestamp.create(:parent_model => parent1)
ParanoidModelWithTimestamp.record_timestamps = false ParanoidModelWithTimestamp.record_timestamps = false
pt1.update_columns(created_at: 20.years.ago, updated_at: 10.years.ago, deleted_at: 10.years.ago) pt1.update_columns(created_at: 20.years.ago, updated_at: 10.years.ago, deleted_at: 10.years.ago)
ParanoidModelWithTimestamp.record_timestamps = true ParanoidModelWithTimestamp.record_timestamps = true
assert pt1.updated_at < 10.minutes.ago assert pt1.updated_at < 10.minutes.ago
refute pt1.deleted_at.nil? refute pt1.deleted_at.nil?
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment