active_record_shards 5.2.0 → 5.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 15b0d33ae1fec065fc2c2763f52bf5385da900ddbc3d8b55089ac6f1f99f9b1c
4
- data.tar.gz: 61de1ca1ad18611d9eb059dcec55eb85e4de55c965dd3ee33c60088a5d68f4f2
3
+ metadata.gz: 032e2279ef2f82d2a1a30a171429a7b34b625766fe3a6060ad21141d551c0ca4
4
+ data.tar.gz: 169c3f8e0bf7d4f036a23106d8523b5e09756535f289be4da5ec295ec6c1605d
5
5
  SHA512:
6
- metadata.gz: 90e52914b1a63fbfb633882f60f2c2020a057b7ad16530c5800cc44f2e88d551c638fcdc31615f2c1c13d4fd031148ce568a10d4776c0082b0c917e9c7ee8dba
7
- data.tar.gz: 8b215a24c9c0e1ff79e75499b9d47976b0143095023c0ec2a1ab4c03cc5e9c048957ef63703fe2a8eef5b92c72b3d156a95662ac3367a67ec6324f9527142913
6
+ metadata.gz: c35a082c7b33926009467f10871177ecb02a2c5dc3b009a1f5d1a889fb49f039ea88bb931cdd80c7cc67763120158748610925ece6d53ee463020388a4690cc5
7
+ data.tar.gz: 4acc42ae93f573d60d12384f6f7454b4a85d1a5a13239491cb6b00a10d722dfb1534026119f845b3f8c39c5e0114500099eac6806cc2c8b6864c7c9f74835fde
@@ -4,6 +4,8 @@ require 'active_record_shards/shard_support'
4
4
 
5
5
  module ActiveRecordShards
6
6
  module ConnectionSwitcher
7
+ class LegacyConnectionHandlingError < StandardError; end
8
+
7
9
  case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
8
10
  when '6.1', '7.0'
9
11
  SHARD_NAMES_CONFIG_KEY = :shard_names
@@ -96,10 +98,9 @@ module ActiveRecordShards
96
98
  end
97
99
 
98
100
  def on_cx_switch_block(which, force: false, construct_ro_scope: nil, &block)
99
- @disallow_replica ||= 0
100
- @disallow_replica += 1 if which == :primary
101
+ self.disallow_replica += 1 if which == :primary
101
102
 
102
- switch_to_replica = force || @disallow_replica.zero?
103
+ switch_to_replica = force || disallow_replica.zero?
103
104
  old_options = current_shard_selection.options
104
105
 
105
106
  switch_connection(replica: switch_to_replica)
@@ -112,10 +113,18 @@ module ActiveRecordShards
112
113
  readonly.scoping(&block)
113
114
  end
114
115
  ensure
115
- @disallow_replica -= 1 if which == :primary
116
+ self.disallow_replica -= 1 if which == :primary
116
117
  switch_connection(old_options) if old_options
117
118
  end
118
119
 
120
+ def disallow_replica=(value)
121
+ Thread.current[:__active_record_shards__disallow_replica_by_thread] = value
122
+ end
123
+
124
+ def disallow_replica
125
+ Thread.current[:__active_record_shards__disallow_replica_by_thread] ||= 0
126
+ end
127
+
119
128
  def supports_sharding?
120
129
  shard_names.any?
121
130
  end
@@ -163,6 +172,8 @@ module ActiveRecordShards
163
172
  alias_method :check_config_for_env, :config_for_env
164
173
 
165
174
  def switch_connection(options)
175
+ ensure_legacy_connection_handling if ActiveRecord.version >= Gem::Version.new('6.1')
176
+
166
177
  if options.any?
167
178
  if options.key?(:replica)
168
179
  current_shard_selection.on_replica = options[:replica]
@@ -178,6 +189,22 @@ module ActiveRecordShards
178
189
  end
179
190
  end
180
191
 
192
+ def ensure_legacy_connection_handling
193
+ unless legacy_connection_handling_owner.legacy_connection_handling
194
+ raise LegacyConnectionHandlingError, "ActiveRecordShards is _only_ compatible with ActiveRecord `legacy_connection_handling` set to `true`."
195
+ end
196
+ end
197
+
198
+ def legacy_connection_handling_owner
199
+ @legacy_connection_handling_owner ||=
200
+ case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
201
+ when '7.0'
202
+ ActiveRecord
203
+ when '6.1'
204
+ ActiveRecord::Base
205
+ end
206
+ end
207
+
181
208
  def shard_env
182
209
  ActiveRecordShards.app_env
183
210
  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: 5.2.0
4
+ version: 5.3.1
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-10-11 00:00:00.000000000 Z
16
+ date: 2023-02-14 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: activerecord
@@ -97,20 +97,6 @@ dependencies:
97
97
  - - ">="
98
98
  - !ruby/object:Gem::Version
99
99
  version: '0'
100
- - !ruby/object:Gem::Dependency
101
- name: mocha
102
- requirement: !ruby/object:Gem::Requirement
103
- requirements:
104
- - - ">="
105
- - !ruby/object:Gem::Version
106
- version: 1.4.0
107
- type: :development
108
- prerelease: false
109
- version_requirements: !ruby/object:Gem::Requirement
110
- requirements:
111
- - - ">="
112
- - !ruby/object:Gem::Version
113
- version: 1.4.0
114
100
  - !ruby/object:Gem::Dependency
115
101
  name: mysql2
116
102
  requirement: !ruby/object:Gem::Requirement