active_record_shards 3.19.0 → 3.19.3

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: 24b77105aa8df10b1e79313f01b54fe87dba293938c9d41dc103f41cac64ec84
4
- data.tar.gz: ec1da959a6dc2adbb6f287f73a3346c68fd49db30f7922dbc9adca7368532af2
3
+ metadata.gz: 7a4987e63e2554b3570cdc243b569b292ff6d9d7c0821ebeeb78768e244e8c93
4
+ data.tar.gz: fccba58bc0876d615dfbe62b4a14486dac3bf67eefd2614da23363ea52f7519b
5
5
  SHA512:
6
- metadata.gz: 5ccae049b9c4a450f04cc2ae6b0cfca9eba75ac72d5f795c0c49ae5fc19e37fad1341d14f3ba34f75c684696131586a663fb306bcd35c2dad9fc625ad36955f3
7
- data.tar.gz: 0b46d1cafa4fecae447a01106bfc4de592b7271fc17fef4c254a44a80cc22b22f5155e460d6aba036baad1a229f6f0b6935c3d108c935c5fd5cec9e6271eab83
6
+ metadata.gz: 9344f045cce08fad0308aa7767d3581e527fd2afd6897c9ae50461070e9e730c4a392ab9a9c47218cf5cc36f2d26afd0aadebea922bbd5f3c0724ada3a50c182
7
+ data.tar.gz: fc6f5e72ccb01955d028796c53d389a472d3157f05a1350a5c9c200714a48cc67ceb57f1def6d6e8be0fb5187ffb676cef855ea428f3c3042b6165a3ff72a2a5
@@ -41,6 +41,19 @@ module ActiveRecordShards
41
41
  conf
42
42
  end
43
43
 
44
+ def replace_slave_keys(conf)
45
+ conf.to_a.each do |env_name, env_config|
46
+ next unless env_name.end_with?("_slave")
47
+
48
+ replica_key = env_name.sub(/_slave$/, "_replica")
49
+ next if conf.key?(replica_key)
50
+
51
+ conf[replica_key] = env_config.deep_dup
52
+ end
53
+
54
+ conf
55
+ end
56
+
44
57
  def expand_child!(parent, child)
45
58
  parent.each do |key, value|
46
59
  unless ['slave', 'replica', 'shards'].include?(key) || value.is_a?(Hash)
@@ -50,7 +63,9 @@ module ActiveRecordShards
50
63
  end
51
64
 
52
65
  def configurations_with_shard_explosion=(conf)
53
- self.configurations_without_shard_explosion = explode(conf)
66
+ exploded_configuration = explode(conf)
67
+ configuration_with_slave_keys_replaced = replace_slave_keys(exploded_configuration)
68
+ self.configurations_without_shard_explosion = configuration_with_slave_keys_replaced
54
69
  end
55
70
 
56
71
  def self.extended(base)
@@ -4,7 +4,7 @@ module ActiveRecordShards
4
4
  name = current_shard_selection.resolve_connection_name(sharded: is_sharded?, configurations: configurations)
5
5
 
6
6
  unless configurations[name] || name == "primary"
7
- raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.keys.inspect})"
7
+ raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.to_h.keys.inspect})"
8
8
  end
9
9
 
10
10
  name
@@ -4,7 +4,7 @@ module ActiveRecordShards
4
4
  name = current_shard_selection.resolve_connection_name(sharded: is_sharded?, configurations: configurations)
5
5
 
6
6
  unless configurations[name] || name == "primary"
7
- raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.keys.inspect})"
7
+ raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.to_h.keys.inspect})"
8
8
  end
9
9
 
10
10
  name
@@ -0,0 +1,30 @@
1
+ module ActiveRecordShards
2
+ module ConnectionSwitcher
3
+ def connection_specification_name
4
+ name = current_shard_selection.resolve_connection_name(sharded: is_sharded?, configurations: configurations)
5
+
6
+ @_ars_connection_specification_names ||= {}
7
+ unless @_ars_connection_specification_names.include?(name)
8
+ unless configurations[name] || name == "primary"
9
+ raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.to_h.keys.inspect})"
10
+ end
11
+
12
+ @_ars_connection_specification_names[name] = true
13
+ end
14
+
15
+ name
16
+ end
17
+
18
+ private
19
+
20
+ def ensure_shard_connection
21
+ # See if we've connected before. If not, call `#establish_connection`
22
+ # so that ActiveRecord can resolve connection_specification_name to an
23
+ # ARS connection.
24
+ spec_name = connection_specification_name
25
+
26
+ pool = connection_handler.retrieve_connection_pool(spec_name)
27
+ connection_handler.establish_connection(spec_name.to_sym) if pool.nil?
28
+ end
29
+ end
30
+ end
@@ -153,18 +153,27 @@ module ActiveRecordShards
153
153
  end
