diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-01-20 11:04:22 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-01-20 11:04:22 -0800 |
commit | a1c0e51a2c99ec88fba59824b59f32f0d34a0aae (patch) | |
tree | d6d4cfd43d691f13f1e8eaf1f55f835f15e4a594 | |
parent | 5503796096eb0edbe1ed554a6c8d3fdc98314231 (diff) | |
parent | bbcebb6ab276c71be88c3ae0548bb7bf4e18630d (diff) | |
download | rails-a1c0e51a2c99ec88fba59824b59f32f0d34a0aae.tar.gz rails-a1c0e51a2c99ec88fba59824b59f32f0d34a0aae.tar.bz2 rails-a1c0e51a2c99ec88fba59824b59f32f0d34a0aae.zip |
Merge pull request #9006 from wangjohn/activerecord_transaction_state
Created state for a transaction and added tests.
-rw-r--r-- | activerecord/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/transaction.rb | 12 | ||||
-rw-r--r-- | activerecord/lib/active_record/core.rb | 23 | ||||
-rw-r--r-- | activerecord/test/cases/transactions_test.rb | 20 |
4 files changed, 49 insertions, 11 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 45a2b59d47..f1cca0ad76 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,10 @@ ## Rails 4.0.0 (unreleased) ## +* Added a state instance variable to each transaction. Will allow other objects + to know whether a transaction has been committed or rolled back. + + *John Wang* + * Collection associations `#empty?` always respects builded records. Fix #8879. diff --git a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb index 4cca94e40b..2b8026dbf9 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb @@ -5,7 +5,17 @@ module ActiveRecord def initialize(connection) @connection = connection + @state = nil end + + def committed? + @state == :commit + end + + def rolledback? + @state == :rollback + end + end class ClosedTransaction < Transaction #:nodoc: @@ -91,6 +101,7 @@ module ActiveRecord end def rollback_records + @state = :rollback records.uniq.each do |record| begin record.rolledback!(parent.closed?) @@ -101,6 +112,7 @@ module ActiveRecord end def commit_records + @state = :commit records.uniq.each do |record| begin record.committed! diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 94c6684700..812f1ce5c5 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -365,17 +365,18 @@ module ActiveRecord pk = self.class.primary_key @attributes[pk] = nil unless @attributes.key?(pk) - @aggregation_cache = {} - @association_cache = {} - @attributes_cache = {} - @previously_changed = {} - @changed_attributes = {} - @readonly = false - @destroyed = false - @marked_for_destruction = false - @new_record = true - @txn = nil - @_start_transaction_state = {} + @aggregation_cache = {} + @association_cache = {} + @attributes_cache = {} + @previously_changed = {} + @changed_attributes = {} + @readonly = false + @destroyed = false + @marked_for_destruction = false + @new_record = true + @txn = nil + @_start_transaction_state = {} + @transaction = nil end end end diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index bcbc48b38a..9d278480ef 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -451,6 +451,26 @@ class TransactionTest < ActiveRecord::TestCase end end + def test_transactions_state_from_rollback + connection = Topic.connection + transaction = ActiveRecord::ConnectionAdapters::ClosedTransaction.new(connection).begin + + assert transaction.open? + transaction.perform_rollback + + assert transaction.rolledback? + end + + def test_transactions_state_from_commit + connection = Topic.connection + transaction = ActiveRecord::ConnectionAdapters::ClosedTransaction.new(connection).begin + + assert transaction.open? + transaction.perform_commit + + assert transaction.committed? + end + private %w(validation save destroy).each do |filter| |