diff --git a/lib/standby/connection_holder.rb b/lib/standby/connection_holder.rb index cd8cc6e..a41fbad 100644 --- a/lib/standby/connection_holder.rb +++ b/lib/standby/connection_holder.rb @@ -6,7 +6,13 @@ class << self # for delayed activation def activate(target) env_name = "#{ActiveRecord::ConnectionHandling::RAILS_ENV.call}_#{target}" - spec = ActiveRecord::Base.configurations.find_db_config(env_name)&.configuration_hash + + if ActiveRecord.version >= Gem::Version.new('6.0') + spec = ActiveRecord::Base.configurations.find_db_config(env_name).try(:configuration_hash) + else + spec = ActiveRecord::Base.configurations[env_name] + end + raise Error, "Standby target '#{target}' is invalid!" if spec.nil? establish_connection spec diff --git a/spec/configuration_spec.rb b/spec/configuration_spec.rb index 6676e45..b0d5954 100644 --- a/spec/configuration_spec.rb +++ b/spec/configuration_spec.rb @@ -4,13 +4,22 @@ before do # Backup connection and configs @backup_conn = Standby.instance_variable_get :@standby_connections - @backup_config = ActiveRecord::Base.configurations.configs_for.map do |config| - [config.env_name, config.configuration_hash] - end.to_h @backup_disabled = Standby.disabled + + if ActiveRecord.version >= Gem::Version.new('6.0') + @backup_config = ActiveRecord::Base.configurations.configs_for.map do |config| + [config.env_name, config.configuration_hash] + end.to_h + @empty_config = {} + else + @backup_config = ActiveRecord::Base.configurations.dup + @empty_config = nil + end + @backup_conn.each_key do |klass_name| Object.send(:remove_const, klass_name) if Object.const_defined?(klass_name) end + Standby.instance_variable_set :@standby_connections, {} end @@ -22,13 +31,13 @@ end it 'raises error if standby configuration not specified' do - ActiveRecord::Base.configurations = @backup_config.merge({ 'test_standby' => {} }) + ActiveRecord::Base.configurations = @backup_config.merge({ 'test_standby' => @empty_config }) expect { Standby.on_standby { User.count } }.to raise_error(Standby::Error) end it 'connects to primary if standby configuration is disabled' do - ActiveRecord::Base.configurations = @backup_config.merge({ 'test_standby' => {} }) + ActiveRecord::Base.configurations = @backup_config.merge({ 'test_standby' => @empty_config }) Standby.disabled = true expect(Standby.on_standby { User.count }).to be 2