Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Handle case when AR config is not set.

  • Loading branch information...
commit c67e7fec1f74a0f45b5fb9a130f4f1ee354aa820 1 parent 5039dc9
Alexander Staubo authored
Showing with 27 additions and 21 deletions.
  1. +19 −17 lib/multidb/balancer.rb
  2. +8 −4 lib/multidb/configuration.rb
View
36 lib/multidb/balancer.rb
@@ -30,24 +30,26 @@ class Balancer
def initialize(configuration)
@candidates = {}.with_indifferent_access
@configuration = configuration
- (@configuration.raw_configuration[:databases] || {}).each_pair do |name, config|
- configs = config.is_a?(Array) ? config : [config]
- configs.each do |config|
- candidate = Candidate.new(@configuration.default_adapter.merge(config))
- @candidates[name] ||= []
- @candidates[name].push(candidate)
+ if @configuration
+ (@configuration.raw_configuration[:databases] || {}).each_pair do |name, config|
+ configs = config.is_a?(Array) ? config : [config]
+ configs.each do |config|
+ candidate = Candidate.new(@configuration.default_adapter.merge(config))
+ @candidates[name] ||= []
+ @candidates[name].push(candidate)
+ end
+ end
+ if @configuration.raw_configuration.include?(:fallback)
+ @fallback = @configuration.raw_configuration[:fallback]
+ elsif defined?(Rails)
+ @fallback = %w(development test).include?(Rails.env)
+ else
+ @fallback = false
+ end
+ @default_candidate = Candidate.new(@configuration.default_pool)
+ unless @candidates.include?(:default)
+ @candidates[:default] = [@default_candidate]
end
- end
- if @configuration.raw_configuration.include?(:fallback)
- @fallback = @configuration.raw_configuration[:fallback]
- elsif defined?(Rails)
- @fallback = %w(development test).include?(Rails.env)
- else
- @fallback = false
- end
- @default_candidate = Candidate.new(@configuration.default_pool)
- unless @candidates.include?(:default)
- @candidates[:default] = [@default_candidate]
end
end
View
12 lib/multidb/configuration.rb
@@ -3,10 +3,14 @@ module Multidb
class << self
def configure!
- activerecord_config = ActiveRecord::Base.connection_pool.connection.instance_variable_get(:@config).dup.with_indifferent_access
- default_adapter, configuration_hash = activerecord_config, activerecord_config.delete(:multidb)
- configuration_hash ||= {}
- @configuration = Configuration.new(default_adapter, configuration_hash)
+ connection_pool = ActiveRecord::Base.connection_pool
+ if connection_pool
+ connection = connection_pool.connection
+ activerecord_config = connection.instance_variable_get(:@config).dup.with_indifferent_access
+ default_adapter, configuration_hash = activerecord_config, activerecord_config.delete(:multidb)
+ configuration_hash ||= {}
+ @configuration = Configuration.new(default_adapter, configuration_hash)
+ end
end
attr_reader :configuration
Please sign in to comment.
Something went wrong with that request. Please try again.