Module: Switchman::ActiveRecord::Base::ClassMethods
- Defined in:
- lib/switchman/active_record/base.rb
Instance Method Summary collapse
- #clear_query_caches_for_current_thread ⇒ Object
-
#current_role(without_overrides: false) ⇒ Object
significant change: Allow per-shard roles.
- #current_role_overriden? ⇒ Boolean
-
#current_shard ⇒ Object
significant change: _don’t_ check if klasses.include?(Base) i.e.
- #current_switchman_shard ⇒ Object
- #find_ids_in_ranges(opts = {}, &block) ⇒ Object
- #integral_id? ⇒ Boolean
- #reset_column_information ⇒ Object
- #sharded_model ⇒ Object
- #transaction ⇒ Object
- #unscoped ⇒ Object
Instance Method Details
#clear_query_caches_for_current_thread ⇒ Object
57 58 59 60 61 |
# File 'lib/switchman/active_record/base.rb', line 57 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 |
#current_role(without_overrides: false) ⇒ Object
significant change: Allow per-shard roles
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/switchman/active_record/base.rb', line 68 def current_role(without_overrides: false) return super() if without_overrides sharded_role = nil connected_to_stack.reverse_each do |hash| shard_role = hash.dig(:shard_roles, current_shard) if shard_role && (hash[:klasses].include?(::ActiveRecord::Base) || hash[:klasses].include?(connection_classes)) 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_role_overriden? ⇒ Boolean
63 64 65 |
# File 'lib/switchman/active_record/base.rb', line 63 def current_role_overriden? current_role != current_role(without_overrides: true) 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
87 88 89 90 91 92 93 |
# File 'lib/switchman/active_record/base.rb', line 87 def current_shard connected_to_stack.reverse_each do |hash| return hash[:shard] if hash[:shard] && hash[:klasses].include?(connection_classes) end default_shard end |
#current_switchman_shard ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/switchman/active_record/base.rb', line 95 def current_switchman_shard connected_to_stack.reverse_each do |hash| return hash[:switchman_shard] if hash[:switchman_shard] && hash[:klasses].include?(connection_classes) end Shard.default 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
22 23 24 25 |
# File 'lib/switchman/active_record/base.rb', line 22 def integral_id? @integral_id = columns_hash[primary_key]&.type == :integer if @integral_id.nil? @integral_id end |
#reset_column_information ⇒ Object
39 40 41 42 |
# File 'lib/switchman/active_record/base.rb', line 39 def reset_column_information @sharded_column_values = {} super end |
#sharded_model ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/switchman/active_record/base.rb', line 14 def sharded_model self.abstract_class = true return if self == UnshardedRecord Shard.send(:add_sharded_model, self) end |
#transaction ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/switchman/active_record/base.rb', line 27 def transaction(**) if self != ::ActiveRecord::Base && current_scope current_scope.activate do db = Shard.current(connection_classes).database_server db.unguard { super } end else db = Shard.current(connection_classes).database_server db.unguard { super } end end |
#unscoped ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/switchman/active_record/base.rb', line 44 def unscoped if block_given? super do current_scope.shard_value = nil yield end else result = super result.shard_value = nil result end end |