File tree 2 files changed +21
-6
lines changed
2 files changed +21
-6
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,13 @@ class << self
6
6
# for delayed activation
7
7
def activate ( target )
8
8
env_name = "#{ ActiveRecord ::ConnectionHandling ::RAILS_ENV . call } _#{ target } "
9
- spec = ActiveRecord ::Base . configurations . find_db_config ( env_name ) &.configuration_hash
9
+
10
+ if ActiveRecord . version >= Gem ::Version . new ( '6.0' )
11
+ spec = ActiveRecord ::Base . configurations . find_db_config ( env_name ) . try ( :configuration_hash )
12
+ else
13
+ spec = ActiveRecord ::Base . configurations [ env_name ]
14
+ end
15
+
10
16
raise Error , "Standby target '#{ target } ' is invalid!" if spec . nil?
11
17
12
18
establish_connection spec
Original file line number Diff line number Diff line change 4
4
before do
5
5
# Backup connection and configs
6
6
@backup_conn = Standby . instance_variable_get :@standby_connections
7
- @backup_config = ActiveRecord ::Base . configurations . configs_for . map do |config |
8
- [ config . env_name , config . configuration_hash ]
9
- end . to_h
10
7
@backup_disabled = Standby . disabled
8
+
9
+ if ActiveRecord . version >= Gem ::Version . new ( '6.0' )
10
+ @backup_config = ActiveRecord ::Base . configurations . configs_for . map do |config |
11
+ [ config . env_name , config . configuration_hash ]
12
+ end . to_h
13
+ @empty_config = { }
14
+ else
15
+ @backup_config = ActiveRecord ::Base . configurations . dup
16
+ @empty_config = nil
17
+ end
18
+
11
19
@backup_conn . each_key do |klass_name |
12
20
Object . send ( :remove_const , klass_name ) if Object . const_defined? ( klass_name )
13
21
end
22
+
14
23
Standby . instance_variable_set :@standby_connections , { }
15
24
end
16
25
22
31
end
23
32
24
33
it 'raises error if standby configuration not specified' do
25
- ActiveRecord ::Base . configurations = @backup_config . merge ( { 'test_standby' => { } } )
34
+ ActiveRecord ::Base . configurations = @backup_config . merge ( { 'test_standby' => @empty_config } )
26
35
27
36
expect { Standby . on_standby { User . count } } . to raise_error ( Standby ::Error )
28
37
end
29
38
30
39
it 'connects to primary if standby configuration is disabled' do
31
- ActiveRecord ::Base . configurations = @backup_config . merge ( { 'test_standby' => { } } )
40
+ ActiveRecord ::Base . configurations = @backup_config . merge ( { 'test_standby' => @empty_config } )
32
41
Standby . disabled = true
33
42
34
43
expect ( Standby . on_standby { User . count } ) . to be 2
You can’t perform that action at this time.
0 commit comments