active_record_shards 5.3.3 → 5.5.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: e71e05125d6c15ad696e8243cb09234c09d29e1c381b47adeee2a81b0b79e188
4
- data.tar.gz: f06e143aabfc0e14d3058e03acc367e54d0219bcbf9355df57ee33e174393dbf
3
+ metadata.gz: f9e4c9f26a03dde94772c0888e49e3af56d68ffc4576fff3123a8dd50a73cce7
4
+ data.tar.gz: d4ea926bf565928ac2873e02b08b481cf731d3d62483482de450137886505d4d
5
5
  SHA512:
6
- metadata.gz: 60629cde5e087c941d9ce3d753a57c5d73f37cbd78d9e7b0f6f1d921834c25267371a0b2e957a92112eac3f295a837a2a41681cda63dc3a6bfd981811dfbcd91
7
- data.tar.gz: 2c4e012130eead2088089cefb2204508141b3bd5056f733ccd26029fa053ae78b10b077750b64f5b364731b752842210086e368f813b3b1b333e9cdf2fecefb9
6
+ metadata.gz: 699945070cfc1dde026841e71489a1e3d9e743cb4b49ff8e9a5f0849c1f50f3a55dab9833614377b8a998b8b181755c0efd2d090d3acd1d0f180c7dffee69c03
7
+ data.tar.gz: 6c1d7c1eb206352102e9d13b2f045e4e9cbd7262f8fe6baab0218ad9273e0ecab21442900730d8b1b07754b65e15d07f7341415f495930265ff892e455eb73c7
@@ -5,6 +5,11 @@ require 'active_record_shards/shard_support'
5
5
  module ActiveRecordShards
6
6
  module ConnectionSwitcher
7
7
  class LegacyConnectionHandlingError < StandardError; end
8
+ class IsolationLevelError < StandardError; end
9
+
10
+ Thread.attr_accessor :_active_record_shards_disallow_replica_by_thread,
11
+ :_active_record_shards_in_migration,
12
+ :_active_record_shards_shard_selection
8
13
 
9
14
  case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
10
15
  when '6.1', '7.0'
@@ -19,6 +24,9 @@ module ActiveRecordShards
19
24
 
20
25
  base.singleton_class.send(:alias_method, :table_exists_without_default_shard?, :table_exists?)
21
26
  base.singleton_class.send(:alias_method, :table_exists?, :table_exists_with_default_shard?)
27
+
28
+ base.singleton_class.send(:alias_method, :reset_primary_key_without_default_shard, :reset_primary_key)
29
+ base.singleton_class.send(:alias_method, :reset_primary_key, :reset_primary_key_with_default_shard)
22
30
  end
23
31
 
24
32
  def on_primary_db(&block)
@@ -118,11 +126,11 @@ module ActiveRecordShards
118
126
  end
119
127
 
120
128
  def disallow_replica=(value)
121
- Thread.current[:__active_record_shards__disallow_replica_by_thread] = value
129
+ Thread.current._active_record_shards_disallow_replica_by_thread = value
122
130
  end
123
131
 
124
132
  def disallow_replica
125
- Thread.current[:__active_record_shards__disallow_replica_by_thread] ||= 0
133
+ Thread.current._active_record_shards_disallow_replica_by_thread ||= 0
126
134
  end
127
135
 
128
136
  def supports_sharding?
@@ -134,7 +142,7 @@ module ActiveRecordShards
134
142
  end
135
143
 
136
144
  def current_shard_selection
137
- Thread.current[:shard_selection] ||= ShardSelection.new
145
+ Thread.current._active_record_shards_shard_selection ||= ShardSelection.new
138
146
  end
139
147
 
140
148
  def current_shard_id
@@ -145,6 +153,10 @@ module ActiveRecordShards
145
153
  config_for_env[SHARD_NAMES_CONFIG_KEY] || []
146
154
  end
147
155
 
156
+ def reset_primary_key_with_default_shard
157
+ with_default_shard { reset_primary_key_without_default_shard }
158
+ end
159
+
148
160
  private
149
161
 
150
162
  def config_for_env
@@ -177,6 +189,7 @@ module ActiveRecordShards
177
189
 
178
190
  def switch_connection(options)
179
191
  ensure_legacy_connection_handling if ActiveRecord.version >= Gem::Version.new('6.1')
192
+ ensure_thread_isolation_level if ActiveRecord.version >= Gem::Version.new('7.0')
180
193
 
181
194
  if options.any?
182
195
  if options.key?(:replica)
@@ -199,6 +212,12 @@ module ActiveRecordShards
199
212
  end
200
213
  end
201
214
 
215
+ def ensure_thread_isolation_level
216
+ unless ActiveSupport::IsolatedExecutionState.isolation_level == :thread
217
+ raise IsolationLevelError, "ActiveRecordShards is _only_ compatible when ActiveSupport::IsolatedExecutionState's isolation_level is set to :thread"
218
+ end
219
+ end
220
+
202
221
  def legacy_connection_handling_owner
