Module: Switchman::ActiveRecord::Base::ClassMethods

Defined in:
lib/switchman/active_record/base.rb

Instance Method Summary collapse

Instance Method Details

#clear_query_caches_for_current_threadObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/switchman/active_record/base.rb', line 55

def clear_query_caches_for_current_thread
  pools = ::ActiveRecord::Base.connection_handler.connection_pool_list(:all)
  pools.each do |pool|
    if ::Rails.version < "7.2"
      pool.connection(switch_shard: false).clear_query_cache if pool.active_connection?
    elsif pool.active_connection?
      pool.lease_connection(switch_shard: false).clear_query_cache
    end
  end
end

#connected_to_stackObject



88
89
90
91
92
93
94
95
96
# File 'lib/switchman/active_record/base.rb', line 88

def connected_to_stack
  has_own_stack = ::ActiveSupport::IsolatedExecutionState.key?(:active_record_connected_to_stack)

  ret = super
  return ret if has_own_stack

  DatabaseServer.guard_servers
  ret
end

#current_role(without_overrides: false, target_shard: current_shard) ⇒ Object

significant change: Allow per-shard roles



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/switchman/active_record/base.rb', line 99

def current_role(without_overrides: false, target_shard: current_shard)
  return super() if without_overrides

  sharded_role = nil
  connected_to_stack.reverse_each do |hash|
    shard_role = hash.dig(:shard_roles, target_shard)
    unless shard_role &&
           (hash[:klasses].include?(::ActiveRecord::Base) || hash[:klasses].include?(connection_class_for_self))
      next
    end

    sharded_role = shard_role
    break
  end
  # Allow a shard-specific role to be reverted to regular inheritance
  return sharded_role if sharded_role && sharded_role != :_switchman_inherit

  super()
end

#current_shardObject

significant change: _don’t_ check if klasses.include?(Base) i.e. other sharded models don’t inherit the current shard of Base



121
122
123
124
125
126
127
# File 'lib/switchman/active_record/base.rb', line 121

def current_shard
  connected_to_stack.reverse_each do |hash|
    return hash[:shard] if hash[:shard] && hash[:klasses].include?(connection_class_for_self)
  end

  default_shard
end

#current_switchman_shardObject



129
130
131
132
133
134
135
136
137
# File 'lib/switchman/active_record/base.rb', line 129

def current_switchman_shard
  connected_to_stack.reverse_each do |hash|
    if hash[:switchman_shard] && hash[:klasses].include?(connection_class_for_self)
      return hash[:switchman_shard]
    end
  end

  Shard.default
end

#establish_connection(config_or_env = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/switchman/active_record/base.rb', line 70

def establish_connection(config_or_env = nil)
  if config_or_env.is_a?(Symbol) && config_or_env != ::Rails.env.to_sym
    raise ArgumentError,
          "establish connection cannot be used on the non-current shard/role"
  end

  # Ensure we don't randomly surprise change the connection parms associated with a shard/role
  config_or_env = nil if config_or_env == ::Rails.env.to_sym

  config_or_env ||= if current_shard == ::Rails.env.to_sym && current_role == :primary
                      :primary
                    else
                      :"#{current_shard}/#{current_role}"
                    end

  super
end

#find_ids_in_ranges(opts = {}) ⇒ Object



9
10
11
12
# File 'lib/switchman/active_record/base.rb', line 9

def find_ids_in_ranges(opts = {}, &)
  opts.reverse_merge!(loose: true)
  all.find_ids_in_ranges(opts, &)
end

#integral_id?Boolean

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/switchman/active_record/base.rb', line 20

def integral_id?
  @integral_id = columns_hash[primary_key]&.type == :integer if @integral_id.nil?
  @integral_id
end

#reset_column_informationObject



37
38
39
40
# File 'lib/switchman/active_record/base.rb', line 37

def reset_column_information
  @sharded_column_values = {}
  super
end

#role_overriden?(shard_id) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/switchman/active_record/base.rb', line 66

def role_overriden?(shard_id)
  current_role(target_shard: shard_id) != current_role(without_overrides: true)
end

#sharded_modelObject



14
15
16
17
18
# File 'lib/switchman/active_record/base.rb', line 14

def sharded_model
  self.abstract_class = true

  Shard.send(:add_sharded_model, self)
end

#transactionObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/switchman/active_record/base.rb', line 25

def transaction(**)
  if self != ::ActiveRecord::Base && current_scope
    current_scope.activate do
      db = Shard.current(connection_class_for_self).database_server
      db.unguard { super }
    end
  else
    db = Shard.current(connection_class_for_self).database_server
    db.unguard { super }
  end
end

#unscopedObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/switchman/active_record/base.rb', line 42

def unscoped
  if block_given?
    super do
      current_scope.shard_value = nil
      yield
    end
  else
    result = super
    result.shard_value = nil
    result
  end
end