active_record_shards 5.0.0 → 5.1.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: 07a2ae4e9b397abcc455655266c9d66c76af160e56ea963b401a130589f3f48c
4
- data.tar.gz: 45993e917fb0585637c72f89d6bee77f805e0192efb6ca3f476e86929d9192ab
3
+ metadata.gz: aa6b8faea89614322485e584a2e63f9cb7b6ee505c641c68dcf239337776230a
4
+ data.tar.gz: eb5038c548eecf373a19e811d0354df3c535b6f6f9bfa5cca9140e6d4dbe2994
5
5
  SHA512:
6
- metadata.gz: 16309006259fadb5b3851ea470d8859618c2df4d10555e5ecb50c257d538ac3f36c69490a33547bc7e53e6a2962d5985623176021b5f0866f9a35973138000cc
7
- data.tar.gz: d23f2ae22180be953ba4a8bbb23000f73901ecf7950cae95858d371eec24bf01f2e7d6ee0c7225e8bbe2f76490261baae4059193718ae3cb17ffd78ab338f852
6
+ metadata.gz: 3b06434d4f484e2d8ea3cceaa98e535712c41b7ac9b2fece389ca25a21e1d0f6249b5c4e85a6206b8718784b9150477b26247de8f667439486dab8d0abf4ef30
7
+ data.tar.gz: 6453444d52019b1f296df12d96085abaabc40af5bf7595ed2077104efcec8bc7dcda7d43164efb254d09922c67690584e68f6c711d74cefe6de4d16fbe390bab
data/README.md CHANGED
@@ -7,6 +7,8 @@ switch between database connections. We've made the implementation very small, a
7
7
 
8
8
  ActiveRecord Shards has been used and tested on Rails 5.x and 6.0, and has in some form or another been used in production on large Rails apps for several years.
9
9
 
