Sha256: 03064fb5e96529e08bbbb19f9b691fed6c8636b26310ca4016c5388c14ff0d66
Contents?: true
Size: 1.4 KB
Versions: 13
Compression:
Stored size: 1.4 KB
Contents
module ActiveRecord::Turntable class Shard DEFAULT_CONFIG = { "connection" => (defined?(Rails) ? Rails.env : "development") }.with_indifferent_access def initialize(shard_spec) @config = DEFAULT_CONFIG.merge(shard_spec) @name = @config["connection"] end def connection_pool @connection_pool ||= retrieve_connection_pool end def connection connection = connection_pool.connection connection.turntable_shard_name = name connection end def name @name end private def retrieve_connection_pool ActiveRecord::Base.turntable_connections[name] ||= begin config = ActiveRecord::Base.configurations[Rails.env]["shards"][name] raise ArgumentError, "Unknown database config: #{name}, have #{ActiveRecord::Base.configurations.inspect}" unless config ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec_for(config)) end end def spec_for(config) begin require "active_record/connection_adapters/#{config['adapter']}_adapter" rescue LoadError => e raise "Please install the #{config['adapter']} adapter: `gem install activerecord-#{config['adapter']}-adapter` (#{e})" end adapter_method = "#{config['adapter']}_connection" ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(config, adapter_method) end end end
Version data entries
13 entries across 13 versions & 1 rubygems