switchman 3.1.0 → 3.1.3

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: 48b27124d5233adf585aed105d0b2522c76249bdd1e13fbcfd7e904241d3a624
4
- data.tar.gz: fea1ccc0ad63f00633fb90bee1d7bd4606f6de0e18564250865c1b1dde61ef8d
3
+ metadata.gz: 54ae135ce532aa5578592027f6ce46cf3aedecdf0a6fd097ae3597752f436bae
4
+ data.tar.gz: 02d88f28e64e487c6841940a383afd92a56378ea7c2e4b1156252c0bcf0fa71f
5
5
  SHA512:
6
- metadata.gz: c05b445111dd0e4b16ecbfaea6ab84557fa2cbadc6102fa0af6fb43cff2f6bbb6606e825f76ed20f6b8a4a2a2966e31babadf66c5eb065a6c4d8a7eea6a8b92a
7
- data.tar.gz: 0af9e15706142f90772d24e2187db93ec83331997bccbb25c1b89cec2dde1bd2414d2774fec3785c7bea1a1b06d3fa5ad49e15b716f0aa35794e2e5734c848a8
6
+ metadata.gz: 383075584faafb39abc64be4f6e77a99c4ca3dfd5acc1c57bae885ba71c8d13ce2cb0f89d9332fd8387f5100886a5a0ffbb6f6d53e382b0e54412aea6b72cf06
7
+ data.tar.gz: 875421e2c515a671ed8503fd07b55d22e16f098ddde635b5530a3ab4801c9bebd06a5e4e75d7a547b1e84e5628288dac9aefc0c6f9a23cedad80b1f8389bb5de
@@ -256,20 +256,6 @@ module Switchman
256
256
  attribute(attr_name)
257
257
  end
258
258
  end
259
-
260
- private
261
-
262
- def connection_class_for_self_for_reflection(reflection)
263
- if reflection
264
- if reflection.options[:polymorphic]
265
- read_attribute(reflection.foreign_type)&.constantize&.connection_class_for_self
266
- else
267
- reflection.klass.connection_class_for_self
268
- end
269
- else
270
- self.class.connection_class_for_self
271
- end
272
- end
273
259
  end
274
260
  end
275
261
  end
@@ -22,16 +22,20 @@ module Switchman
22
22
  @integral_id
23
23
  end
24
24
 
25
- def transaction(**)
26
- if self != ::ActiveRecord::Base && current_scope
27
- current_scope.activate do
28
- db = Shard.current(connection_class_for_self).database_server
29
- db.unguard { super }
25
+ %w[transaction insert_all upsert_all].each do |method|
26
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
27
+ def #{method}(*, **)
28
+ if self != ::ActiveRecord::Base && current_scope
29
+ current_scope.activate do
30
+ db = Shard.current(connection_class_for_self).database_server
31
+ db.unguard { super }
32
+ end
33
+ else
34
+ db = Shard.current(connection_class_for_self).database_server
35
+ db.unguard { super }
36
+ end
30
37
  end
31
- else
32
- db = Shard.current(connection_class_for_self).database_server
33
- db.unguard { super }
34
- end
38
+ RUBY
35
39
  end
36
40
 
37
41
  def reset_column_information
@@ -138,9 +142,33 @@ module Switchman
138
142
  else
139
143
  Shard.current(self.class.connection_class_for_self)
140
144
  end
145
+ readonly! if shadow_record? && !Switchman.config[:writable_shadow_records]
141
146
  super
142
147
  end
143
148
 
149
+ def shadow_record?
150
+ pkey = self[self.class.primary_key]
151
+ return false unless self.class.sharded_column?(self.class.primary_key) && pkey
152
+
153
+ pkey > Shard::IDS_PER_SHARD
154
+ end
155
+
156
+ def save_shadow_record(new_attrs: attributes, target_shard: Shard.current)
157
+ return if target_shard == shard
158
+
159
+ shadow_attrs = {}
160
+ new_attrs.each do |attr, value|
161
+ shadow_attrs[attr] = if self.class.sharded_column?(attr)
162
+ Shard.relative_id_for(value, shard, target_shard)
163
+ else
164
+ value
165
+ end
166
+ end
167
+ target_shard.activate do
168
+ self.class.upsert(shadow_attrs, unique_by: self.class.primary_key)
169
+ end
170
+ end
171
+
144
172
  def shard
145
173
  @shard || Shard.current(self.class.connection_class_for_self) || Shard.default
146
174
  end
@@ -20,13 +20,15 @@ module Switchman
20
20
  end
21
21
 
22
22
  module Migrator
23
- # significant change: just return MIGRATOR_SALT directly
24
- # especially if you're going through pgbouncer, the database
25
- # name you're accessing may not be consistent. it is NOT allowed
26
- # to run migrations against multiple shards in the same database
27
- # concurrently
23
+ # significant change: use the shard name instead of the database name
24
+ # in the lock id. Especially if you're going through pgbouncer, the
25
+ # database name you're accessing may not be consistent
28
26
  def generate_migrator_advisory_lock_id
29
- ::ActiveRecord::Migrator::MIGRATOR_SALT
27
+ db_name_hash = Zlib.crc32(Shard.current.name)
28
+ shard_name_hash = ::ActiveRecord::Migrator::MIGRATOR_SALT * db_name_hash
29
+ # Store in internalmetadata to allow other tools to be able to lock out migrations
30
+ ::ActiveRecord::InternalMetadata[:migrator_advisory_lock_id] = shard_name_hash
31
+ shard_name_hash
30
32
  end
31
33
 
32
34
  # significant change: strip out prefer_secondary from config
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchman
4
- VERSION = '3.1.0'
4
+ VERSION = '3.1.3'
5
5
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchman
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
8
8
  - James Williams
9
9
  - Jacob Fugal
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2022-06-02 00:00:00.000000000 Z
13
+ date: 2022-08-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -300,7 +300,7 @@ licenses:
300
300
  - MIT
301
301
  metadata:
302
302
  rubygems_mfa_required: 'true'
303
- post_install_message:
303
+ post_install_message:
304
304
  rdoc_options: []
305
305
  require_paths:
306
306
  - lib
@@ -316,7 +316,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
316
316
  version: '0'
317
317
  requirements: []
318
318
  rubygems_version: 3.1.6
319
- signing_key:
319
+ signing_key:
320
320
  specification_version: 4
321
321
  summary: Rails sharding magic
322
322
  test_files: []