Module: Switchman::ActiveRecord::Base::ClassMethods
- Defined in:
- lib/switchman/active_record/base.rb
Instance Method Summary collapse
- #clear_query_caches_for_current_thread ⇒ Object
- #connected_to_stack ⇒ Object
- #connection_class_for_self ⇒ Object
-
#current_role(without_overrides: false, target_shard: current_shard) ⇒ Object
significant change: Allow per-shard roles.
-
#current_shard ⇒ Object
significant change: _don’t_ check if klasses.include?(Base) i.e.
- #current_switchman_shard ⇒ Object
- #establish_connection(config_or_env = nil) ⇒ Object
- #find_ids_in_ranges(opts = {}, &block) ⇒ Object
- #integral_id? ⇒ Boolean
- #reset_column_information ⇒ Object
- #role_overriden?(shard_id) ⇒ Boolean
- #sharded_model ⇒ Object
- #unscoped ⇒ Object
Instance Method Details
#clear_query_caches_for_current_thread ⇒ Object
59 60 61 62 63 |
# File 'lib/switchman/active_record/base.rb', line 59 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_stack ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/switchman/active_record/base.rb', line 87 def connected_to_stack has_own_stack = if ::Rails.version < "7.0" Thread.current.thread_variable?(:ar_connected_to_stack) else ::ActiveSupport::IsolatedExecutionState.key?(:active_record_connected_to_stack) end ret = super return ret if has_own_stack DatabaseServer.guard_servers ret end |
#connection_class_for_self ⇒ Object
143 144 145 |
# File 'lib/switchman/active_record/base.rb', line 143 def connection_class_for_self connection_classes end |
#current_role(without_overrides: false, target_shard: current_shard) ⇒ Object
significant change: Allow per-shard roles
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/switchman/active_record/base.rb', line 102 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_shard ⇒ Object
significant change: _don’t_ check if klasses.include?(Base) i.e. other sharded models don’t inherit the current shard of Base
124 125 126 127 128 129 130 |
# File 'lib/switchman/active_record/base.rb', line 124 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_shard ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/switchman/active_record/base.rb', line 132 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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/switchman/active_record/base.rb', line 69 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}".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
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_information ⇒ Object
41 42 43 44 |
# File 'lib/switchman/active_record/base.rb', line 41 def reset_column_information @sharded_column_values = {} super end |
#role_overriden?(shard_id) ⇒ Boolean
65 66 67 |
# File 'lib/switchman/active_record/base.rb', line 65 def role_overriden?(shard_id) current_role(target_shard: shard_id) != current_role(without_overrides: true) end |
#sharded_model ⇒ Object
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 |
#unscoped ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/switchman/active_record/base.rb', line 46 def unscoped if block_given? super do current_scope.shard_value = nil yield end else result = super result.shard_value = nil result end end |