aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/transactions.rb4
-rw-r--r--activerecord/test/cases/transactions_test.rb42
3 files changed, 52 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 4164b928bd..ae4a672ef1 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Keep track of dirty attributes after transaction is rollback.
+
+ Related #13166.
+
+ *Bogdan Gusiev* *arthurnn*
+
* Add support for module-level `table_name_suffix` in models.
This makes `table_name_suffix` work the same way as `table_name_prefix` when
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb
index 17f76b63b3..cf76d53bf7 100644
--- a/activerecord/lib/active_record/transactions.rb
+++ b/activerecord/lib/active_record/transactions.rb
@@ -350,6 +350,7 @@ module ActiveRecord
end
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) + 1
@_start_transaction_state[:frozen?] = @attributes.frozen?
+ @_start_transaction_state[:changed_attributes] ||= changed_attributes
end
# Clear the new record state and id of a record.
@@ -368,6 +369,9 @@ module ActiveRecord
@attributes = @attributes.dup if @attributes.frozen?
@new_record = restore_state[:new_record]
@destroyed = restore_state[:destroyed]
+ changed_attributes.replace(restore_state[:changed_attributes]).delete_if do |attribute, old_value|
+ old_value == @attributes[attribute]
+ end
if restore_state.has_key?(:id)
write_attribute(self.class.primary_key, restore_state[:id])
else
diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb
index e6ed85394b..7f2e830083 100644
--- a/activerecord/test/cases/transactions_test.rb
+++ b/activerecord/test/cases/transactions_test.rb
@@ -274,6 +274,48 @@ class TransactionTest < ActiveRecord::TestCase
end
end
+ def test_rollback_when_changing_inside_transaction
+ assert !@first.approved?
+ Topic.transaction do
+ @first.approved = true
+ @first.save!
+ raise ActiveRecord::Rollback
+ end
+ assert @first.approved
+ assert @first.changes["approved"]
+ @first.save!
+ assert @first.reload.approved
+ end
+
+ def test_rollback_when_changing_outside_transaction
+ assert !@first.approved?
+ @first.approved = true
+ Topic.transaction do
+ @first.save!
+ raise ActiveRecord::Rollback
+ end
+ assert @first.changes["approved"]
+ assert @first.approved
+ @first.save!
+ assert @first.reload.approved
+ end
+
+ def test_rollback_when_changing_back_to_prev_stage
+ assert !@first.approved?
+ Topic.transaction do
+ @first.approved = true
+ @first.save!
+ @first.approved = false
+ @first.save!
+ raise ActiveRecord::Rollback
+ end
+ assert !@first.approved
+ assert !@first.changes["approved"]
+ @first.save!
+ assert !@first.reload.approved
+ end
+
+
def test_force_savepoint_in_nested_transaction
Topic.transaction do
@first.approved = true