active_record_shards 3.19.1 → 3.20.0

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: 391c29fd84c7b17ac74855b2397903b512648796a6212feebff5cf6984be78a8
4
- data.tar.gz: cce74b546554fd8994ec90ee69fa3aef6a259c6c2aaec193834fd082da9668bc
3
+ metadata.gz: faa823b7357f66f7de85bb3ab50b6df99beb0c2505bcbbeafac9cc1aa4581d7e
4
+ data.tar.gz: 688c6ed703809a88ee86134d43021730495bcaaa8ca2132c81a0926edc2d1eca
5
5
  SHA512:
6
- metadata.gz: 033e74714876add1c2e89c479b44bc23374371410c5f961967ed2dc58719f8bae2eb9212e8ed87af4294380beb72dcc13cfab92acdedb34147a62411115cdb00
7
- data.tar.gz: 2f217fb5098b08417973c8430d3cc46715203a7402060d0980148a3f492f01f1b95d7fdcfa4a30b6a31b6643b0dd21d037b40a239863df64c19515f26d27417f
6
+ metadata.gz: 8e9babc77b5dffe78007005f7abec607a9e5597aafa3676082c501da68c9cce9748714db5068bdc6b7573edef1ed8ad608893820c5396fbc450fa59b5c34f9f2
7
+ data.tar.gz: 72dcbaa612143fe7a3235f23982b83b4fddb86a7c39814dec128d7fc6f4a141b368de82392e2f515424055b148d11181fc3fd27e608f174879735c30d1d13ce4
@@ -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)
@@ -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.to_h.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.to_h.keys.inspect})"
177
- end
184
+ check_config_for_env
178
185
 
179
186
  current_shard_selection.shard = options[:shard]
180
187
  end
@@ -184,7 +191,7 @@ module ActiveRecordShards
184
191
  end
185
192
 
186
193
  def shard_env
187
- ActiveRecordShards.rails_env
194
+ ActiveRecordShards.app_env
188
195
  end
189
196
 
190
197
  # Make these few schema related methods available before having switched to
@@ -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
@@ -26,11 +26,11 @@ module ActiveRecordShards
26
26
  the_shard = shard(klass)
27
27
 
28
28
  @shard_names ||= {}
29
- @shard_names[ActiveRecordShards.rails_env] ||= {}
30
- @shard_names[ActiveRecordShards.rails_env][the_shard] ||= {}
31
- @shard_names[ActiveRecordShards.rails_env][the_shard][try_replica] ||= {}
32
- @shard_names[ActiveRecordShards.rails_env][the_shard][try_replica][@on_replica] ||= begin
33
- s = ActiveRecordShards.rails_env.dup
29
+ @shard_names[ActiveRecordShards.app_env] ||= {}
30
+ @shard_names[ActiveRecordShards.app_env][the_shard] ||= {}
31
+ @shard_names[ActiveRecordShards.app_env][the_shard][try_replica] ||= {}
32
+ @shard_names[ActiveRecordShards.app_env][the_shard][try_replica][@on_replica] ||= begin
33
+ s = ActiveRecordShards.app_env.dup
34
34
  s << "_shard_#{the_shard}" if the_shard
35
35
  s << "_replica" if @on_replica && try_replica
36
36
  s
@@ -50,7 +50,7 @@ module ActiveRecordShards
50
50
  PRIMARY = "primary"
51
51
  def resolve_connection_name(sharded:, configurations:)
52
52
  resolved_shard = sharded ? shard : nil
53
- env = ActiveRecordShards.rails_env
53
+ env = ActiveRecordShards.app_env
54
54
 
55
55
  @connection_names ||= {}
56
56
  @connection_names[env] ||= {}
@@ -10,7 +10,7 @@ namespace :db do
10
10
  desc 'Drops the database for the current RAILS_ENV including shards'
11
11
  task drop: :load_config do
12
12
  ActiveRecord::Base.configurations.to_h.each do |key, conf|
13
- next if !key.start_with?(ActiveRecordShards.rails_env) || key.end_with?("_replica", "_slave")
13
+ next if !key.start_with?(ActiveRecordShards.app_env) || key.end_with?("_replica", "_slave")
14
14
 
15
15
  begin
16
16
  ActiveRecordShards::Tasks.root_connection(conf).drop_database(conf['database'])
@@ -31,7 +31,7 @@ namespace :db do
31
31
  desc "Create the database defined in config/database.yml for the current RAILS_ENV including shards"
32
32
  task create: :load_config do
33
33
  ActiveRecord::Base.configurations.to_h.each do |key, conf|
34
- next if !key.start_with?(ActiveRecordShards.rails_env) || key.end_with?("_replica", "_slave")
34
+ next if !key.start_with?(ActiveRecordShards.app_env) || key.end_with?("_replica", "_slave")
35
35
 
36
36
  begin
37
37
  # MysqlAdapter takes charset instead of encoding in Rails 4.2 or greater
@@ -48,7 +48,7 @@ namespace :db do
48
48
  end
49
49
  end
50
50
  end
51
- ActiveRecord::Base.establish_connection(ActiveRecordShards.rails_env.to_sym)
51
+ ActiveRecord::Base.establish_connection(ActiveRecordShards.app_env.to_sym)
52
52
  end
53
53
 
54
54
  desc "Raises an error if there are pending migrations"
@@ -12,10 +12,12 @@ require 'active_record_shards/default_replica_patches'
12
12
  require 'active_record_shards/schema_dumper_extension'
13
13
 
14
14
  module ActiveRecordShards
15
- def self.rails_env
15
+ def self.app_env
16
16
  env = Rails.env if defined?(Rails.env)
17
17
  env ||= RAILS_ENV if Object.const_defined?(:RAILS_ENV)
18
18
  env ||= ENV['RAILS_ENV']
19
+ env ||= APP_ENV if Object.const_defined?(:APP_ENV)
20
+ env ||= ENV['APP_ENV']
19
21
  env || 'development'
20
22
  end
21
23
  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.1
4
+ version: 3.20.0
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-09-23 00:00:00.000000000 Z
16
+ date: 2022-04-23 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.