1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Created state for a transaction and added tests.

This commit is contained in:
wangjohn 2013-01-20 12:40:36 -05:00
parent f48a16bdb2
commit 0355962c27
3 changed files with 45 additions and 11 deletions

View file

@ -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!

View file

@ -365,17 +365,19 @@ 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
@reflects_transaction_state = false
end
end
end

View file

@ -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|