class ReplicaPools::Pools

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/replica_pools/pools.rb, line 7
def initialize
  pools = {}
  pool_configurations.group_by{|_, name, _| name }.each do |name, set|
    pools[name.to_sym] = ReplicaPools::Pool.new(
      name,
      set.map{ |conn_name, _, replica_name|
        connection_class(name, replica_name, conn_name)
      }
    )
  end

  if pools.empty?
    ReplicaPools.log :info, "No pools found for #{ReplicaPools.config.environment}. Loading a default pool with leader instead."
    pools[:default] = ReplicaPools::Pool.new('default', [ActiveRecord::Base])
  end

  super pools
end

Private Instance Methods

config_hash() click to toggle source
# File lib/replica_pools/pools.rb, line 36
def config_hash
  if ActiveRecord::VERSION::MAJOR >= 6
    # in Rails >= 6, `configurations` is an instance of ActiveRecord::DatabaseConfigurations
    ActiveRecord::Base.configurations.to_h
  else
    # in Rails < 6, it's just a hash
    ActiveRecord::Base.configurations
  end
end
connection_class(pool_name, replica_name, connection_name) click to toggle source

generates a unique ActiveRecord::Base subclass for a single replica

# File lib/replica_pools/pools.rb, line 47
def connection_class(pool_name, replica_name, connection_name)
  class_name = "#{pool_name.camelize}#{replica_name.camelize}"

  ReplicaPools.module_eval %Q{
    class #{class_name} < ActiveRecord::Base
      self.abstract_class = true
      establish_connection :#{connection_name}
      def self.connection_config
        configurations[#{connection_name.to_s.inspect}]
      end
    end
  }, __FILE__, __LINE__
  ReplicaPools.const_get(class_name)
end
pool_configurations() click to toggle source

finds valid pool configs

# File lib/replica_pools/pools.rb, line 29
def pool_configurations
  config_hash.map do |name, config|
    next unless name.to_s =~ /#{ReplicaPools.config.environment}_pool_(.*)_name_(.*)/
    [name, $1, $2]
  end.compact
end