active_record_shards 3.19.2 → 3.21.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/active_record_shards/connection_switcher-6-0.rb +30 -0
- data/lib/active_record_shards/connection_switcher.rb +21 -12
- data/lib/active_record_shards/shard_selection.rb +6 -6
- data/lib/active_record_shards/tasks.rb +3 -3
- data/lib/active_record_shards.rb +7 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aadf0c3e7681201dfc6b2f9ff1c9a33164d7e9b17394052e09601f807926c73a
|
4
|
+
data.tar.gz: 3e1eaac98dbcdeb6133787447fdd4a7c56aa4056013e9b01aa2082f00a6bc733
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eecd43504f6dcb074c97128d96a2b5bbeeea08bc92e6c005bc3b8d3952670ea7a201856709b50c0ad5e4d91db1eb9aac9bba02762a424eaa3a23bb44c00fbdbd
|
7
|
+
data.tar.gz: 1f91bc255828d9bae15bf5267a5a836049e12eef663751dbad63de1f35e84cea566f071f290db96f84accc4bb20c2d52c82cbedbe79a6bc637769cae7aeafa75
|
data/README.md
CHANGED
@@ -148,12 +148,16 @@ ActiveRecord::Base.on_replica do
|
|
148
148
|
end
|
149
149
|
```
|
150
150
|
|
151
|
-
This will perform the query on the replica, and mark the returned instances as read
|
151
|
+
This will perform the query on the replica, and mark the returned instances as read-only. There is also a shortcut for this:
|
152
152
|
|
153
153
|
```ruby
|
154
154
|
Account.on_replica.find_by_big_expensive_query
|
155
155
|
```
|
156
156
|
|
157
|
+
If you do not want instances returned from replicas to be marked as read-only, this can be disabled globally:
|
158
|
+
|
159
|
+
`ActiveRecordShards.disable_replica_readonly_records = true`
|
160
|
+
|
157
161
|
## Debugging
|
158
162
|
|
159
163
|
Show if a query went to primary or replica in the logs:
|
@@ -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
|
@@ -125,7 +125,7 @@ module ActiveRecordShards
|
|
125
125
|
|
126
126
|
# we avoid_readonly_scope to prevent some stack overflow problems, like when
|
127
127
|
# .columns calls .with_scope which calls .columns and onward, endlessly.
|
128
|
-
if self == ActiveRecord::Base || !switch_to_replica || construct_ro_scope == false
|
128
|
+
if self == ActiveRecord::Base || !switch_to_replica || construct_ro_scope == false || ActiveRecordShards.disable_replica_readonly_records == true
|
129
129
|
yield
|
130
130
|
else
|
131
131
|
readonly.scoping(&block)
|
@@ -153,18 +153,27 @@ module ActiveRecordShards
|
|
153
153
|
end
|
154
154
|
|
155
155
|
def shard_names
|
156
|
-
unless
|
157
|
-
raise "
|
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
|
-
|
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
|
-
|
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.
|
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'
|
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.
|
30
|
-
@shard_names[ActiveRecordShards.
|
31
|
-
@shard_names[ActiveRecordShards.
|
32
|
-
@shard_names[ActiveRecordShards.
|
33
|
-
s = ActiveRecordShards.
|
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.
|
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.
|
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.
|
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.
|
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"
|
data/lib/active_record_shards.rb
CHANGED
@@ -12,10 +12,16 @@ require 'active_record_shards/default_replica_patches'
|
|
12
12
|
require 'active_record_shards/schema_dumper_extension'
|
13
13
|
|
14
14
|
module ActiveRecordShards
|
15
|
-
|
15
|
+
class << self
|
16
|
+
attr_accessor :disable_replica_readonly_records
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.app_env
|
16
20
|
env = Rails.env if defined?(Rails.env)
|
17
21
|
env ||= RAILS_ENV if Object.const_defined?(:RAILS_ENV)
|
18
22
|
env ||= ENV['RAILS_ENV']
|
23
|
+
env ||= APP_ENV if Object.const_defined?(:APP_ENV)
|
24
|
+
env ||= ENV['APP_ENV']
|
19
25
|
env || 'development'
|
20
26
|
end
|
21
27
|
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.
|
4
|
+
version: 3.21.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: 2022-
|
16
|
+
date: 2022-07-05 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.
|
236
|
+
rubygems_version: 3.1.6
|
236
237
|
signing_key:
|
237
238
|
specification_version: 4
|
238
239
|
summary: Simple database switching for ActiveRecord.
|