active_record_shards 5.1.0 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/active_record_shards/connection_switcher-6-1.rb +31 -0
- data/lib/active_record_shards/connection_switcher-7-0.rb +31 -0
- data/lib/active_record_shards/connection_switcher.rb +16 -3
- data/lib/active_record_shards/shard_selection.rb +17 -3
- data/lib/active_record_shards/tasks.rb +35 -4
- data/lib/active_record_shards.rb +1 -1
- metadata +12 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15b0d33ae1fec065fc2c2763f52bf5385da900ddbc3d8b55089ac6f1f99f9b1c
|
4
|
+
data.tar.gz: 61de1ca1ad18611d9eb059dcec55eb85e4de55c965dd3ee33c60088a5d68f4f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90e52914b1a63fbfb633882f60f2c2020a057b7ad16530c5800cc44f2e88d551c638fcdc31615f2c1c13d4fd031148ce568a10d4776c0082b0c917e9c7ee8dba
|
7
|
+
data.tar.gz: 8b215a24c9c0e1ff79e75499b9d47976b0143095023c0ec2a1ab4c03cc5e9c048957ef63703fe2a8eef5b92c72b3d156a95662ac3367a67ec6324f9527142913
|
data/README.md
CHANGED
@@ -24,9 +24,9 @@ Rails 6.1 introduced new connection handling and support for sharding. Apps are
|
|
24
24
|
|
25
25
|
and make sure to require 'active\_record\_shards' in some way.
|
26
26
|
|
27
|
-
### Rails 6.1 installation
|
27
|
+
### Rails 6.1 & 7.0 installation
|
28
28
|
|
29
|
-
Rails 6.1
|
29
|
+
Rails 6.1 & 7.0 are **only** supported with `legacy_connection_handling` set to `true`.
|
30
30
|
|
31
31
|
Enable the legacy handling in your configuration files e.g. `config/application.rb` by setting:
|
32
32
|
|
@@ -0,0 +1,31 @@
|
|
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.configs_for(env_name: name, include_replicas: true).any? || name == "ActiveRecord::Base"
|
9
|
+
raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.configurations.map(&:env_name).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
|
+
|
28
|
+
connection_handler.establish_connection(spec_name.to_sym) if pool.nil?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,31 @@
|
|
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.configs_for(env_name: name, include_hidden: true).any? || name == "ActiveRecord::Base"
|
9
|
+
raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.configurations.map(&:env_name).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
|
+
|
28
|
+
connection_handler.establish_connection(spec_name.to_sym) if pool.nil?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -4,7 +4,8 @@ require 'active_record_shards/shard_support'
|
|
4
4
|
|
5
5
|
module ActiveRecordShards
|
6
6
|
module ConnectionSwitcher
|
7
|
-
|
7
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
8
|
+
when '6.1', '7.0'
|
8
9
|
SHARD_NAMES_CONFIG_KEY = :shard_names
|
9
10
|
else
|
10
11
|
SHARD_NAMES_CONFIG_KEY = 'shard_names'
|
@@ -144,7 +145,15 @@ module ActiveRecordShards
|
|
144
145
|
def config_for_env
|
145
146
|
@_ars_config_for_env ||= {}
|
146
147
|
@_ars_config_for_env[shard_env] ||= begin
|
147
|
-
|
148
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
149
|
+
when '7.0'
|
150
|
+
config = configurations.configs_for(env_name: shard_env, include_hidden: true).first.configuration_hash
|
151
|
+
when '6.1'
|
152
|
+
config = configurations.configs_for(env_name: shard_env, include_replicas: true).first.configuration_hash
|
153
|
+
else
|
154
|
+
config = configurations[shard_env]
|
155
|
+
end
|
156
|
+
unless config
|
148
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})"
|
149
158
|
end
|
150
159
|
|
@@ -208,8 +217,12 @@ end
|
|
208
217
|
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
209
218
|
when '5.1', '5.2'
|
210
219
|
require 'active_record_shards/connection_switcher-5-1'
|
211
|
-
when '6.0'
|
220
|
+
when '6.0'
|
212
221
|
require 'active_record_shards/connection_switcher-6-0'
|
222
|
+
when '6.1'
|
223
|
+
require 'active_record_shards/connection_switcher-6-1'
|
224
|
+
when '7.0'
|
225
|
+
require 'active_record_shards/connection_switcher-7-0'
|
213
226
|
else
|
214
227
|
raise "ActiveRecordShards is not compatible with #{ActiveRecord::VERSION::STRING}"
|
215
228
|
end
|
@@ -17,8 +17,12 @@ module ActiveRecordShards
|
|
17
17
|
@shard || self.class.ars_default_shard
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
21
|
-
|
20
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
21
|
+
when '6.1', '7.0'
|
22
|
+
PRIMARY = "ActiveRecord::Base"
|
23
|
+
else
|
24
|
+
PRIMARY = "primary"
|
25
|
+
end
|
22
26
|
def resolve_connection_name(sharded:, configurations:)
|
23
27
|
resolved_shard = sharded ? shard : nil
|
24
28
|
env = ActiveRecordShards.app_env
|
@@ -29,7 +33,17 @@ module ActiveRecordShards
|
|
29
33
|
@connection_names[env][resolved_shard][@on_replica] ||= begin
|
30
34
|
name = env.dup
|
31
35
|
name << "_shard_#{resolved_shard}" if resolved_shard
|
32
|
-
|
36
|
+
replica_config = begin
|
37
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
38
|
+
when '7.0'
|
39
|
+
configurations.configs_for(env_name: "#{name}_replica", include_hidden: true).any?
|
40
|
+
when '6.1'
|
41
|
+
configurations.configs_for(env_name: "#{name}_replica", include_replicas: true).any?
|
42
|
+
else
|
43
|
+
configurations["#{name}_replica"]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
if @on_replica && replica_config
|
33
47
|
"#{name}_replica"
|
34
48
|
else
|
35
49
|
# ActiveRecord always names its default connection pool 'primary'
|
@@ -9,10 +9,24 @@ end
|
|
9
9
|
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
|
+
configurations = begin
|
13
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
14
|
+
when '6.1', '7.0'
|
15
|
+
ActiveRecord::Base.configurations.configurations.map { |configuration| [configuration.env_name, configuration.configuration_hash] }
|
16
|
+
else
|
17
|
+
ActiveRecord::Base.configurations.to_h
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
configurations.each do |key, conf|
|
13
22
|
next if !key.start_with?(ActiveRecordShards.app_env) || key.end_with?("_replica")
|
14
23
|
|
15
24
|
begin
|
25
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
26
|
+
when '6.1', '7.0'
|
27
|
+
conf = conf.stringify_keys
|
28
|
+
end
|
29
|
+
|
16
30
|
ActiveRecordShards::Tasks.root_connection(conf).drop_database(conf['database'])
|
17
31
|
# rescue ActiveRecord::NoDatabaseError # TODO: exists in AR but never is raised here ...
|
18
32
|
# $stderr.puts "Database '#{conf['database']}' does not exist"
|
@@ -30,13 +44,26 @@ namespace :db do
|
|
30
44
|
|
31
45
|
desc "Create the database defined in config/database.yml for the current RAILS_ENV including shards"
|
32
46
|
task create: :load_config do
|
33
|
-
|
47
|
+
configurations = begin
|
48
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
49
|
+
when '6.1', '7.0'
|
50
|
+
ActiveRecord::Base.configurations.configurations.map { |configuration| [configuration.env_name, configuration.configuration_hash] }
|
51
|
+
else
|
52
|
+
ActiveRecord::Base.configurations.to_h
|
53
|
+
end
|
54
|
+
end
|
55
|
+
configurations.each do |key, conf|
|
34
56
|
next if !key.start_with?(ActiveRecordShards.app_env) || key.end_with?("_replica")
|
35
57
|
|
36
58
|
begin
|
37
59
|
# MysqlAdapter takes charset instead of encoding in Rails 4.2 or greater
|
38
60
|
# https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/tasks/mysql_database_tasks.rb#L85-L96
|
39
61
|
symbolized_configuration = conf.symbolize_keys
|
62
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
63
|
+
when '6.1', '7.0'
|
64
|
+
conf = conf.stringify_keys
|
65
|
+
end
|
66
|
+
|
40
67
|
symbolized_configuration[:charset] = symbolized_configuration[:encoding]
|
41
68
|
|
42
69
|
ActiveRecordShards::Tasks.root_connection(conf).create_database(conf['database'], symbolized_configuration)
|
@@ -94,7 +121,8 @@ module ActiveRecordShards
|
|
94
121
|
def root_connection(conf)
|
95
122
|
conf = conf.merge('database' => nil)
|
96
123
|
spec = spec_for(conf)
|
97
|
-
|
124
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
125
|
+
when '6.1', '7.0'
|
98
126
|
ActiveRecord::Base.send("#{conf['adapter']}_connection", spec.db_config.configuration_hash)
|
99
127
|
else
|
100
128
|
ActiveRecord::Base.send("#{conf['adapter']}_connection", spec.config)
|
@@ -104,7 +132,10 @@ module ActiveRecordShards
|
|
104
132
|
private
|
105
133
|
|
106
134
|
def spec_for(conf)
|
107
|
-
|
135
|
+
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
136
|
+
when '7.0'
|
137
|
+
ActiveRecord::Base.connection_handler.send(:resolve_pool_config, conf, ActiveRecord::Base, ActiveRecord::Base.current_role, ActiveRecord::Base.current_shard)
|
138
|
+
when '6.1'
|
108
139
|
ActiveRecord::Base.connection_handler.send(:resolve_pool_config, conf, ActiveRecord::Base)
|
109
140
|
else
|
110
141
|
resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(ActiveRecord::Base.configurations)
|
data/lib/active_record_shards.rb
CHANGED
@@ -62,7 +62,7 @@ when '5.2'
|
|
62
62
|
|
63
63
|
# https://github.com/rails/rails/blob/v5.2.6/activerecord/lib/active_record/associations/preloader/association.rb#L96
|
64
64
|
ActiveRecord::Associations::Preloader::Association.prepend(ActiveRecordShards::DefaultReplicaPatches::AssociationsPreloaderAssociationLoadRecordsPatch)
|
65
|
-
when '6.0', '6.1'
|
65
|
+
when '6.0', '6.1', '7.0'
|
66
66
|
# https://github.com/rails/rails/blob/v6.0.4/activerecord/lib/active_record/type_caster/connection.rb#L28
|
67
67
|
ActiveRecord::TypeCaster::Connection.prepend(ActiveRecordShards::DefaultReplicaPatches::TypeCasterConnectionConnectionPatch)
|
68
68
|
|
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.
|
4
|
+
version: 5.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Quorning
|
@@ -10,10 +10,10 @@ authors:
|
|
10
10
|
- Mick Staugaard
|
11
11
|
- Eric Chapweske
|
12
12
|
- Ben Osheroff
|
13
|
-
autorequire:
|
13
|
+
autorequire:
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
|
-
date: 2022-
|
16
|
+
date: 2022-10-11 00:00:00.000000000 Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: activerecord
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
version: '5.1'
|
25
25
|
- - "<"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: '7.
|
27
|
+
version: '7.1'
|
28
28
|
type: :runtime
|
29
29
|
prerelease: false
|
30
30
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -34,7 +34,7 @@ dependencies:
|
|
34
34
|
version: '5.1'
|
35
35
|
- - "<"
|
36
36
|
- !ruby/object:Gem::Version
|
37
|
-
version: '7.
|
37
|
+
version: '7.1'
|
38
38
|
- !ruby/object:Gem::Dependency
|
39
39
|
name: activesupport
|
40
40
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,7 +44,7 @@ dependencies:
|
|
44
44
|
version: '5.1'
|
45
45
|
- - "<"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '7.
|
47
|
+
version: '7.1'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '5.1'
|
55
55
|
- - "<"
|
56
56
|
- !ruby/object:Gem::Version
|
57
|
-
version: '7.
|
57
|
+
version: '7.1'
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: bump
|
60
60
|
requirement: !ruby/object:Gem::Requirement
|
@@ -197,6 +197,8 @@ files:
|
|
197
197
|
- lib/active_record_shards/configuration_parser.rb
|
198
198
|
- lib/active_record_shards/connection_switcher-5-1.rb
|
199
199
|
- lib/active_record_shards/connection_switcher-6-0.rb
|
200
|
+
- lib/active_record_shards/connection_switcher-6-1.rb
|
201
|
+
- lib/active_record_shards/connection_switcher-7-0.rb
|
200
202
|
- lib/active_record_shards/connection_switcher.rb
|
201
203
|
- lib/active_record_shards/default_replica_patches.rb
|
202
204
|
- lib/active_record_shards/default_shard.rb
|
@@ -211,7 +213,7 @@ homepage: https://github.com/zendesk/active_record_shards
|
|
211
213
|
licenses:
|
212
214
|
- MIT
|
213
215
|
metadata: {}
|
214
|
-
post_install_message:
|
216
|
+
post_install_message:
|
215
217
|
rdoc_options: []
|
216
218
|
require_paths:
|
217
219
|
- lib
|
@@ -226,8 +228,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
226
228
|
- !ruby/object:Gem::Version
|
227
229
|
version: '0'
|
228
230
|
requirements: []
|
229
|
-
rubygems_version: 3.1
|
230
|
-
signing_key:
|
231
|
+
rubygems_version: 3.0.3.1
|
232
|
+
signing_key:
|
231
233
|
specification_version: 4
|
232
234
|
summary: Simple database switching for ActiveRecord.
|
233
235
|
test_files: []
|