10
+ Rails 6.1 introduced new connection handling and support for sharding. Apps are encouraged to migrate to the native sharding logic but ActiveRecord Shards supports Rails 6.1 when `legacy_connection_handling` is set to `true`. For more information see [Rails 6.1 installation](#rails-61-installation) and Rails' [multiple databases guide](https://guides.rubyonrails.org/active_record_multiple_databases.html).
11
+
10
12
  - [Installation](#installation)
11
13
  - [Configuration](#configuration)
12
14
  - [Migrations](#migrations)
@@ -22,6 +24,22 @@ ActiveRecord Shards has been used and tested on Rails 5.x and 6.0, and has in so
22
24
 
23
25
  and make sure to require 'active\_record\_shards' in some way.
24
26
 
27
+ ### Rails 6.1 installation
28
+
29
+ Rails 6.1 is **only** supported with `legacy_connection_handling` set to `true`.
30
+
31
+ Enable the legacy handling in your configuration files e.g. `config/application.rb` by setting:
32
+
33
+ ``` Ruby
34
+ config.active_record.legacy_connection_handling = true
35
+ ```
36
+
37
+ or
38
+
39
+ ``` Ruby
40
+ ActiveRecord::Base.legacy_connection_handling = true
41
+ ```
42
+
25
43
  ## Configuration
26
44
 
27
45
  Add the replica and shard configuration to config/database.yml:
@@ -4,7 +4,11 @@ require 'active_record_shards/shard_support'
4
4
 
5
5
  module ActiveRecordShards
6
6
  module ConnectionSwitcher
7
- SHARD_NAMES_CONFIG_KEY = 'shard_names'
7
+ if "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}" == '6.1'
8
+ SHARD_NAMES_CONFIG_KEY = :shard_names
9
+ else
10
+ SHARD_NAMES_CONFIG_KEY = 'shard_names'
11
+ end
8
12
 
9
13
  def self.extended(base)
10
14
  base.singleton_class.send(:alias_method, :load_schema_without_default_shard!, :load_schema!)
@@ -14,11 +18,6 @@ module ActiveRecordShards
14
18
  base.singleton_class.send(:alias_method, :table_exists?, :table_exists_with_default_shard?)
15
19
  end
16
20
 
17
- def default_shard=(new_default_shard)
18
- ActiveRecordShards::ShardSelection.default_shard = new_default_shard
19
- switch_connection(shard: new_default_shard)
20
- end
21
-
22
21
  def on_primary_db(&block)
23
22
  on_shard(nil, &block)
24
23
  end
@@ -209,7 +208,7 @@ end
209
208
  case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
210
209
  when '5.1', '5.2'
211
210
  require 'active_record_shards/connection_switcher-5-1'
212
- when '6.0'
211
+ when '6.0', '6.1'
213
212
  require 'active_record_shards/connection_switcher-6-0'
214
213
  else
215
214
  raise "ActiveRecordShards is not compatible with #{ActiveRecord::VERSION::STRING}"
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordShards
4
+ module DefaultShard
5
+ def default_shard=(new_default_shard)
6
+ if ars_shard_type?(new_default_shard)
7
+ ActiveRecordShards::ShardSelection.ars_default_shard = new_default_shard
8
+ switch_connection(shard: new_default_shard)
9
+ else
10
+ super
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def ars_shard_type?(shard)
17
+ return true if ActiveRecord.version < Gem::Version.new('6.1')
18
+ return true if shard.nil?
19
+ return true if shard == :_no_shard
20
+ return true if shard.is_a?(Integer)
21
+
22
+ false
23
+ end
24
+ end
25
+ end
26
+
27
+ ActiveRecord::Base.singleton_class.prepend(ActiveRecordShards::DefaultShard)
@@ -3,7 +3,7 @@
3
3
  module ActiveRecordShards
4
4
  class ShardSelection
5
5
  NO_SHARD = :_no_shard
6
- cattr_accessor :default_shard
6
+ cattr_accessor :ars_default_shard
7
7
 
8
8
  def initialize
9
9
  @on_replica = false
@@ -14,7 +14,7 @@ module ActiveRecordShards
14
14
  if @shard.nil? || @shard == NO_SHARD
15
15
  nil
16
16
  else
17
- @shard || self.class.default_shard
17
+ @shard || self.class.ars_default_shard
18
18
  end
19
19
  end
20
20
 
@@ -94,15 +94,22 @@ module ActiveRecordShards
94
94
  def root_connection(conf)
95
95
  conf = conf.merge('database' => nil)
96
96
  spec = spec_for(conf)
97
-
98
- ActiveRecord::Base.send("#{conf['adapter']}_connection", spec.config)
97
+ if "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}" == '6.1'
98
+ ActiveRecord::Base.send("#{conf['adapter']}_connection", spec.db_config.configuration_hash)
99
+ else
100
+ ActiveRecord::Base.send("#{conf['adapter']}_connection", spec.config)
101
+ end
99
102
  end
100
103
 
101
104
  private
102
105
 
103
106
  def spec_for(conf)
104
- resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(ActiveRecord::Base.configurations)
105
- resolver.spec(conf)
107
+ if "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}" == '6.1'
108
+ ActiveRecord::Base.connection_handler.send(:resolve_pool_config, conf, ActiveRecord::Base)
109
+ else
110
+ resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(ActiveRecord::Base.configurations)
111
+ resolver.spec(conf)
112
+ end
106
113
  end
107
114
  end
108
115
  end
@@ -9,6 +9,7 @@ require 'active_record_shards/connection_switcher'
9
9
  require 'active_record_shards/association_collection_connection_selection'
10
10
  require 'active_record_shards/migration'
11
11
  require 'active_record_shards/default_replica_patches'
12
+ require 'active_record_shards/default_shard'
12
13
  require 'active_record_shards/schema_dumper_extension'
13
14
 
14
15
  module ActiveRecordShards
@@ -61,7 +62,7 @@ when '5.2'
61
62
 
62
63
  # https://github.com/rails/rails/blob/v5.2.6/activerecord/lib/active_record/associations/preloader/association.rb#L96
63
64
  ActiveRecord::Associations::Preloader::Association.prepend(ActiveRecordShards::DefaultReplicaPatches::AssociationsPreloaderAssociationLoadRecordsPatch)
64
- when '6.0'
65
+ when '6.0', '6.1'
65
66
  # https://github.com/rails/rails/blob/v6.0.4/activerecord/lib/active_record/type_caster/connection.rb#L28
66
67
  ActiveRecord::TypeCaster::Connection.prepend(ActiveRecordShards::DefaultReplicaPatches::TypeCasterConnectionConnectionPatch)
67
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.0.0
4
+ version: 5.1.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-07-06 00:00:00.000000000 Z
16
+ date: 2022-08-23 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: '6.1'
27
+ version: '7.0'
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: '6.1'
37
+ version: '7.0'
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: '6.1'
47
+ version: '7.0'
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: '6.1'
57
+ version: '7.0'
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: bump
60
60
  requirement: !ruby/object:Gem::Requirement
@@ -199,6 +199,7 @@ files:
199
199
  - lib/active_record_shards/connection_switcher-6-0.rb
200
200
  - lib/active_record_shards/connection_switcher.rb
201
201
  - lib/active_record_shards/default_replica_patches.rb
202
+ - lib/active_record_shards/default_shard.rb
202
203
  - lib/active_record_shards/migration.rb
203
204
  - lib/active_record_shards/model.rb
204
205
  - lib/active_record_shards/schema_dumper_extension.rb