Module: ActiveRecordShards::DefaultSlavePatches

Included in:
ActiveRecord::Base
Defined in:
lib/active_record_shards/default_slave_patches.rb

Defined Under Namespace

Modules: ActiveRelationPatches, InstanceMethods, Rails41HasAndBelongsToManyBuilderExtension

Constant Summary collapse

CLASS_SLAVE_METHODS =
[
  :calculate,
  :count_by_sql,
  :exists?,
  :find_by_sql,
  :find_every,
  :find_one,
  :find_some
].freeze
CLASS_FORCE_SLAVE_METHODS =
[
  :columns,
  :replace_bind_variable,
  :replace_bind_variables,
  :sanitize_sql_array,
  :sanitize_sql_hash_for_assignment,
  :table_exists?
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/active_record_shards/default_slave_patches.rb', line 72

def self.extended(base)
  CLASS_SLAVE_METHODS.each { |m| ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(true, base, m) }
  CLASS_FORCE_SLAVE_METHODS.each { |m| ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(true, base, m, force_on_slave: true) }

  base.class_eval do
    include InstanceMethods

    class << self
      alias_method :transaction_without_slave_off, :transaction
      alias_method :transaction, :transaction_with_slave_off
    end
  end
  if ActiveRecord::Associations.const_defined?(:HasAndBelongsToManyAssociation)
    ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, ActiveRecord::Associations::HasAndBelongsToManyAssociation, :construct_sql)
    ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, ActiveRecord::Associations::HasAndBelongsToManyAssociation, :construct_find_options!)
  end
end

.wrap_method_in_on_slave(class_method, base, method, force_on_slave: false) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_record_shards/default_slave_patches.rb', line 5

def self.wrap_method_in_on_slave(class_method, base, method, force_on_slave: false)
  base_methods =
    if class_method
      base.methods + base.private_methods
    else
      base.instance_methods + base.private_instance_methods
    end

  return unless base_methods.include?(method)

  _, method, punctuation = method.to_s.match(/^(.*?)([\?\!]?)$/).to_a
  # _ALWAYS_ on slave, or only for on `on_slave_by_default = true` models?
  wrapper = force_on_slave ? 'force_on_slave' : 'on_slave_unless_tx'
  base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    #{class_method ? 'class << self' : ''}
      def #{method}_with_default_slave#{punctuation}(*args, &block)
        #{wrapper} do
          #{method}_without_default_slave#{punctuation}(*args, &block)
        end
      end

      alias_method :#{method}_without_default_slave#{punctuation}, :#{method}#{punctuation}
      alias_method :#{method}#{punctuation}, :#{method}_with_default_slave#{punctuation}
    #{class_method ? 'end' : ''}
  RUBY
end

Instance Method Details

#force_on_slave(&block) ⇒ Object



98
99
100
# File 'lib/active_record_shards/default_slave_patches.rb', line 98

def force_on_slave(&block)
  on_cx_switch_block(:slave, construct_ro_scope: false, force: true, &block)
end

#on_slave_unless_tx(&block) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/active_record_shards/default_slave_patches.rb', line 90

def on_slave_unless_tx(&block)
  if on_slave_by_default? && !Thread.current[:_active_record_shards_slave_off]
    on_slave(&block)
  else
    yield
  end
end

#transaction_with_slave_off(*args, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/active_record_shards/default_slave_patches.rb', line 32

def transaction_with_slave_off(*args, &block)
  if on_slave_by_default?
    begin
      old_val = Thread.current[:_active_record_shards_slave_off]
      Thread.current[:_active_record_shards_slave_off] = true
      transaction_without_slave_off(*args, &block)
    ensure
      Thread.current[:_active_record_shards_slave_off] = old_val
    end
  else
    transaction_without_slave_off(*args, &block)
  end
end