Skip to content
This repository
Browse code

Move transaction joinability into the transaction object

  • Loading branch information...
commit 280587588aba6ce13717cd6679e3f2b43d287443 1 parent a6fbddb
Jon Leighton authored September 14, 2012
26  activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -3,8 +3,7 @@ module ConnectionAdapters # :nodoc:
3 3
     module DatabaseStatements
4 4
       def initialize
5 5
         super
6  
-        @transaction_joinable = nil
7  
-        @transaction          = ClosedTransaction.new(self)
  6
+        @transaction = ClosedTransaction.new(self)
8 7
       end
9 8
 
10 9
       # Converts an arel AST to SQL
@@ -173,14 +172,11 @@ def supports_statement_cache?
173 172
       def transaction(options = {})
174 173
         options.assert_valid_keys :requires_new, :joinable
175 174
 
176  
-        last_transaction_joinable = @transaction_joinable
177  
-        @transaction_joinable     = options.fetch(:joinable, true)
178  
-        requires_new              = options[:requires_new] || !last_transaction_joinable
179  
-        transaction_open          = false
  175
+        transaction_open = false
180 176
 
181 177
         begin
182  
-          if @transaction.closed? || requires_new
183  
-            begin_transaction
  178
+          if options[:requires_new] || !current_transaction.joinable?
  179
+            begin_transaction(options)
184 180
             transaction_open = true
185 181
           end
186 182
 
@@ -195,11 +191,9 @@ def transaction(options = {})
195 191
         end
196 192
 
197 193
       ensure
198  
-        @transaction_joinable = last_transaction_joinable
199  
-
200 194
         if outside_transaction?
201 195
           @transaction = ClosedTransaction.new(self)
202  
-        elsif @transaction.open? && transaction_open
  196
+        elsif current_transaction.open? && transaction_open
203 197
           begin
204 198
             commit_transaction
205 199
           rescue Exception
@@ -209,12 +203,18 @@ def transaction(options = {})
209 203
         end
210 204
       end
211 205
 
212  
-      def transaction_state #:nodoc:
  206
+      def current_transaction #:nodoc:
213 207
         @transaction
214 208
       end
215 209
 
216  
-      def begin_transaction #:nodoc:
  210
+      def transaction_open?
  211
+        @transaction.open?
  212
+      end
  213
+
  214
+      def begin_transaction(options = {}) #:nodoc:
217 215
         @transaction = @transaction.begin
  216
+        @transaction.joinable = options.fetch(:joinable, true)
  217
+        @transaction
218 218
       end
219 219
 
220 220
       def commit_transaction #:nodoc:
9  activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
@@ -25,13 +25,20 @@ def open?
25 25
         false
26 26
       end
27 27
 
  28
+      def joinable?
  29
+        false
  30
+      end
  31
+
28 32
       # This is a noop when there are no open transactions
29 33
       def add_record(record)
30 34
       end
31 35
     end
32 36
 
33 37
     class OpenTransaction < Transaction #:nodoc:
34  
-      attr_reader :parent, :records
  38
+      attr_reader   :parent, :records
  39
+      attr_accessor :joinable
  40
+
  41
+      alias joinable? joinable
35 42
 
36 43
       def initialize(connection, parent)
37 44
         super connection
7  activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -247,15 +247,16 @@ def open_transactions
247 247
       end
248 248
 
249 249
       def increment_open_transactions
250  
-        ActiveSupport::Deprecation.warn "increment_open_transactions is deprecated and has no effect"
  250
+        ActiveSupport::Deprecation.warn "#increment_open_transactions is deprecated and has no effect"
251 251
       end
252 252
 
253 253
       def decrement_open_transactions
254  
-        ActiveSupport::Deprecation.warn "decrement_open_transactions is deprecated and has no effect"
  254
+        ActiveSupport::Deprecation.warn "#decrement_open_transactions is deprecated and has no effect"
255 255
       end
256 256
 
257 257
       def transaction_joinable=(joinable)
258  
-        @transaction_joinable = joinable
  258
+        ActiveSupport::Deprecation.warn "#transaction_joinable= is deprecated. Please pass the :joinable option to #begin_transaction instead."
  259
+        @transaction.joinable = joinable
259 260
       end
260 261
 
261 262
       def create_savepoint
5  activerecord/lib/active_record/fixtures.rb
@@ -843,8 +843,7 @@ def setup_fixtures
843 843
         end
844 844
         @fixture_connections = enlist_fixture_connections
845 845
         @fixture_connections.each do |connection|
846  
-          connection.begin_transaction
847  
-          connection.transaction_joinable = false
  846
+          connection.begin_transaction joinable: false
848 847
         end
849 848
       # Load fixtures for every test.
850 849
       else
@@ -867,7 +866,7 @@ def teardown_fixtures
867 866
       # Rollback changes if a transaction is active.
868 867
       if run_in_transaction?
869 868
         @fixture_connections.each do |connection|
870  
-          connection.rollback_transaction if connection.transaction_state.open?
  869
+          connection.rollback_transaction if connection.transaction_open?
871 870
         end
872 871
         @fixture_connections.clear
873 872
       end
9  activerecord/test/cases/transactions_test.rb
@@ -581,5 +581,14 @@ def test_transaction_isolation__read_committed
581 581
 
582 582
       assert_equal original_salary, Developer.find(1).salary
583 583
     end
  584
+
  585
+    test "#transaction_joinable= is deprecated" do
  586
+      Developer.transaction do
  587
+        conn = Developer.connection
  588
+        assert conn.current_transaction.joinable?
  589
+        assert_deprecated { conn.transaction_joinable = false }
  590
+        assert !conn.current_transaction.joinable?
  591
+      end
  592
+    end
584 593
   end
585 594
 end

0 notes on commit 2805875

Please sign in to comment.
Something went wrong with that request. Please try again.