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
# File 'lib/switchman/active_record/base.rb', line 55

def clear_query_caches_for_current_thread
  ::ActiveRecord::Base.connection_handler.connection_pool_list.each do |pool|
    pool.connection(switch_shard: false).clear_query_cache if pool.active_connection?
  end
end

#connected_to_stackObject



80
81
82
83
84
85
86
# File 'lib/switchman/active_record/base.rb', line 80

def connected_to_stack
  return super if ::Rails.version < '7.0' ? Thread.current.thread_variable?(:ar_connected_to_stack) : ::ActiveSupport::IsolatedExecutionState.key?(:active_record_connected_to_stack)

  ret = super
  DatabaseServer.guard_servers
  ret
end

#connection_class_for_selfObject



125
126
127
# File 'lib/switchman/active_record/base.rb', line 125

def connection_class_for_self
  connection_classes
end

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

significant change: Allow per-shard roles



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/switchman/active_record/base.rb', line 89

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)
    if shard_role && (hash[:klasses].include?(::ActiveRecord::Base) || hash[:klasses].include?(connection_class_for_self))
      sharded_role = shard_role
      break
    end
  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



108
109
110
111
112
113
114
# File 'lib/switchman/active_record/base.rb', line 108

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



116
117
118
119
120
121
122
# File 'lib/switchman/active_record/base.rb', line 116

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

  Shard.default
end

#establish_connection(config_or_env = nil) ⇒ Object

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/switchman/active_record/base.rb', line 65

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

  # 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}".to_sym
                    end

  super(config_or_env)
end

#find_ids_in_ranges(opts = {}, &block) ⇒ Object



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

def find_ids_in_ranges(opts = {}, &block)
  opts.reverse_merge!(loose: true)
  all.find_ids_in_ranges(opts, &block)
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)


61
62
63
# File 'lib/switchman/active_record/base.rb', line 61

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