switchman 3.0.15 → 3.0.18

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: d009eac65601ef2687150f472d14824f5d55976102257a381a2b7da4e9859131
4
- data.tar.gz: 37f505b1911705b84053e6c2949f544e0975a1ee4ce99df1db33c53ce5c1df46
3
+ metadata.gz: 21bdb8885af6932038bdf482531843f9d0157e0c51d806601937248e636ff98a
4
+ data.tar.gz: 92f0ae609d94b8d7eeefa909927481964a09305a00ebcc792d8b8079625df226
5
5
  SHA512:
6
- metadata.gz: 05c8ac0cec905fd3922c2271fa2e41dd0ba64890b433cdee1a15ad1255a74827d96fa79bd7f569c51cdd01e92965adc2889e534e63eaa72fae200ece912970d8
7
- data.tar.gz: f363145f950678a22893653bc6bfd34230c2e650e68151ebdf65ce7174cc14c79fc1dcfb16a1da4f1f4d264f7eae3a16f8766a27696e4659b58ca924d0ce9513
6
+ metadata.gz: 5d399c2f5de126812026515e4e5afd80380995cd8f098a561a3b17467c68caee64367ac60aa34c46c9a0f4550f9b246a459fd107d364e905db0098b665f32e9c
7
+ data.tar.gz: '096f236515a0641c11805fc71f43e866301cedfced33d4662222f9a9df08e8dbdecf97dd315273c80d21252ebd75ddb447628d1b6dccff4c94d5f3f782d5d7af'
@@ -182,7 +182,7 @@ module Switchman
182
182
  new_title = [short_parent_name, name].join(' ')
183
183
  Process.setproctitle(new_title)
184
184
  Switchman.config[:on_fork_proc]&.call
185
- with_each_shard(subscope, classes, exception: exception, &block)
185
+ with_each_shard(subscope, classes, exception: exception, &block).map { |result| Parallel::ResultWrapper.new(result) }
186
186
  rescue => e
187
187
  logger.error e.full_message
188
188
  Parallel::QuietExceptionWrapper.new(name, ::Parallel::ExceptionWrapper.new(e))
@@ -198,7 +198,7 @@ module Switchman
198
198
  cause: errors.first.exception
199
199
  end
200
200
 
201
- return ret
201
+ return ret.map(&:result)
202
202
  end
203
203
 
204
204
  classes ||= []
@@ -407,6 +407,7 @@ module Switchman
407
407
 
408
408
  klass.connects_to shards: connects_to_hash
409
409
  end
410
+ DatabaseServer.all.each { |db| db.guard! if db.config[:prefer_secondary] } unless @sharding_initialized
410
411
 
411
412
  @sharding_initialized = true
412
413
  end
@@ -28,19 +28,11 @@ module Switchman
28
28
  if self != ::ActiveRecord::Base && current_scope
29
29
  current_scope.activate do
30
30
  db = Shard.current(connection_classes).database_server
31
- if ::GuardRail.environment == db.guard_rail_environment
32
- super
33
- else
34
- db.unguard { super }
35
- end
31
+ db.unguard { super }
36
32
  end
37
33
  else
38
34
  db = Shard.current(connection_classes).database_server
39
- if ::GuardRail.environment == db.guard_rail_environment
40
- super
41
- else
42
- db.unguard { super }
43
- end
35
+ db.unguard { super }
44
36
  end
45
37
  end
46
38
 
@@ -68,6 +60,28 @@ module Switchman
68
60
  end
69
61
  end
70
62
 
63
+ def current_role_overriden?
64
+ current_role != current_role(without_overrides: true)
65
+ end
66
+
67
+ # significant change: Allow per-shard roles
68
+ def current_role(without_overrides: false)
69
+ return super() if without_overrides
70
+
71
+ sharded_role = nil
72
+ connected_to_stack.reverse_each do |hash|
73
+ shard_role = hash.dig(:shard_roles, current_shard)
74
+ if shard_role && (hash[:klasses].include?(::ActiveRecord::Base) || hash[:klasses].include?(connection_classes))
75
+ sharded_role = shard_role
76
+ break
77
+ end
78
+ end
79
+ # Allow a shard-specific role to be reverted to regular inheritance
80
+ return sharded_role if sharded_role && sharded_role != :_switchman_inherit
81
+
82
+ super()
83
+ end
84
+
71
85
  # significant change: _don't_ check if klasses.include?(Base)
72
86
  # i.e. other sharded models don't inherit the current shard of Base
73
87
  def current_shard
@@ -146,7 +160,8 @@ module Switchman
146
160
 
147
161
  def with_transaction_returning_status
148
162
  shard.activate(self.class.connection_classes) do
149
- super
163
+ db = Shard.current(self.class.connection_classes).database_server
164
+ db.unguard { super }
150
165
  end
