From f7081636eef264b44ce49293db55c25c439d1908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20S=CC=8Cpa=CC=81nik?= Date: Tue, 28 Mar 2023 18:04:22 +0200 Subject: [PATCH] Fix ActiveRecord::Base.configurations for rails 7 --- gemfiles/rails7.0.gemfile | 5 +++++ lib/standby/connection_holder.rb | 8 +++++--- spec/configuration_spec.rb | 8 +++++--- standby.gemspec | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 gemfiles/rails7.0.gemfile diff --git a/gemfiles/rails7.0.gemfile b/gemfiles/rails7.0.gemfile new file mode 100644 index 0000000..41dc24d --- /dev/null +++ b/gemfiles/rails7.0.gemfile @@ -0,0 +1,5 @@ +source "https://rubygems.org" + +gemspec name: 'standby', path: '../' + +gem 'activerecord', '~> 7.0' \ No newline at end of file diff --git a/lib/standby/connection_holder.rb b/lib/standby/connection_holder.rb index 5b2a31e..cd8cc6e 100644 --- a/lib/standby/connection_holder.rb +++ b/lib/standby/connection_holder.rb @@ -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 @@ -25,4 +27,4 @@ def connection_holder(target) end end end -end +end \ No newline at end of file diff --git a/spec/configuration_spec.rb b/spec/configuration_spec.rb index 81d39c1..6676e45 100644 --- a/spec/configuration_spec.rb +++ b/spec/configuration_spec.rb @@ -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) @@ -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 diff --git a/standby.gemspec b/standby.gemspec index de29fa3..2dcc61c 100644 --- a/standby.gemspec +++ b/standby.gemspec @@ -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'