Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compatibility with older active record versions #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/standby/connection_holder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down