151
166
  end
152
167
 
@@ -167,9 +182,7 @@ module Switchman
167
182
 
168
183
  def update_columns(*)
169
184
  db = shard.database_server
170
- return db.unguard { super } if ::GuardRail.environment != db.guard_rail_environment
171
-
172
- super
185
+ db.unguard { super }
173
186
  end
174
187
 
175
188
  def id_for_database
@@ -14,9 +14,7 @@ module Switchman
14
14
 
15
15
  def delete
16
16
  db = shard.database_server
17
- return db.unguard { super } unless ::GuardRail.environment == db.guard_rail_environment
18
-
19
- super
17
+ db.unguard { super }
20
18
  end
21
19
  end
22
20
  end
@@ -129,27 +129,26 @@ module Switchman
129
129
  end
130
130
  end
131
131
 
132
- def guard_rail_environment
133
- @guard_rail_environment || ::GuardRail.environment
134
- end
135
-
136
132
  # locks this db to a specific environment, except for
137
133
  # when doing writes (then it falls back to the current
138
134
  # value of GuardRail.environment)
139
135
  def guard!(environment = :secondary)
140
- @guard_rail_environment = environment
136
+ ::ActiveRecord::Base.connected_to_stack << { shard_roles: { id.to_sym => environment }, klasses: [::ActiveRecord::Base] }
141
137
  end
142
138
 
143
139
  def unguard!
144
- @guard_rail_environment = nil
140
+ ::ActiveRecord::Base.connected_to_stack << { shard_roles: { id.to_sym => :_switchman_inherit }, klasses: [::ActiveRecord::Base] }
145
141
  end
146
142
 
147
143
  def unguard
148
- old_env = @guard_rail_environment
149
- unguard!
150
- yield
151
- ensure
152
- guard!(old_env)
144
+ return yield unless ::ActiveRecord::Base.current_role_overriden?
145
+
146
+ begin
147
+ unguard!
148
+ yield
149
+ ensure
150
+ ::ActiveRecord::Base.connected_to_stack.pop
151
+ end
153
152
  end
154
153
 
155
154
  def shards
@@ -6,20 +6,17 @@ module Switchman
6
6
  def exec_queries(*args)
7
7
  if lock_value
8
8
  db = Shard.current(connection_classes).database_server
9
- return db.unguard { super } if ::GuardRail.environment != db.guard_rail_environment
9
+ db.unguard { super }
10
+ else
11
+ super
10
12
  end
11
- super
12
13
  end
13
14
 
14
15
  %w[update_all delete_all].each do |method|
15
16
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
16
17
  def #{method}(*args)
17
18
  db = Shard.current(connection_classes).database_server
18
- if ::GuardRail.environment != db.guard_rail_environment
19
- db.unguard { super }
20
- else
21
- super
22
- end
19
+ db.unguard { super }
23
20
  end
24
21
  RUBY
25
22
  end
@@ -3,6 +3,11 @@
3
3
  module Switchman
4
4
  module GuardRail
5
5
  module ClassMethods
6
+ def environment
7
+ # no overrides so we get the global role, not the role for the default shard
8
+ ::ActiveRecord::Base.current_role(without_overrides: true)
9
+ end
10
+
6
11
  def activate(role)
7
12
  DatabaseServer.send(:reference_role, role)
8
13
  super
@@ -25,6 +25,31 @@ module Switchman
25
25
  end
26
26
  end
27
27
 
28
+ class UndumpableResult
29
+ attr_reader :name
30
+
31
+ def initialize(result)
32
+ @name = result.inspect
33
+ end
34
+
35
+ def inspect
36
+ "#<UndumpableResult:#{name}>"
37
+ end
38
+ end
39
+
40
+ class ResultWrapper
41
+ attr_reader :result
42
+
43
+ def initialize(result)
44
+ @result =
45
+ begin
46
+ Marshal.dump(result) && result
47
+ rescue
48
+ UndumpableResult.new(result)
49
+ end
50
+ end
51
+ end
52
+
28
53
  class PrefixingIO
29
54
  delegate_missing_to :@original_io
30
55
 
@@ -16,7 +16,7 @@ module Switchman
16
16
  payload[:shard] = {
17
17
  database_server_id: shard.database_server.id,
18
18
  id: shard.id,
19
- env: shard.database_server.guard_rail_environment
19
+ env: @shard_host.pool.connection_klass&.current_role
20
20
  }
21
21
  end
22
22
  super name, payload
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchman
4
- VERSION = '3.0.15'
4
+ VERSION = '3.0.18'
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.0.15
4
+ version: 3.0.18
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: 2022-03-24 00:00:00.000000000 Z
13
+ date: 2022-04-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord