Module: ActiveRecordShards::DefaultReplicaPatches

Defined in:
lib/active_record_shards/default_replica_patches.rb

Defined Under Namespace

Modules: ActiveRelationPatches, AssociationsAssociationAssociationScopePatch, AssociationsAssociationFindTargetPatch, AssociationsAssociationGetRecordsPatch, AssociationsPreloaderAssociationAssociatedRecordsByOwnerPatch, AssociationsPreloaderAssociationLoadRecordsPatch, InstanceMethods, Rails41HasAndBelongsToManyBuilderExtension, Rails52RelationPatches, SchemaDefinePatch, TypeCasterConnectionConnectionPatch

Constant Summary collapse

CLASS_REPLICA_METHODS =
[
  :calculate,
  :count_by_sql,
  :exists?,
  :find,
  :find_by,
  :find_by_sql,
  :find_every,
  :find_one,
  :find_some,
  :get_primary_key
].freeze
CLASS_FORCE_REPLICA_METHODS =
[
  :replace_bind_variable,
  :replace_bind_variables,
  :sanitize_sql_array,
  :sanitize_sql_hash_for_assignment,
  :table_exists?
].freeze
CLASS_SLAVE_METHODS =
CLASS_REPLICA_METHODS
CLASS_FORCE_SLAVE_METHODS =
CLASS_FORCE_REPLICA_METHODS

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/active_record_shards/default_replica_patches.rb', line 90

def self.extended(base)
  CLASS_REPLICA_METHODS.each { |m| ActiveRecordShards::DefaultReplicaPatches.wrap_method_in_on_replica(true, base, m) }
  CLASS_FORCE_REPLICA_METHODS.each { |m| ActiveRecordShards::DefaultReplicaPatches.wrap_method_in_on_replica(true, base, m, force_on_replica: true) }

  if ActiveRecord::VERSION::MAJOR >= 5
    ActiveRecordShards::DefaultReplicaPatches.wrap_method_in_on_replica(true, base, :load_schema!, force_on_replica: true)
  else
    ActiveRecordShards::DefaultReplicaPatches.wrap_method_in_on_replica(true, base, :columns, force_on_replica: true)
  end

  ActiveRecordShards::DefaultReplicaPatches.wrap_method_in_on_replica(false, base, :reload)

  base.class_eval do
    include InstanceMethods

    class << self
      alias_method :transaction_without_replica_off, :transaction
      alias_method :transaction, :transaction_with_replica_off
    end
  end
end

.wrap_method_in_on_replica(class_method, base, method, force_on_replica: 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_replica_patches.rb', line 5

def self.wrap_method_in_on_replica(class_method, base, method, force_on_replica: 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 replica, or only for on `on_replica_by_default = true` models?
  wrapper = force_on_replica ? 'force_on_replica' : 'on_replica_unless_tx'
  base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    #{class_method ? 'class << self' : ''}
      def #{method}_with_default_replica#{punctuation}(*args, &block)
        #{wrapper} do
          #{method}_without_default_replica#{punctuation}(*args, &block)
        end
      end

      alias_method :#{method}_without_default_replica#{punctuation}, :#{method}#{punctuation}
      alias_method :#{method}#{punctuation}, :#{method}_with_default_replica#{punctuation}
    #{class_method ? 'end' : ''}
  RUBY
end

.wrap_method_in_on_slave(*args) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/active_record_shards/default_replica_patches.rb', line 32

def self.wrap_method_in_on_slave(*args)
  ActiveRecordShards::Deprecation.deprecation_warning(
    :'self.wrap_method_in_on_slave',
    :'self.wrap_method_in_on_replica'
  )
  wrap_method_in_on_replica(*args)
end

Instance Method Details

#force_on_replica(&block) ⇒ Object



124
125
126
127
128
# File 'lib/active_record_shards/default_replica_patches.rb', line 124

def force_on_replica(&block)
  return yield if Thread.current[:_active_record_shards_in_migration]

  on_cx_switch_block(:replica, construct_ro_scope: false, force: true, &block)
end

#on_replica_unless_tx(&block) ⇒ Object Also known as: on_slave_unless_tx



112
113
114
115
116
117
118
119
120
121
# File 'lib/active_record_shards/default_replica_patches.rb', line 112

def on_replica_unless_tx(&block)
  return yield if Thread.current[:_active_record_shards_in_migration]
  return yield if Thread.current[:_active_record_shards_in_tx]

  if on_replica_by_default?
    on_replica(&block)
  else
    yield
  end
end

#transaction_with_replica_off(*args, &block) ⇒ Object Also known as: transaction_with_slave_off



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/active_record_shards/default_replica_patches.rb', line 40

def transaction_with_replica_off(*args, &block)
  if on_replica_by_default?
    begin
      old_val = Thread.current[:_active_record_shards_in_tx]
      Thread.current[:_active_record_shards_in_tx] = true
      transaction_without_replica_off(*args, &block)
    ensure
      Thread.current[:_active_record_shards_in_tx] = old_val
    end
  else
    transaction_without_replica_off(*args, &block)
  end
end