Skip to content

Commit

Permalink
Merge pull request #40 from xspaniki/master
Browse files Browse the repository at this point in the history
Fix ActiveRecord::Base.configurations for rails 7
  • Loading branch information
kenn authored Mar 29, 2023
2 parents babe13f + f708163 commit 6fbc059
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
5 changes: 5 additions & 0 deletions gemfiles/rails7.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source "https://rubygems.org"

gemspec name: 'standby', path: '../'

gem 'activerecord', '~> 7.0'
8 changes: 5 additions & 3 deletions lib/standby/connection_holder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ class ConnectionHolder < ActiveRecord::Base
class << self
# for delayed activation
def activate(target)
spec = ActiveRecord::Base.configurations["#{ActiveRecord::ConnectionHandling::RAILS_ENV.call}_#{target}"]
raise Error.new("Standby target '#{target}' is invalid!") if spec.nil?
env_name = "#{ActiveRecord::ConnectionHandling::RAILS_ENV.call}_#{target}"
spec = ActiveRecord::Base.configurations.find_db_config(env_name)&.configuration_hash
raise Error, "Standby target '#{target}' is invalid!" if spec.nil?

establish_connection spec
end
end
Expand All @@ -25,4 +27,4 @@ def connection_holder(target)
end
end
end
end
end
8 changes: 5 additions & 3 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
before do
# Backup connection and configs
@backup_conn = Standby.instance_variable_get :@standby_connections
@backup_config = ActiveRecord::Base.configurations.dup
@backup_config = ActiveRecord::Base.configurations.configs_for.map do |config|
[config.env_name, config.configuration_hash]
end.to_h
@backup_disabled = Standby.disabled
@backup_conn.each_key do |klass_name|
Object.send(:remove_const, klass_name) if Object.const_defined?(klass_name)
Expand All @@ -20,13 +22,13 @@
end

it 'raises error if standby configuration not specified' do
ActiveRecord::Base.configurations['test_standby'] = nil
ActiveRecord::Base.configurations = @backup_config.merge({ 'test_standby' => {} })

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['test_standby'] = nil
ActiveRecord::Base.configurations = @backup_config.merge({ 'test_standby' => {} })
Standby.disabled = true

expect(Standby.on_standby { User.count }).to be 2
Expand Down
2 changes: 1 addition & 1 deletion standby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
gem.require_paths = ['lib']
gem.required_ruby_version = '>= 2.0'

gem.add_runtime_dependency 'activerecord', '>= 3.0.0', '< 6.0'
gem.add_runtime_dependency 'activerecord', '>= 3.0.0', '< 8.0'

gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
Expand Down

0 comments on commit 6fbc059

Please sign in to comment.