Skip to content

Commit

Permalink
Old version support
Browse files Browse the repository at this point in the history
  • Loading branch information
kenn committed Nov 4, 2023
1 parent 1bd3df2 commit 8305829
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
6 changes: 4 additions & 2 deletions lib/standby/active_record/log_subscriber.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 5 additions & 1 deletion lib/standby/connection_holder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/standby/version.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 17 additions & 5 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 8305829

Please sign in to comment.