203
222
  @legacy_connection_handling_owner ||=
204
223
  case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
@@ -29,21 +29,6 @@ module ActiveRecordShards
29
29
  RUBY
30
30
  end
31
31
 
32
- def transaction_with_replica_off(*args, &block)
33
- if on_replica_by_default?
34
- begin
35
- old_val = Thread.current[:_active_record_shards_in_tx]
36
- Thread.current[:_active_record_shards_in_tx] = true
37
- transaction_without_replica_off(*args, &block)
38
- ensure
39
- Thread.current[:_active_record_shards_in_tx] = old_val
40
- end
41
- else
42
- transaction_without_replica_off(*args, &block)
43
- end
44
- end
45
- ruby2_keywords(:transaction_with_replica_off) if respond_to?(:ruby2_keywords, true)
46
-
47
32
  module InstanceMethods
48
33
  def on_replica_unless_tx
49
34
  self.class.on_replica_unless_tx { yield }
@@ -80,17 +65,12 @@ module ActiveRecordShards
80
65
 
81
66
  base.class_eval do
82
67
  include InstanceMethods
83
-
84
- class << self
85
- alias_method :transaction_without_replica_off, :transaction
86
- alias_method :transaction, :transaction_with_replica_off
87
- end
88
68
  end
89
69
  end
90
70
 
91
71
  def on_replica_unless_tx(&block)
92
- return yield if Thread.current[:_active_record_shards_in_migration]
93
- return yield if Thread.current[:_active_record_shards_in_tx]
72
+ return yield if Thread.current._active_record_shards_in_migration
73
+ return yield if _in_transaction?
94
74
 
95
75
  if on_replica_by_default?
96
76
  on_replica(&block)
@@ -99,8 +79,12 @@ module ActiveRecordShards
99
79
  end
100
80
  end
101
81
 
82
+ def _in_transaction?
83
+ connected? && connection.transaction_open?
84
+ end
85
+
102
86
  def force_on_replica(&block)
103
- return yield if Thread.current[:_active_record_shards_in_migration]
87
+ return yield if Thread.current._active_record_shards_in_migration
104
88
 
105
89
  on_cx_switch_block(:replica, construct_ro_scope: false, force: true, &block)
106
90
  end
@@ -121,8 +105,8 @@ module ActiveRecordShards
121
105
 
122
106
  module Rails52RelationPatches
123
107
  def connection
124
- return super if Thread.current[:_active_record_shards_in_migration]
125
- return super if Thread.current[:_active_record_shards_in_tx]
108
+ return super if Thread.current._active_record_shards_in_migration
109
+ return super if _in_transaction?
126
110
 
127
111
  if @klass.on_replica_by_default?
128
112
  @klass.on_replica.connection
@@ -213,8 +197,8 @@ module ActiveRecordShards
213
197
 
214
198
  module TypeCasterConnectionConnectionPatch
215
199
  def connection
216
- return super if Thread.current[:_active_record_shards_in_migration]
217
- return super if Thread.current[:_active_record_shards_in_tx]
200
+ return super if Thread.current._active_record_shards_in_migration
201
+ return super if ActiveRecord::Base._in_transaction?
218
202
 
219
203
  if @klass.on_replica_by_default?
220
204
  @klass.on_replica.connection
@@ -226,11 +210,11 @@ module ActiveRecordShards
226
210
 
227
211
  module SchemaDefinePatch
228
212
  def define(info, &block)
229
- old_val = Thread.current[:_active_record_shards_in_migration]
230
- Thread.current[:_active_record_shards_in_migration] = true
213
+ old_val = Thread.current._active_record_shards_in_migration
214
+ Thread.current._active_record_shards_in_migration = true
231
215
  super
232
216
  ensure
233
- Thread.current[:_active_record_shards_in_migration] = old_val
217
+ Thread.current._active_record_shards_in_migration = old_val
234
218
  end
235
219
  end
236
220
  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.3.3
4
+ version: 5.5.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: 2023-04-28 00:00:00.000000000 Z
16
+ date: 2023-08-26 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: activerecord
@@ -83,20 +83,6 @@ dependencies:
83
83
  - - ">="
84
84
  - !ruby/object:Gem::Version
85
85
  version: 5.10.0
86
- - !ruby/object:Gem::Dependency
87
- name: minitest-rg
88
- requirement: !ruby/object:Gem::Requirement
89
- requirements:
90
- - - ">="
91
- - !ruby/object:Gem::Version
92
- version: '0'
93
- type: :development
94
- prerelease: false
95
- version_requirements: !ruby/object:Gem::Requirement
96
- requirements:
97
- - - ">="
98
- - !ruby/object:Gem::Version
99
- version: '0'
100
86
  - !ruby/object:Gem::Dependency
101
87
  name: mysql2
102
88
  requirement: !ruby/object:Gem::Requirement