switchman 3.6.7 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/switchman/active_record/associations.rb +1 -35
- data/lib/switchman/active_record/attribute_methods.rb +1 -6
- data/lib/switchman/active_record/base.rb +1 -11
- data/lib/switchman/active_record/calculations.rb +4 -13
- data/lib/switchman/active_record/connection_pool.rb +1 -1
- data/lib/switchman/active_record/relation.rb +1 -1
- data/lib/switchman/engine.rb +4 -17
- data/lib/switchman/r_spec_helper.rb +1 -0
- data/lib/switchman/sharded_instrumenter.rb +1 -5
- data/lib/switchman/version.rb +1 -1
- data/lib/tasks/switchman.rake +3 -33
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb5bc75d858d97e55442dfdafcd1826d7040fe1bd4819bc3c2ee6053257a72ab
|
4
|
+
data.tar.gz: e73beaf3a5987f26e2402078f66c028831afc6924f21546d0ca1cb35f70e0831
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 273718d8bc773ded149ac5d81509a6d85c6858f17bc7c1a03f3dbdf15bbb96f101fea5842eddf87ba442aabf1f0bffe1a21421da4b9329548b0f40e51de26645
|
7
|
+
data.tar.gz: 9f81d1963fe213d334aed7377057fe29ecbdb637c6b2b8a33a49d52fcc0f726e1a8e87853a6e1030c8c2b0bbaff51e369d54cf8118f1c512ca0d62bf5684267c
|
@@ -203,41 +203,7 @@ module Switchman
|
|
203
203
|
# #compare_by_identity makes such owners different hash keys
|
204
204
|
@records_by_owner = {}.compare_by_identity
|
205
205
|
|
206
|
-
|
207
|
-
raw_records ||= loader_query.records_for([self])
|
208
|
-
elsif owner_keys.empty?
|
209
|
-
raw_records ||= []
|
210
|
-
else
|
211
|
-
# determine the shard to search for each owner
|
212
|
-
if reflection.macro == :belongs_to
|
213
|
-
# for belongs_to, it's the shard of the foreign_key
|
214
|
-
partition_proc = lambda do |owner|
|
215
|
-
if owner.class.sharded_column?(owner_key_name)
|
216
|
-
Shard.shard_for(owner[owner_key_name], owner.shard)
|
217
|
-
else
|
218
|
-
Shard.current
|
219
|
-
end
|
220
|
-
end
|
221
|
-
elsif !reflection.options[:multishard]
|
222
|
-
# for non-multishard associations, it's *just* the owner's shard
|
223
|
-
partition_proc = ->(owner) { owner.shard }
|
224
|
-
end
|
225
|
-
|
226
|
-
raw_records ||= Shard.partition_by_shard(owners, partition_proc) do |partitioned_owners|
|
227
|
-
relative_owner_keys = partitioned_owners.map do |owner|
|
228
|
-
key = owner[owner_key_name]
|
229
|
-
if key && owner.class.sharded_column?(owner_key_name)
|
230
|
-
key = Shard.relative_id_for(key,
|
231
|
-
owner.shard,
|
232
|
-
Shard.current(klass.connection_class_for_self))
|
233
|
-
end
|
234
|
-
convert_key(key)
|
235
|
-
end
|
236
|
-
relative_owner_keys.compact!
|
237
|
-
relative_owner_keys.uniq!
|
238
|
-
records_for(relative_owner_keys)
|
239
|
-
end
|
240
|
-
end
|
206
|
+
raw_records ||= loader_query.records_for([self])
|
241
207
|
|
242
208
|
@preloaded_records = raw_records.select do |record|
|
243
209
|
assignments = false
|
@@ -55,12 +55,7 @@ module Switchman
|
|
55
55
|
end
|
56
56
|
|
57
57
|
def define_cached_method(owner, name, namespace:, as:, &block)
|
58
|
-
|
59
|
-
yield owner
|
60
|
-
owner.rename_method(as, name)
|
61
|
-
else
|
62
|
-
owner.define_cached_method(name, namespace: namespace, as: as, &block)
|
63
|
-
end
|
58
|
+
owner.define_cached_method(name, namespace: namespace, as: as, &block)
|
64
59
|
end
|
65
60
|
|
66
61
|
def define_method_global_attribute(attr_name, owner:)
|
@@ -136,11 +136,7 @@ module Switchman
|
|
136
136
|
end
|
137
137
|
|
138
138
|
def connected_to_stack
|
139
|
-
has_own_stack =
|
140
|
-
Thread.current.thread_variable?(:ar_connected_to_stack)
|
141
|
-
else
|
142
|
-
::ActiveSupport::IsolatedExecutionState.key?(:active_record_connected_to_stack)
|
143
|
-
end
|
139
|
+
has_own_stack = ::ActiveSupport::IsolatedExecutionState.key?(:active_record_connected_to_stack)
|
144
140
|
|
145
141
|
ret = super
|
146
142
|
return ret if has_own_stack
|
@@ -189,12 +185,6 @@ module Switchman
|
|
189
185
|
|
190
186
|
Shard.default
|
191
187
|
end
|
192
|
-
|
193
|
-
if ::Rails.version < "7.0"
|
194
|
-
def connection_class_for_self
|
195
|
-
connection_classes
|
196
|
-
end
|
197
|
-
end
|
198
188
|
end
|
199
189
|
|
200
190
|
def self.prepended(klass)
|
@@ -148,19 +148,10 @@ module Switchman
|
|
148
148
|
private
|
149
149
|
|
150
150
|
def type_cast_calculated_value_switchman(value, column_name, operation)
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
lookup_cast_type_from_join_dependencies(column_name.to_s) || ::ActiveRecord::Type.default_value
|
156
|
-
type.deserialize(val)
|
157
|
-
end
|
158
|
-
else
|
159
|
-
column = aggregate_column(column_name)
|
160
|
-
type ||= column.try(:type_caster) ||
|
161
|
-
lookup_cast_type_from_join_dependencies(column_name.to_s) || ::ActiveRecord::Type.default_value
|
162
|
-
type_cast_calculated_value(value, operation, type)
|
163
|
-
end
|
151
|
+
column = aggregate_column(column_name)
|
152
|
+
type ||= column.try(:type_caster) ||
|
153
|
+
lookup_cast_type_from_join_dependencies(column_name.to_s) || ::ActiveRecord::Type.default_value
|
154
|
+
type_cast_calculated_value(value, operation, type)
|
164
155
|
end
|
165
156
|
|
166
157
|
def column_name_for(field)
|
data/lib/switchman/engine.rb
CHANGED
@@ -10,20 +10,9 @@ module Switchman
|
|
10
10
|
|
11
11
|
::GuardRail.singleton_class.prepend(GuardRail::ClassMethods)
|
12
12
|
|
13
|
-
# after :initialize_dependency_mechanism to ensure autoloading is
|
14
|
-
# configured for any downstream initializers that care. In rails 7.0 we
|
15
|
-
# should be able to just use an explicit after on configuring the once
|
16
|
-
# autoloaders and not need to go monkey around with initializer order
|
17
|
-
if ::Rails.version < "7.0"
|
18
|
-
initialize_dependency_mechanism = ::Rails::Application::Bootstrap.initializers.find do |i|
|
19
|
-
i.name == :initialize_dependency_mechanism
|
20
|
-
end
|
21
|
-
initialize_dependency_mechanism.instance_variable_get(:@options)[:after] = :set_autoload_paths
|
22
|
-
end
|
23
|
-
|
24
13
|
initializer "switchman.active_record_patch",
|
25
14
|
before: "active_record.initialize_database",
|
26
|
-
after:
|
15
|
+
after: :setup_once_autoloader do
|
27
16
|
::ActiveSupport.on_load(:active_record) do
|
28
17
|
# Switchman requires postgres, so just always load the pg adapter
|
29
18
|
require "active_record/connection_adapters/postgresql_adapter"
|
@@ -54,11 +43,9 @@ module Switchman
|
|
54
43
|
::ActiveRecord::Associations::CollectionProxy.include(ActiveRecord::Associations::CollectionProxy)
|
55
44
|
|
56
45
|
::ActiveRecord::Associations::Preloader::Association.prepend(ActiveRecord::Associations::Preloader::Association)
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
)
|
61
|
-
end
|
46
|
+
::ActiveRecord::Associations::Preloader::Association::LoaderRecords.prepend(
|
47
|
+
ActiveRecord::Associations::Preloader::Association::LoaderRecords
|
48
|
+
)
|
62
49
|
::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(ActiveRecord::AbstractAdapter)
|
63
50
|
unless ::Rails.version < "7.1"
|
64
51
|
::ActiveRecord::ConnectionAdapters::ConnectionHandler.prepend(ActiveRecord::ConnectionHandler)
|
@@ -16,11 +16,7 @@ module Switchman
|
|
16
16
|
payload[:shard] = {
|
17
17
|
database_server_id: shard.database_server.id,
|
18
18
|
id: shard.id,
|
19
|
-
env:
|
20
|
-
@shard_host.pool.connection_klass&.current_role
|
21
|
-
else
|
22
|
-
@shard_host.pool.connection_class&.current_role
|
23
|
-
end
|
19
|
+
env: @shard_host.pool.connection_class&.current_role
|
24
20
|
}
|
25
21
|
end
|
26
22
|
super(name, payload)
|
data/lib/switchman/version.rb
CHANGED
data/lib/tasks/switchman.rake
CHANGED
@@ -1,16 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# In rails 7.0+ if you have only 1 db in the env it doesn't try to do explicit activation
|
4
|
-
# (and for rails purposes we only have one db per env because each database server is a separate env)
|
5
|
-
if Rails.version < "7.0"
|
6
|
-
task_prefix = Rake::Task.task_defined?("app:db:migrate") ? "app:db" : "db"
|
7
|
-
Rake::Task["#{task_prefix}:migrate"].clear_actions.enhance do
|
8
|
-
ActiveRecord::Tasks::DatabaseTasks.migrate
|
9
|
-
# Ensure this doesn't blow up when running inside the dummy app
|
10
|
-
Rake::Task["#{task_prefix}:_dump"].invoke
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
3
|
module Switchman
|
15
4
|
module Rake
|
16
5
|
def self.filter_database_servers
|
@@ -265,28 +254,9 @@ module Switchman
|
|
265
254
|
|
266
255
|
module ActiveRecord
|
267
256
|
module PostgreSQLDatabaseTasks
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
args = ["--schema-only", "--no-privileges", "--no-owner", "--file", filename]
|
272
|
-
args.concat(Array(extra_flags)) if extra_flags
|
273
|
-
shard = Shard.current.name
|
274
|
-
serialized_search_path = shard
|
275
|
-
args << "--schema=#{Shellwords.escape(shard)}"
|
276
|
-
|
277
|
-
ignore_tables = ::ActiveRecord::SchemaDumper.ignore_tables
|
278
|
-
args += ignore_tables.flat_map { |table| ["-T", table] } if ignore_tables.any?
|
279
|
-
|
280
|
-
args << db_config.database
|
281
|
-
run_cmd("pg_dump", args, "dumping")
|
282
|
-
remove_sql_header_comments(filename)
|
283
|
-
File.open(filename, "a") { |f| f << "SET search_path TO #{serialized_search_path};\n\n" }
|
284
|
-
end
|
285
|
-
else
|
286
|
-
def structure_dump(...)
|
287
|
-
::ActiveRecord.dump_schemas = Switchman::Shard.current.name
|
288
|
-
super
|
289
|
-
end
|
257
|
+
def structure_dump(...)
|
258
|
+
::ActiveRecord.dump_schemas = Switchman::Shard.current.name
|
259
|
+
super
|
290
260
|
end
|
291
261
|
end
|
292
262
|
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:
|
4
|
+
version: 4.0.0
|
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:
|
13
|
+
date: 2025-02-12 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
21
|
+
version: '7.0'
|
22
22
|
- - "<"
|
23
23
|
- !ruby/object:Gem::Version
|
24
24
|
version: '7.2'
|
@@ -28,7 +28,7 @@ dependencies:
|
|
28
28
|
requirements:
|
29
29
|
- - ">="
|
30
30
|
- !ruby/object:Gem::Version
|
31
|
-
version:
|
31
|
+
version: '7.0'
|
32
32
|
- - "<"
|
33
33
|
- !ruby/object:Gem::Version
|
34
34
|
version: '7.2'
|
@@ -66,7 +66,7 @@ dependencies:
|
|
66
66
|
requirements:
|
67
67
|
- - ">="
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
version: '
|
69
|
+
version: '7.0'
|
70
70
|
- - "<"
|
71
71
|
- !ruby/object:Gem::Version
|
72
72
|
version: '7.2'
|
@@ -76,7 +76,7 @@ dependencies:
|
|
76
76
|
requirements:
|
77
77
|
- - ">="
|
78
78
|
- !ruby/object:Gem::Version
|
79
|
-
version: '
|
79
|
+
version: '7.0'
|
80
80
|
- - "<"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '7.2'
|