active_record_shards 5.3.0 → 5.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 71d3927981244ea33e21fd09e315e4beef0427eb4dae56011484626ffb224346
4
- data.tar.gz: 5596dbc4576f6cd9cd6ad3e1547ab8ff07d5fb33d3ee0688495fac14a0c50343
3
+ metadata.gz: 3d54a5e8446f89d23e9c302763e48033f2398a72495d150772ea4bc3ec4fe575
4
+ data.tar.gz: 9add02d3fec821e54c6f388a021cb09f69320de09846242e49fa2f26269ce948
5
5
  SHA512:
6
- metadata.gz: f6b430fc35baf8da887402d75e906cae36c2272e5e76076ee553f833423caf4059089298e2b4b002e826d99287fa5f31f69e9ed3d580370f1a9900c6dd0019de
7
- data.tar.gz: f5bf02670f7ace64c734d3a5ef8e008395067b23451b61ea55a2bfb2b207c4ea20bc5225d7dd8230bbd73b223639079bdf3e0e7267ff1d94636cf6dc68702d48
6
+ metadata.gz: 27627d24f879c675af2d414c334fd713f9845f2e7d93dd8e6c580194357bba038f5c1ded593be06702a570e7631beeb92e5c2be8e26d8629b5ef84b3333e4b47
7
+ data.tar.gz: d47b1b6906e5ff40633ebe6128fdc845cac7b81d377a16fd31ece96398a174fe87f8bd2a33406abe3516f53969ab5c910e163a792fb0f93e2e6119c8dfc15267
@@ -4,6 +4,8 @@ require 'active_record_shards/shard_support'
4
4
 
5
5
  module ActiveRecordShards
6
6
  module ConnectionSwitcher
7
+ class LegacyConnectionHandlingError < StandardError; end
8
+
7
9
  case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
8
10
  when '6.1', '7.0'
9
11
  SHARD_NAMES_CONFIG_KEY = :shard_names
@@ -140,10 +142,6 @@ module ActiveRecordShards
140
142
  end
141
143
 
142
144
  def shard_names
143
- unless config_for_env.fetch(SHARD_NAMES_CONFIG_KEY, []).all? { |shard_name| shard_name.is_a?(Integer) }
144
- raise "All shard names must be integers: #{config_for_env[SHARD_NAMES_CONFIG_KEY].inspect}."
145
- end
146
-
147
145
  config_for_env[SHARD_NAMES_CONFIG_KEY] || []
148
146
  end
149
147
 
@@ -164,12 +162,22 @@ module ActiveRecordShards
164
162
  raise "Did not find #{shard_env} in configurations, did you forget to add it to your database config? (configurations: #{configurations.to_h.keys.inspect})"
165
163
  end
166
164
 
165
+ ensure_all_shard_names_are_integers(config)
166
+
167
167
  config
168
168
  end
169
169
  end
170
170
  alias_method :check_config_for_env, :config_for_env
171
171
 
172
+ def ensure_all_shard_names_are_integers(config)
173
+ unless config.fetch(SHARD_NAMES_CONFIG_KEY, []).all? { |shard_name| shard_name.is_a?(Integer) }
174
+ raise "All shard names must be integers: #{config.inspect}."
175
+ end
176
+ end
177
+
172
178
  def switch_connection(options)
179
+ ensure_legacy_connection_handling if ActiveRecord.version >= Gem::Version.new('6.1')
180
+
173
181
  if options.any?
174
182
  if options.key?(:replica)
175
183
  current_shard_selection.on_replica = options[:replica]
@@ -185,6 +193,22 @@ module ActiveRecordShards
185
193
  end
186
194
  end
187
195
 