154
154
 
155
155
  def shard_names
156
- unless config = configurations[shard_env]
157
- raise "Did not find #{shard_env} in configurations, did you forget to add it to your database config? (configurations: #{configurations.keys.inspect})"
158
- end
159
- unless config.fetch(SHARD_NAMES_CONFIG_KEY, []).all? { |shard_name| shard_name.is_a?(Integer) }
160
- raise "All shard names must be integers: #{config[SHARD_NAMES_CONFIG_KEY].inspect}."
156
+ unless config_for_env.fetch(SHARD_NAMES_CONFIG_KEY, []).all? { |shard_name| shard_name.is_a?(Integer) }
157
+ raise "All shard names must be integers: #{config_for_env[SHARD_NAMES_CONFIG_KEY].inspect}."
161
158
  end
162
159
 
163
- config[SHARD_NAMES_CONFIG_KEY] || []
160
+ config_for_env[SHARD_NAMES_CONFIG_KEY] || []
164
161
  end
165
162
 
166
163
  private
167
164
 
165
+ def config_for_env
166
+ @_ars_config_for_env ||= {}
167
+ @_ars_config_for_env[shard_env] ||= begin
168
+ unless config = configurations[shard_env]
169
+ raise "Did not find #{shard_env} in configurations, did you forget to add it to your database config? (configurations: #{configurations.to_h.keys.inspect})"
170
+ end
171
+
172
+ config
173
+ end
174
+ end
175
+ alias_method :check_config_for_env, :config_for_env
176
+
168
177
  def switch_connection(options)
169
178
  if options.any?
170
179
  if options.key?(:replica)
@@ -172,9 +181,7 @@ module ActiveRecordShards
172
181
  end
173
182
 
174
183
  if options.key?(:shard)
175
- unless configurations[shard_env]
176
- raise "Did not find #{shard_env} in configurations, did you forget to add it to your database config? (configurations: #{configurations.keys.inspect})"
177
- end
184
+ check_config_for_env
178
185
 
179
186
  current_shard_selection.shard = options[:shard]
180
187
  end
@@ -231,8 +238,10 @@ when '4.2'
231
238
  require 'active_record_shards/connection_switcher-4-2'
232
239
  when '5.0'
233
240
  require 'active_record_shards/connection_switcher-5-0'
234
- when '5.1', '5.2', '6.0'
241
+ when '5.1', '5.2'
235
242
  require 'active_record_shards/connection_switcher-5-1'
243
+ when '6.0'
244
+ require 'active_record_shards/connection_switcher-6-0'
236
245
  else
237
246
  raise "ActiveRecordShards is not compatible with #{ActiveRecord::VERSION::STRING}"
238
247
  end
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: 3.19.0
4
+ version: 3.19.3
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: 2021-07-09 00:00:00.000000000 Z
16
+ date: 2022-03-04 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: activerecord
@@ -201,6 +201,7 @@ files:
201
201
  - lib/active_record_shards/connection_switcher-4-2.rb
202
202
  - lib/active_record_shards/connection_switcher-5-0.rb
203
203
  - lib/active_record_shards/connection_switcher-5-1.rb
204
+ - lib/active_record_shards/connection_switcher-6-0.rb
204
205
  - lib/active_record_shards/connection_switcher.rb
205
206
  - lib/active_record_shards/default_replica_patches.rb
206
207
  - lib/active_record_shards/default_slave_patches.rb
@@ -232,7 +233,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
232
233
  - !ruby/object:Gem::Version
233
234
  version: '0'
234
235
  requirements: []
235
- rubygems_version: 3.2.16
236
+ rubygems_version: 3.3.1
236
237
  signing_key:
237
238
  specification_version: 4
238
239
  summary: Simple database switching for ActiveRecord.