diff --git a/lib/standby/active_record/log_subscriber.rb b/lib/standby/active_record/log_subscriber.rb index 8d2de13..d7e9515 100644 --- a/lib/standby/active_record/log_subscriber.rb +++ b/lib/standby/active_record/log_subscriber.rb @@ -1,15 +1,17 @@ +require 'standby/version' + module ActiveRecord class LogSubscriber alias_method :debug_without_standby, :debug def debug(msg) - db = Standby.disabled ? "" : log_header + db = Standby.disabled ? '' : log_header debug_without_standby(db + msg) end def log_header - if Gem::Version.new(ActiveRecord.version) >= Gem::Version.new('7.1') + if Standby.version_gte?('7.1') color("[#{Thread.current[:_standby] || "primary"}]", ActiveSupport::LogSubscriber::GREEN, bold: true) else color("[#{Thread.current[:_standby] || "primary"}]", ActiveSupport::LogSubscriber::GREEN, true) diff --git a/lib/standby/connection_holder.rb b/lib/standby/connection_holder.rb index cd8cc6e..3dd487d 100644 --- a/lib/standby/connection_holder.rb +++ b/lib/standby/connection_holder.rb @@ -6,7 +6,11 @@ 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 Standby.version_gte?('7.0') + spec = ActiveRecord::Base.configurations.find_db_config(env_name)&.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/lib/standby/version.rb b/lib/standby/version.rb index 0c8d1f0..f0d0bba 100644 --- a/lib/standby/version.rb +++ b/lib/standby/version.rb @@ -1,3 +1,9 @@ module Standby VERSION = '4.0.0' + + class << self + def version_gte?(version) + Gem::Version.new(ActiveRecord.version) >= Gem::Version.new(version) + end + end end diff --git a/spec/configuration_spec.rb b/spec/configuration_spec.rb index 6676e45..580cfe1 100644 --- a/spec/configuration_spec.rb +++ b/spec/configuration_spec.rb @@ -4,9 +4,13 @@ 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 + if Standby.version_gte?('7.0') + @backup_config = ActiveRecord::Base.configurations.configs_for.map do |config| + [config.env_name, config.configuration_hash] + end.to_h + else + @backup_config = ActiveRecord::Base.configurations.dup + end @backup_disabled = Standby.disabled @backup_conn.each_key do |klass_name| Object.send(:remove_const, klass_name) if Object.const_defined?(klass_name) @@ -22,13 +26,21 @@ end it 'raises error if standby configuration not specified' do - ActiveRecord::Base.configurations = @backup_config.merge({ 'test_standby' => {} }) + if Standby.version_gte?('7.0') + ActiveRecord::Base.configurations = @backup_config.merge({ 'test_standby' => {} }) + else + ActiveRecord::Base.configurations['test_standby'] = nil + end 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' => {} }) + if Standby.version_gte?('7.0') + ActiveRecord::Base.configurations = @backup_config.merge({ 'test_standby' => {} }) + else + ActiveRecord::Base.configurations['test_standby'] = nil + end Standby.disabled = true expect(Standby.on_standby { User.count }).to be 2