switchman 3.6.5 → 3.6.7

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: 7f9407ddbab463ffaa11b358a6591186d512f7558dde95713bf54f584a0fd362
4
- data.tar.gz: a356c5a51ffad024b5c23ab69f5b32bbfa95785f8cbec7c80f0d0ef0eb0f4996
3
+ metadata.gz: 2e86d08703a350aa5b8d3d843c51a0813f00e6ee0c681d0c95094f84863b327b
4
+ data.tar.gz: f13e438d68d996f55c9be543cc8cb89967b62f9ddd111ab4fae188da8b959096
5
5
  SHA512:
6
- metadata.gz: b0470870a20eb63d03cf46efa57b6db2dc9b3f14701491293b298261f71e7db93cb82d7c27cb9b36a6ea36f6de1d97daf3ec56cd0fb6e3d1595daad65044f1f2
7
- data.tar.gz: c28da8999f5660e01486ac4c28dfddb1b254422d7f51f29156bcd97ec6d774bcdcc0c2ef4ffbd7de4f959adaedd6005dc3e9535bc317f7312620c7f8d88236a8
6
+ metadata.gz: 7be63615d79be0b46184b9e6754bcb6091873e2eb0c4722705daea7dd81dd50320819b2238538c818637901570c2ad47a7ed62febd441c4d5b130c78391554cc
7
+ data.tar.gz: c3eb6290f9437ccac7162216840c2f258f31fc6c978121b9054c6308640bff6d522f2f61fd2247fea178c1c173a012a4d7f2c22b2e234d7b4f431ce4b8306403
@@ -22,13 +22,59 @@ module Switchman
22
22
  @integral_id
23
23
  end
24
24
 
25
- %w[transaction insert_all upsert_all].each do |method|
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 }
30
+ end
31
+ else
32
+ db = Shard.current(connection_class_for_self).database_server
33
+ db.unguard { super }
34
+ end
35
+ end
36
+
37
+ # NOTE: `returning` values are _not_ transposed back to the current shard
38
+ %w[insert_all upsert_all].each do |method|
26
39
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
27
- def #{method}(*, **)
28
- if self != ::ActiveRecord::Base && current_scope
29
- current_scope.activate do
40
+ def #{method}(attributes, returning: nil, **)
41
+ scope = self != ::ActiveRecord::Base && current_scope
42
+ if (target_shard = scope&.primary_shard) == (current_shard = Shard.current(connection_class_for_self))
43
+ scope = nil
44
+ end
45
+ if scope
46
+ dupped = false
47
+ attributes.each_with_index do |hash, i|
48
+ if dupped || hash.any? { |k, v| sharded_column?(k) }
49
+ unless dupped
50
+ attributes = attributes.dup
51
+ dupped = true
52
+ end
53
+ attributes[i] = hash.to_h do |k, v|
54
+ if sharded_column?(k)
55
+ [k, Shard.relative_id_for(v, current_shard, target_shard)]
56
+ else
57
+ [k, v]
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ if scope
65
+ scope.activate do
30
66
  db = Shard.current(connection_class_for_self).database_server
31
- db.unguard { super }
67
+ result = db.unguard { super }
68
+ if result&.columns&.any? { |c| sharded_column?(c) }
69
+ transposed_rows = result.rows.map do |row|
70
+ row.map.with_index do |value, i|
71
+ sharded_column?(result.columns[i]) ? Shard.relative_id_for(value, target_shard, current_shard) : value
72
+ end
73
+ end
74
+ result = ::ActiveRecord::Result.new(result.columns, transposed_rows, result.column_types)
75
+ end
76
+
77
+ result
32
78
  end
33
79
  else
34
80
  db = Shard.current(connection_class_for_self).database_server
@@ -22,12 +22,14 @@ module Switchman
22
22
 
23
23
  def lookup_store(*store_options)
24
24
  store = super
25
- # can't use defined?, because it's a _ruby_ autoloaded constant,
26
- # so just checking that will cause it to get required
27
- if store.instance_of?(ActiveSupport::Cache::RedisCacheStore) &&
28
- !::ActiveSupport::Cache::RedisCacheStore <= RedisCacheStore
25
+ # must use the string name, otherwise it will try to auto-load the constant
26
+ # and we don't want to require redis in this file (since it's not a hard dependency)
27
+ # rubocop:disable Style/ClassEqualityComparison
28
+ if store.class.name == "ActiveSupport::Cache::RedisCacheStore" &&
29
+ !(::ActiveSupport::Cache::RedisCacheStore <= RedisCacheStore)
29
30
  ::ActiveSupport::Cache::RedisCacheStore.prepend(RedisCacheStore)
30
31
  end
32
+ # rubocop:enable Style/ClassEqualityComparison
31
33
  store.options[:namespace] ||= -> { Shard.current.default? ? nil : "shard_#{Shard.current.id}" }
32
34
  store
33
35
  end
@@ -36,7 +38,7 @@ module Switchman
36
38
  module RedisCacheStore
37
39
  def clear(namespace: nil, **)
38
40
  # RedisCacheStore tries to be smart and only clear the cache under your namespace, if you have one set
39
- # unfortunately, it uses the keys command, which is extraordinarily inefficient in a large redis instance
41
+ # unfortunately, it doesn't work using redis clustering because of the way redis keys are distributed
40
42
  # fortunately, we can assume we control the entire instance, because we set up the namespacing, so just
41
43
  # always unset it temporarily for clear calls
42
44
  namespace = nil # rubocop:disable Lint/ShadowedArgument
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchman
4
- VERSION = "3.6.5"
4
+ VERSION = "3.6.7"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchman
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.5
4
+ version: 3.6.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2024-06-12 00:00:00.000000000 Z
13
+ date: 2024-09-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord