diff options
author | Arthur Neves <arthurnn@gmail.com> | 2014-07-28 13:25:19 -0400 |
---|---|---|
committer | Arthur Neves <arthurnn@gmail.com> | 2014-07-28 13:51:59 -0400 |
commit | d37bcc1d5a781687384fbe632a1850ab218ccbfd (patch) | |
tree | 98013f3ab9cb41f6903f6c1b8ac0b1c7de718969 /activerecord | |
parent | 97bb76dc288d998a684b17a09d79708e2e4b584a (diff) | |
download | rails-d37bcc1d5a781687384fbe632a1850ab218ccbfd.tar.gz rails-d37bcc1d5a781687384fbe632a1850ab218ccbfd.tar.bz2 rails-d37bcc1d5a781687384fbe632a1850ab218ccbfd.zip |
savepoint_name should return nil for non-savepoint transactions
Also add test to assets the savepoint name
Diffstat (limited to 'activerecord')
3 files changed, 27 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb index 7618d6902d..54f873a2a2 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb @@ -69,6 +69,10 @@ module ActiveRecord def state @state end + + def savepoint_name + nil + end end class TransactionState @@ -233,7 +237,8 @@ module ActiveRecord super - @savepoint_name = "active_record_#{number}" + # Savepoint name only counts the Savepoint transactions, so we need to subtract 1 + @savepoint_name = "active_record_#{number - 1}" connection.create_savepoint(@savepoint_name) end diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index c31726437f..99c728814a 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -358,9 +358,7 @@ module ActiveRecord end def current_savepoint_name - if current_transaction.is_a? SavepointTransaction - current_transaction.savepoint_name - end + current_transaction.savepoint_name end # Check the connection back in to the connection pool diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index de1f624191..f28a7b00e2 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -424,6 +424,26 @@ class TransactionTest < ActiveRecord::TestCase end end + def test_savepoints_name + Topic.transaction do + assert_nil Topic.connection.current_savepoint_name + assert_nil Topic.connection.current_transaction.savepoint_name + + Topic.transaction(requires_new: true) do + assert_equal "active_record_1", Topic.connection.current_savepoint_name + assert_equal "active_record_1", Topic.connection.current_transaction.savepoint_name + + Topic.transaction(requires_new: true) do + assert_equal "active_record_2", Topic.connection.current_savepoint_name + assert_equal "active_record_2", Topic.connection.current_transaction.savepoint_name + end + + assert_equal "active_record_1", Topic.connection.current_savepoint_name + assert_equal "active_record_1", Topic.connection.current_transaction.savepoint_name + end + end + end + def test_rollback_when_commit_raises Topic.connection.expects(:begin_db_transaction) Topic.connection.expects(:commit_db_transaction).raises('OH NOES') |