Class: ActiveRecordShards::ShardSelection
- Inherits:
 - 
      Object
      
        
- Object
 - ActiveRecordShards::ShardSelection
 
 
- Defined in:
 - lib/active_record_shards/shard_selection.rb
 
Constant Summary collapse
- NO_SHARD =
 :_no_shard
Instance Method Summary collapse
- 
  
    
      #initialize  ⇒ ShardSelection 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of ShardSelection.
 - #on_replica=(new_replica) ⇒ Object
 - #on_replica? ⇒ Boolean
 - #options ⇒ Object
 - #resolve_connection_name(sharded:, configurations:) ⇒ Object
 - #shard ⇒ Object
 - #shard=(new_shard) ⇒ Object
 
Constructor Details
#initialize ⇒ ShardSelection
Returns a new instance of ShardSelection.
      8 9 10 11  | 
    
      # File 'lib/active_record_shards/shard_selection.rb', line 8 def initialize @on_replica = false @shard = nil end  | 
  
Instance Method Details
#on_replica=(new_replica) ⇒ Object
      64 65 66  | 
    
      # File 'lib/active_record_shards/shard_selection.rb', line 64 def on_replica=(new_replica) @on_replica = (new_replica == true) end  | 
  
#on_replica? ⇒ Boolean
      60 61 62  | 
    
      # File 'lib/active_record_shards/shard_selection.rb', line 60 def on_replica? @on_replica end  | 
  
#options ⇒ Object
      68 69 70  | 
    
      # File 'lib/active_record_shards/shard_selection.rb', line 68 def { shard: @shard, replica: @on_replica } end  | 
  
#resolve_connection_name(sharded:, configurations:) ⇒ Object
      26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54  | 
    
      # File 'lib/active_record_shards/shard_selection.rb', line 26 def resolve_connection_name(sharded:, configurations:) resolved_shard = sharded ? shard : nil env = ActiveRecordShards.app_env @connection_names ||= {} @connection_names[env] ||= {} @connection_names[env][resolved_shard] ||= {} @connection_names[env][resolved_shard][@on_replica] ||= begin name = env.dup name << "_shard_#{resolved_shard}" if resolved_shard replica_config = begin case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}" when '7.0' configurations.configs_for(env_name: "#{name}_replica", include_hidden: true).any? when '6.1' configurations.configs_for(env_name: "#{name}_replica", include_replicas: true).any? else configurations["#{name}_replica"] end end if @on_replica && replica_config "#{name}_replica" else # ActiveRecord always names its default connection pool 'primary' # while everything else is named by the configuration name resolved_shard ? name : PRIMARY end end end  | 
  
#shard ⇒ Object
      13 14 15 16 17 18 19  | 
    
      # File 'lib/active_record_shards/shard_selection.rb', line 13 def shard if @shard.nil? || @shard == NO_SHARD nil else @shard || self.class.ars_default_shard end end  | 
  
#shard=(new_shard) ⇒ Object
      56 57 58  | 
    
      # File 'lib/active_record_shards/shard_selection.rb', line 56 def shard=(new_shard) @shard = (new_shard || NO_SHARD) end  |