196
+ def ensure_legacy_connection_handling
197
+ unless legacy_connection_handling_owner.legacy_connection_handling
198
+ raise LegacyConnectionHandlingError, "ActiveRecordShards is _only_ compatible with ActiveRecord `legacy_connection_handling` set to `true`."
199
+ end
200
+ end
201
+
202
+ def legacy_connection_handling_owner
203
+ @legacy_connection_handling_owner ||=
204
+ case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
205
+ when '7.0'
206
+ ActiveRecord
207
+ when '6.1'
208
+ ActiveRecord::Base
209
+ end
210
+ end
211
+
188
212
  def shard_env
189
213
  ActiveRecordShards.app_env
190
214
  end
@@ -11,17 +11,26 @@ module ActiveRecordShards
11
11
  end
12
12
 
13
13
  def is_sharded? # rubocop:disable Naming/PredicateName
14
- if self == ActiveRecord::Base
15
- sharded != false && supports_sharding?
16
- elsif self == base_class
17
- if sharded.nil?
18
- ActiveRecord::Base.is_sharded?
19
- else
20
- sharded != false
21
- end
22
- else
23
- base_class.is_sharded?
24
- end
14
+ # "sharded" here means self.sharded, but actually writing "self.sharded"
15
+ # doesn't work until Ruby 2.7 (and this gem currently supports 2.6) because
16
+ # the sharded attr_accessor is private. Private methods must be called without
17
+ # a receiver, but Ruby 2.7+ does allow an explicit "self" as a receiver.
18
+ return sharded unless sharded.nil?
19
+
20
+ # Despite self.sharded not working, self.sharded= _DOES_ work. That's an exception
21
+ # to the "private methods must be called with no receiver" rule (presumably
22
+ # because it would otherwise be ambiguous with local variable assignment).
23
+ self.sharded = if self == ActiveRecord::Base
24
+ sharded != false && supports_sharding?
25
+ elsif self == base_class
26
+ if sharded.nil?
27
+ ActiveRecord::Base.is_sharded?
28
+ else
29
+ sharded != false
30
+ end
31
+ else
32
+ base_class.is_sharded?
33
+ end
25
34
  end
26
35
 
27
36
  def on_replica_by_default?
@@ -18,12 +18,14 @@ module ActiveRecordShards
18
18
  end
19
19
 
20
20
  def self.app_env
21
- env = Rails.env if defined?(Rails.env)
22
- env ||= RAILS_ENV if Object.const_defined?(:RAILS_ENV)
23
- env ||= ENV['RAILS_ENV']
24
- env ||= APP_ENV if Object.const_defined?(:APP_ENV)
25
- env ||= ENV['APP_ENV']
26
- env || 'development'
21
+ @app_env ||= begin
22
+ env = Rails.env if defined?(Rails.env)
23
+ env ||= RAILS_ENV if Object.const_defined?(:RAILS_ENV)
24
+ env ||= ENV['RAILS_ENV']
25
+ env ||= APP_ENV if Object.const_defined?(:APP_ENV)
26
+ env ||= ENV['APP_ENV']
27
+ env || 'development'
28
+ end
27
29
  end
28
30
  end
29
31
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_shards
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.3.0
4
+ version: 5.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Quorning
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2022-10-29 00:00:00.000000000 Z
16
+ date: 2023-04-06 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: activerecord
@@ -97,20 +97,6 @@ dependencies:
97
97
  - - ">="
98
98
  - !ruby/object:Gem::Version
99
99
  version: '0'
100
- - !ruby/object:Gem::Dependency
101
- name: mocha
102
- requirement: !ruby/object:Gem::Requirement
103
- requirements:
104
- - - ">="
105
- - !ruby/object:Gem::Version
106
- version: 1.4.0
107
- type: :development
108
- prerelease: false
109
- version_requirements: !ruby/object:Gem::Requirement
110
- requirements:
111
- - - ">="
112
- - !ruby/object:Gem::Version
113
- version: 1.4.0
114
100
  - !ruby/object:Gem::Dependency
115
101
  name: mysql2
116
102
  requirement: !ruby/object:Gem::Requirement