active_record_shards 3.14.0 → 3.15.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 +1 -1
- data/lib/active_record_shards.rb +4 -3
- data/lib/active_record_shards/connection_handler.rb +2 -8
- data/lib/active_record_shards/connection_specification.rb +6 -16
- data/lib/active_record_shards/connection_switcher-4-0.rb +6 -21
- data/lib/active_record_shards/connection_switcher.rb +6 -16
- data/lib/active_record_shards/default_slave_patches.rb +1 -18
- data/lib/active_record_shards/migration.rb +1 -5
- data/lib/active_record_shards/patches-4-2.rb +0 -5
- data/lib/active_record_shards/tasks.rb +5 -12
- metadata +6 -8
- data/lib/active_record_shards/patches-3-2.rb +0 -11
- data/lib/active_record_shards/patches-5-0.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 894803fd2a019a4e502a051edfe37f9d98e91573488220e634c2cce40eaeea3a
|
4
|
+
data.tar.gz: 5c0ef8cd58a09d72997f967980660e02752945ae02a1bc471cdc62d0a2c03055
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68a350aebd431856ccaf50578a7733ade59a70feef0cc65916dd4140923ec50c807c07cadb753acbd44f1af7a41aba99b87d97a5d3926c729617e039a5a79a16
|
7
|
+
data.tar.gz: 0d08e0aa6eccce4763729e1685965d2367db2244eb1d72a0a9b175f0d3483cb2975f548791568eb61fa5d75f51a76b6c3df8e967d34f246b5e54f57b7d3d9c28
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
ActiveRecord Shards is an extension for ActiveRecord that provides support for sharded database and slaves. Basically it is just a nice way to
|
6
6
|
switch between database connections. We've made the implementation very small, and have tried not to reinvent any wheels already present in ActiveRecord.
|
7
7
|
|
8
|
-
ActiveRecord Shards has been used and tested on Rails
|
8
|
+
ActiveRecord Shards has been used and tested on Rails 4.2 and 5.0 and has in some form or another been used in production on a large Rails app for several years.
|
9
9
|
|
10
10
|
- [Installation](#installation)
|
11
11
|
- [Configuration](#configuration)
|
data/lib/active_record_shards.rb
CHANGED
@@ -8,6 +8,7 @@ require 'active_record_shards/connection_switcher'
|
|
8
8
|
require 'active_record_shards/association_collection_connection_selection'
|
9
9
|
require 'active_record_shards/migration'
|
10
10
|
require 'active_record_shards/default_slave_patches'
|
11
|
+
require 'active_record_shards/schema_dumper_extension'
|
11
12
|
|
12
13
|
module ActiveRecordShards
|
13
14
|
def self.rails_env
|
@@ -24,14 +25,14 @@ ActiveRecord::Base.extend(ActiveRecordShards::ConnectionSwitcher)
|
|
24
25
|
ActiveRecord::Base.extend(ActiveRecordShards::DefaultSlavePatches)
|
25
26
|
ActiveRecord::Relation.include(ActiveRecordShards::DefaultSlavePatches::ActiveRelationPatches)
|
26
27
|
ActiveRecord::Associations::CollectionProxy.include(ActiveRecordShards::AssociationCollectionConnectionSelection)
|
28
|
+
ActiveRecord::Associations::Builder::HasAndBelongsToMany.include(ActiveRecordShards::DefaultSlavePatches::Rails41HasAndBelongsToManyBuilderExtension)
|
29
|
+
ActiveRecord::SchemaDumper.prepend(ActiveRecordShards::SchemaDumperExtension)
|
27
30
|
|
28
31
|
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
29
|
-
when '3.2'
|
30
|
-
require 'active_record_shards/patches-3-2'
|
31
32
|
when '4.2'
|
32
33
|
require 'active_record_shards/patches-4-2'
|
33
34
|
when '5.0', '5.1', '5.2'
|
34
|
-
|
35
|
+
:ok
|
35
36
|
else
|
36
37
|
raise "ActiveRecordShards is not compatible with #{ActiveRecord::VERSION::STRING}"
|
37
38
|
end
|
@@ -1,13 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
ActiveRecord::ConnectionAdapters::ConnectionHandler.class_eval do
|
3
3
|
remove_method :retrieve_connection_pool
|
4
|
-
|
5
|
-
|
6
|
-
class_to_pool[klass.connection_pool_name] ||= pool_for(klass)
|
7
|
-
end
|
8
|
-
else
|
9
|
-
def retrieve_connection_pool(klass)
|
10
|
-
(@class_to_pool || @connection_pools)[klass.connection_pool_name]
|
11
|
-
end
|
4
|
+
def retrieve_connection_pool(klass)
|
5
|
+
class_to_pool[klass.connection_pool_name] ||= pool_for(klass)
|
12
6
|
end
|
13
7
|
end
|
@@ -1,16 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
class << ActiveRecord::Base
|
3
|
-
remove_method :establish_connection
|
3
|
+
remove_method :establish_connection if ActiveRecord::VERSION::MAJOR >= 5
|
4
4
|
def establish_connection(spec = ENV["DATABASE_URL"])
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
spec = resolver.spec(spec)
|
10
|
-
else
|
11
|
-
resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new spec, configurations
|
12
|
-
spec = resolver.spec
|
13
|
-
end
|
5
|
+
spec ||= ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
|
6
|
+
spec = spec.to_sym if spec.is_a?(String)
|
7
|
+
resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new configurations
|
8
|
+
spec = resolver.spec(spec)
|
14
9
|
|
15
10
|
unless respond_to?(spec.adapter_method)
|
16
11
|
raise AdapterNotFound, "database configuration specifies nonexistent #{spec.config[:adapter]} adapter"
|
@@ -18,11 +13,6 @@ class << ActiveRecord::Base
|
|
18
13
|
|
19
14
|
remove_connection
|
20
15
|
specification_cache[connection_pool_name] = spec
|
21
|
-
|
22
|
-
if ActiveRecord::VERSION::MAJOR >= 4
|
23
|
-
connection_handler.establish_connection self, spec
|
24
|
-
else
|
25
|
-
connection_handler.establish_connection connection_pool_name, spec
|
26
|
-
end
|
16
|
+
connection_handler.establish_connection self, spec
|
27
17
|
end
|
28
18
|
end
|
@@ -25,23 +25,12 @@ module ActiveRecordShards
|
|
25
25
|
raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.keys.inspect})"
|
26
26
|
end
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
specification_cache[name] ||= begin
|
32
|
-
resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new configurations
|
33
|
-
resolver.spec(spec)
|
34
|
-
end
|
35
|
-
|
36
|
-
connection_handler.establish_connection(self, specification_cache[name])
|
37
|
-
else
|
38
|
-
specification_cache[name] ||= begin
|
39
|
-
resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new spec, configurations
|
40
|
-
resolver.spec
|
41
|
-
end
|
42
|
-
|
43
|
-
connection_handler.establish_connection(connection_pool_name, specification_cache[name])
|
28
|
+
specification_cache[name] ||= begin
|
29
|
+
resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new configurations
|
30
|
+
resolver.spec(spec)
|
44
31
|
end
|
32
|
+
|
33
|
+
connection_handler.establish_connection(self, specification_cache[name])
|
45
34
|
end
|
46
35
|
|
47
36
|
def specification_cache
|
@@ -58,11 +47,7 @@ module ActiveRecordShards
|
|
58
47
|
end
|
59
48
|
|
60
49
|
def connected_to_shard?
|
61
|
-
|
62
|
-
specs_to_pools = Hash[connection_handler.connection_pool_list.map { |pool| [pool.spec, pool] }]
|
63
|
-
else
|
64
|
-
specs_to_pools = connection_handler.connection_pools
|
65
|
-
end
|
50
|
+
specs_to_pools = Hash[connection_handler.connection_pool_list.map { |pool| [pool.spec, pool] }]
|
66
51
|
|
67
52
|
specs_to_pools.key?(connection_pool_key)
|
68
53
|
end
|
@@ -170,21 +170,11 @@ module ActiveRecordShards
|
|
170
170
|
ActiveRecordShards.rails_env
|
171
171
|
end
|
172
172
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
yield
|
179
|
-
end
|
180
|
-
end
|
181
|
-
else
|
182
|
-
def with_default_shard
|
183
|
-
if is_sharded? && current_shard_id.nil? && table_name != ActiveRecord::Migrator.schema_migrations_table_name
|
184
|
-
on_first_shard { yield }
|
185
|
-
else
|
186
|
-
yield
|
187
|
-
end
|
173
|
+
def with_default_shard
|
174
|
+
if is_sharded? && current_shard_id.nil? && table_name != ActiveRecord::SchemaMigration.table_name
|
175
|
+
on_first_shard { yield }
|
176
|
+
else
|
177
|
+
yield
|
188
178
|
end
|
189
179
|
end
|
190
180
|
|
@@ -216,7 +206,7 @@ module ActiveRecordShards
|
|
216
206
|
end
|
217
207
|
|
218
208
|
case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
|
219
|
-
when '
|
209
|
+
when '4.2'
|
220
210
|
require 'active_record_shards/connection_switcher-4-0'
|
221
211
|
when '5.0'
|
222
212
|
require 'active_record_shards/connection_switcher-5-0'
|
@@ -93,10 +93,7 @@ module ActiveRecordShards
|
|
93
93
|
|
94
94
|
module ActiveRelationPatches
|
95
95
|
def self.included(base)
|
96
|
-
[
|
97
|
-
:calculate, :exists?, :pluck,
|
98
|
-
ActiveRecord::VERSION::MAJOR >= 4 ? :load : :find_with_associations
|
99
|
-
].each do |m|
|
96
|
+
[:calculate, :exists?, :pluck, :load].each do |m|
|
100
97
|
ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, base, m)
|
101
98
|
end
|
102
99
|
end
|
@@ -106,20 +103,6 @@ module ActiveRecordShards
|
|
106
103
|
end
|
107
104
|
end
|
108
105
|
|
109
|
-
module HasAndBelongsToManyPreloaderPatches
|
110
|
-
def self.included(base)
|
111
|
-
ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, base, :records_for) rescue nil # rubocop:disable Style/RescueModifier
|
112
|
-
end
|
113
|
-
|
114
|
-
def on_slave_unless_tx
|
115
|
-
klass.on_slave_unless_tx { yield }
|
116
|
-
end
|
117
|
-
|
118
|
-
def exists_with_default_slave?(*args, &block)
|
119
|
-
on_slave_unless_tx { exists_without_default_slave?(*args, &block) }
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
106
|
# in rails 4.1+, they create a join class that's used to pull in records for HABTM.
|
124
107
|
# this simplifies the hell out of our existence, because all we have to do is inerit on-slave-by-default
|
125
108
|
# down from the parent now.
|
@@ -15,11 +15,7 @@ module ActiveRecord
|
|
15
15
|
# Rails creates the internal tables on the unsharded DB. We make them
|
16
16
|
# manually on the sharded DBs.
|
17
17
|
ActiveRecord::Base.on_all_shards do
|
18
|
-
|
19
|
-
ActiveRecord::SchemaMigration.create_table
|
20
|
-
else
|
21
|
-
ActiveRecord::Base.connection.initialize_schema_migrations_table
|
22
|
-
end
|
18
|
+
ActiveRecord::SchemaMigration.create_table
|
23
19
|
if ActiveRecord::VERSION::MAJOR >= 5
|
24
20
|
ActiveRecord::InternalMetadata.create_table
|
25
21
|
end
|
@@ -2,12 +2,7 @@
|
|
2
2
|
require 'active_record_shards/connection_pool'
|
3
3
|
require 'active_record_shards/connection_handler'
|
4
4
|
require 'active_record_shards/connection_specification'
|
5
|
-
require 'active_record_shards/schema_dumper_extension'
|
6
5
|
|
7
6
|
ActiveRecordShards::ConnectionSpecification = ActiveRecord::ConnectionAdapters::ConnectionSpecification
|
8
7
|
methods_to_override = [:establish_connection, :remove_connection, :pool_for, :pool_from_any_process_for]
|
9
8
|
ActiveRecordShards.override_connection_handler_methods(methods_to_override)
|
10
|
-
|
11
|
-
ActiveRecord::Associations::Builder::HasAndBelongsToMany.include(ActiveRecordShards::DefaultSlavePatches::Rails41HasAndBelongsToManyBuilderExtension)
|
12
|
-
|
13
|
-
ActiveRecord::SchemaDumper.prepend(ActiveRecordShards::SchemaDumperExtension)
|
@@ -31,8 +31,8 @@ namespace :db do
|
|
31
31
|
ActiveRecord::Base.configurations.each do |key, conf|
|
32
32
|
next if !key.starts_with?(ActiveRecordShards.rails_env) || key.ends_with?("_slave")
|
33
33
|
begin
|
34
|
-
# MysqlAdapter takes charset instead of encoding in Rails
|
35
|
-
# https://github.com/rails/rails/blob/
|
34
|
+
# MysqlAdapter takes charset instead of encoding in Rails 4.2 or greater
|
35
|
+
# https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/tasks/mysql_database_tasks.rb#L85-L96
|
36
36
|
symbolized_configuration = conf.symbolize_keys
|
37
37
|
symbolized_configuration[:charset] = symbolized_configuration[:encoding]
|
38
38
|
|
@@ -55,10 +55,8 @@ namespace :db do
|
|
55
55
|
if ActiveRecord::VERSION::STRING >= "5.2.0"
|
56
56
|
migrations = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths).migrations
|
57
57
|
ActiveRecord::Migrator.new(:up, migrations).pending_migrations
|
58
|
-
elsif ActiveRecord::VERSION::MAJOR >= 4
|
59
|
-
ActiveRecord::Base.on_shard(nil) { ActiveRecord::Migrator.open(ActiveRecord::Migrator.migrations_paths).pending_migrations }
|
60
58
|
else
|
61
|
-
ActiveRecord::Base.on_shard(nil) { ActiveRecord::Migrator.
|
59
|
+
ActiveRecord::Base.on_shard(nil) { ActiveRecord::Migrator.open(ActiveRecord::Migrator.migrations_paths).pending_migrations }
|
62
60
|
end
|
63
61
|
|
64
62
|
if pending_migrations.any?
|
@@ -99,13 +97,8 @@ module ActiveRecordShards
|
|
99
97
|
private
|
100
98
|
|
101
99
|
def spec_for(conf)
|
102
|
-
|
103
|
-
|
104
|
-
resolver.spec(conf)
|
105
|
-
else
|
106
|
-
resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new conf, ActiveRecord::Base.configurations
|
107
|
-
resolver.spec
|
108
|
-
end
|
100
|
+
resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(ActiveRecord::Base.configurations)
|
101
|
+
resolver.spec(conf)
|
109
102
|
end
|
110
103
|
end
|
111
104
|
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.15.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: 2019-
|
16
|
+
date: 2019-06-21 00:00:00.000000000 Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: activerecord
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
requirements:
|
22
22
|
- - ">="
|
23
23
|
- !ruby/object:Gem::Version
|
24
|
-
version:
|
24
|
+
version: '4.2'
|
25
25
|
- - "<"
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '6.0'
|
@@ -31,7 +31,7 @@ dependencies:
|
|
31
31
|
requirements:
|
32
32
|
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version:
|
34
|
+
version: '4.2'
|
35
35
|
- - "<"
|
36
36
|
- !ruby/object:Gem::Version
|
37
37
|
version: '6.0'
|
@@ -41,7 +41,7 @@ dependencies:
|
|
41
41
|
requirements:
|
42
42
|
- - ">="
|
43
43
|
- !ruby/object:Gem::Version
|
44
|
-
version:
|
44
|
+
version: '4.2'
|
45
45
|
- - "<"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '6.0'
|
@@ -51,7 +51,7 @@ dependencies:
|
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: '4.2'
|
55
55
|
- - "<"
|
56
56
|
- !ruby/object:Gem::Version
|
57
57
|
version: '6.0'
|
@@ -191,9 +191,7 @@ files:
|
|
191
191
|
- lib/active_record_shards/default_slave_patches.rb
|
192
192
|
- lib/active_record_shards/migration.rb
|
193
193
|
- lib/active_record_shards/model.rb
|
194
|
-
- lib/active_record_shards/patches-3-2.rb
|
195
194
|
- lib/active_record_shards/patches-4-2.rb
|
196
|
-
- lib/active_record_shards/patches-5-0.rb
|
197
195
|
- lib/active_record_shards/schema_dumper_extension.rb
|
198
196
|
- lib/active_record_shards/shard_selection.rb
|
199
197
|
- lib/active_record_shards/shard_support.rb
|
@@ -1,11 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'active_record_shards/connection_pool'
|
3
|
-
require 'active_record_shards/connection_handler'
|
4
|
-
require 'active_record_shards/connection_specification'
|
5
|
-
|
6
|
-
|
7
|
-
ActiveRecordShards::ConnectionSpecification = ActiveRecord::Base::ConnectionSpecification
|
8
|
-
methods_to_override = [:remove_connection]
|
9
|
-
ActiveRecordShards.override_connection_handler_methods(methods_to_override)
|
10
|
-
|
11
|
-
ActiveRecord::Associations::Preloader::HasAndBelongsToMany.include(ActiveRecordShards::DefaultSlavePatches::HasAndBelongsToManyPreloaderPatches)
|
@@ -1,13 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
require 'active_record_shards/schema_dumper_extension'
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
ActiveRecord::Associations::Builder::HasAndBelongsToMany.include(ActiveRecordShards::DefaultSlavePatches::Rails41HasAndBelongsToManyBuilderExtension)
|
12
|
-
|
13
|
-
ActiveRecord::SchemaDumper.prepend(ActiveRecordShards::SchemaDumperExtension)
|