switchman 3.0.16 → 3.0.19
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/switchman/shard.rb +1 -0
- data/lib/switchman/active_record/base.rb +27 -14
- data/lib/switchman/active_record/database_configurations.rb +4 -2
- data/lib/switchman/active_record/persistence.rb +1 -3
- data/lib/switchman/database_server.rb +11 -12
- data/lib/switchman/guard_rail/relation.rb +4 -7
- data/lib/switchman/guard_rail.rb +5 -0
- data/lib/switchman/sharded_instrumenter.rb +1 -1
- data/lib/switchman/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eea092610f18550e6ab0d24baa9a2746e8452341116c0b5e3a94c15b1bcb9be0
|
4
|
+
data.tar.gz: 53e0c2f7735838283311e57017de6bf020aae61f38dd394821a5bab5fdda869e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da4096ca84676fecda1bee5e99a9e7e34c1d4cc2233e4a32f391d68ccd9d93a8d641b5d4a5ecd330e5df340daca8611bad0226709ba9f9fc6653282821216e0a
|
7
|
+
data.tar.gz: d8a6f2245ac58e5ab64398245f7b14188cdb4675dea430fc2dd256b5aea73f68a81a30f5bcd3cd626634c492932bf85f3becab5dfa0a6e773abc6639615ab247
|
@@ -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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
171
|
-
|
172
|
-
super
|
185
|
+
db.unguard { super }
|
173
186
|
end
|
174
187
|
|
175
188
|
def id_for_database
|
@@ -27,7 +27,9 @@ module Switchman
|
|
27
27
|
return configs if configs.is_a?(Array)
|
28
28
|
|
29
29
|
db_configs = configs.flat_map do |env_name, config|
|
30
|
-
|
30
|
+
# It would be nice to do the auto-fallback that we want here, but we haven't
|
31
|
+
# actually done that for years (or maybe ever) and it will be a big lift to get working
|
32
|
+
roles = config.keys.select { |k| config[k].is_a?(Hash) || (config[k].is_a?(Array) && config[k].all? { |ck| ck.is_a?(Hash) }) }
|
31
33
|
base_config = config.except(*roles)
|
32
34
|
|
33
35
|
name = "#{env_name}/primary"
|
@@ -35,7 +37,7 @@ module Switchman
|
|
35
37
|
base_db = build_db_config_from_raw_config(env_name, name, base_config)
|
36
38
|
[base_db] + roles.map do |role|
|
37
39
|
build_db_config_from_raw_config(env_name, "#{env_name}/#{role}",
|
38
|
-
base_config.merge(config[role]))
|
40
|
+
base_config.merge(config[role].is_a?(Array) ? config[role].first : config[role]))
|
39
41
|
end
|
40
42
|
end
|
41
43
|
|
@@ -71,7 +71,7 @@ module Switchman
|
|
71
71
|
if role == 'primary'
|
72
72
|
@database_servers[name] = DatabaseServer.new(config.env_name, config.configuration_hash)
|
73
73
|
else
|
74
|
-
@database_servers[name].roles << role
|
74
|
+
@database_servers[name].roles << role.to_sym
|
75
75
|
end
|
76
76
|
end
|
77
77
|
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
|
-
|
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
|
-
|
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
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
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
|
-
|
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
|
-
|
19
|
-
db.unguard { super }
|
20
|
-
else
|
21
|
-
super
|
22
|
-
end
|
19
|
+
db.unguard { super }
|
23
20
|
end
|
24
21
|
RUBY
|
25
22
|
end
|
data/lib/switchman/guard_rail.rb
CHANGED
@@ -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
|
data/lib/switchman/version.rb
CHANGED
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.
|
4
|
+
version: 3.0.19
|
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-
|
13
|
+
date: 2022-04-06 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|