active_record_shards 2.5.8 → 2.5.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -39,12 +39,7 @@ module ActiveRecordShards
39
39
  reflection = @association_collection.proxy_reflection
40
40
  end
41
41
 
42
- case @which
43
- when :slave
44
- reflection.klass.on_slave_block { @association_collection.send(method, *args, &block) }
45
- when :master
46
- reflection.klass.on_master_block { @association_collection.send(method, *args, &block) }
47
- end
42
+ reflection.klass.on_cx_switch_block(@which) { @association_collection.send(method, *args, &block) }
48
43
  end
49
44
  end
50
45
  end
@@ -55,12 +55,7 @@ module ActiveRecordShards
55
55
 
56
56
  def on_master_or_slave(which, &block)
57
57
  if block_given?
58
- case which
59
- when :master
60
- on_master_block(&block)
61
- when :slave
62
- on_slave_block(&block)
63
- end
58
+ on_cx_switch_block(which, &block)
64
59
  else
65
60
  MasterSlaveProxy.new(self, which)
66
61
  end
@@ -88,9 +83,13 @@ module ActiveRecordShards
88
83
  alias_method :with_slave_if, :on_slave_if
89
84
  alias_method :with_slave_unless, :on_slave_unless
90
85
 
91
- def on_slave_block(&block)
86
+ def on_cx_switch_block(which, &block)
92
87
  old_options = current_shard_selection.options
93
- switch_connection(:slave => (@disallow_slave.nil? || @disallow_slave == 0))
88
+ switch_to_slave = (which == :slave && (@disallow_slave.nil? || @disallow_slave == 0))
89
+ switch_connection(:slave => switch_to_slave)
90
+
91
+ @disallow_slave = (@disallow_slave || 0) + 1 if which == :master
92
+
94
93
  # setting read-only scope on ActiveRecord::Base never made any sense, anyway
95
94
  if self == ActiveRecord::Base
96
95
  yield
@@ -98,18 +97,10 @@ module ActiveRecordShards
98
97
  with_scope({:find => {:readonly => on_slave?}}, :merge, &block)
99
98
  end
100
99
  ensure
100
+ @disallow_slave -= 1 if which == :master
101
101
  switch_connection(old_options)
102
102
  end
103
103
 
104
- # to make this re-entrant we need to keep a counter.
105
- def on_master_block(&block)
106
- @disallow_slave ||= 0
107
- @disallow_slave += 1
108
- yield
109
- ensure
110
- @disallow_slave -= 1
111
- end
112
-
113
104
  # Name of the connection pool. Used by ConnectionHandler to retrieve the current connection pool.
114
105
  def connection_pool_name # :nodoc:
115
106
  name = @connection_pool_name_override || current_shard_selection.shard_name(self)
@@ -24,6 +24,11 @@ module ActiveRecordShards
24
24
  self.class.quote_value(*args, &block)
25
25
  end
26
26
 
27
+ def reload_with_slave_off
28
+ self.class.on_master { reload_without_slave_off }
29
+ end
30
+ alias_method_chain :reload, :slave_off
31
+
27
32
  class << self
28
33
  def transaction_with_slave_off(*args, &block)
29
34
  if on_slave_by_default?
@@ -375,6 +375,11 @@ class ConnectionSwitchenTest < ActiveSupport::TestCase
375
375
  assert_equal 2, count
376
376
  end
377
377
 
378
+ should "reload() on the master" do
379
+ account = Account.find(1000)
380
+ assert_equal 'master_name', account.reload.name
381
+ end
382
+
378
383
  should "Allow override using on_master" do
379
384
  model = Account.on_master.find(1000)
380
385
  assert_equal "master_name", model.name