switchman 3.5.1 → 3.5.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: f381ad09908e23b0560c685c530f7ffae1a0adcbbaf6640b5ae4f267dd9323f3
4
- data.tar.gz: 1ca68afa9a03005c7c5966f8bc8a6be06f552c086ca469fe4b9eab1a54e819ae
3
+ metadata.gz: f6fdd3a18ba6864a5a47f6b151f397fb244ac7357c5474fe7195eba633a74a59
4
+ data.tar.gz: 93f9d372028f3807e5396a8d9a75e6f6b267f18d6aa8d46588e471609ce5ac9f
5
5
  SHA512:
6
- metadata.gz: 3340807bd81183c24527ba2739732e6ac5aa413d1184c22d4b1ad2d3cc0674b5222d1cf5ddb7abad158108a35e6c226bf9c7614f55fc890debbee0f20147ef98
7
- data.tar.gz: e67e961f5f4085ed7d0b00c272d8f8c911d14b284415e8bab2f528c25177db8504c8c7e8ceafa5e4ac5389e0f3f548f8d069838e21c6f8099864a91d5655d5a9
6
+ metadata.gz: 05c39ae962b7585a60e1f896702fbdc0a600c183d4a2c31173d74d710b9616ecb7cedb2ad36b1b65c62a4ee7d6eb0438eb50d482d8fa05a8d6d06469bc9e0643
7
+ data.tar.gz: '020828a35e723d21186d51acdf60c507fb3a1622bc94a43c560cdbd3945a026d6510c395d5308afb42f2029e95972799b67891d11ed5e60ae88b790a84011b6c'
@@ -131,7 +131,12 @@ module Switchman
131
131
  def grouped_calculation_options(operation, column_name, distinct)
132
132
  opts = { operation: operation, column_name: column_name, distinct: distinct }
133
133
 
134
- opts[:aggregate_alias] = aggregate_alias_for(operation, column_name)
134
+ # Rails 7.0.5
135
+ if defined?(::ActiveRecord::Calculations::ColumnAliasTracker)
136
+ column_alias_tracker = ::ActiveRecord::Calculations::ColumnAliasTracker.new(connection)
137
+ end
138
+
139
+ opts[:aggregate_alias] = aggregate_alias_for(operation, column_name, column_alias_tracker)
135
140
  group_attrs = group_values
136
141
  if group_attrs.first.respond_to?(:to_sym)
137
142
  association = klass.reflect_on_association(group_attrs.first.to_sym)
@@ -142,8 +147,14 @@ module Switchman
142
147
  group_fields = group_attrs
143
148
  end
144
149
 
145
- # to_s is because Rails 5 returns a string but Rails 6 returns a symbol.
146
- group_aliases = group_fields.map { |field| column_alias_for(field.downcase.to_s).to_s }
150
+ group_aliases = group_fields.map do |field|
151
+ field = connection.visitor.compile(field) if ::Arel.arel_node?(field)
152
+ if column_alias_tracker
153
+ column_alias_tracker.alias_for(field.to_s.downcase)
154
+ else
155
+ column_alias_for(field.to_s.downcase)
156
+ end
157
+ end
147
158
  group_columns = group_aliases.zip(group_fields).map do |aliaz, field|
148
159
  [aliaz, type_for(field), column_name_for(field)]
149
160
  end
@@ -156,11 +167,13 @@ module Switchman
156
167
  opts
157
168
  end
158
169
 
159
- def aggregate_alias_for(operation, column_name)
170
+ def aggregate_alias_for(operation, column_name, column_alias_tracker)
160
171
  if operation == "count" && column_name == :all
161
172
  "count_all"
162
173
  elsif operation == "average"
163
174
  "average"
175
+ elsif column_alias_tracker
176
+ column_alias_tracker.alias_for("#{operation} #{column_name}")
164
177
  else
165
178
  column_alias_for("#{operation} #{column_name}")
166
179
  end
@@ -27,12 +27,17 @@ 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
- # 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 do |k|
33
- config[k].is_a?(Hash) || (config[k].is_a?(Array) && config[k].all?(Hash))
30
+ if config.is_a?(Hash)
31
+ # It would be nice to do the auto-fallback that we want here, but we haven't
32
+ # actually done that for years (or maybe ever) and it will be a big lift to get working
33
+ roles = config.keys.select do |k|
34
+ config[k].is_a?(Hash) || (config[k].is_a?(Array) && config[k].all?(Hash))
35
+ end
36
+ base_config = config.except(*roles)
37
+ else
38
+ base_config = config
39
+ roles = []
34
40
  end
35
- base_config = config.except(*roles)
36
41
 
37
42
  name = "#{env_name}/primary"
38
43
  name = "primary" if env_name == default_env
@@ -81,6 +81,8 @@ module Switchman
81
81
  # Do this after so that all database servers for all roles are established and we won't prematurely
82
82
  # configure a connection for the wrong role
83
83
  @all_roles = roles.uniq
84
+ return @database_servers if @database_servers.empty?
85
+
84
86
  Shard.send(:configure_connects_to)
85
87
  end
86
88
  @database_servers
@@ -168,7 +168,7 @@ module Switchman
168
168
  # silly like dealloc'ing prepared statements)
169
169
  ::ActiveRecord::Base.clear_all_connections!
170
170
 
171
- parent_process_name = `ps -ocommand= -p#{Process.pid}`.slice(/#{$0}.*/)
171
+ parent_process_name = sanitized_process_title
172
172
  ret = ::Parallel.map(scopes, in_processes: (scopes.length > 1) ? parallel : 0) do |server, subscope|
173
173
  name = server.id
174
174
  last_description = name
@@ -444,6 +444,39 @@ module Switchman
444
444
  shard = nil unless shard.database_server
445
445
  shard
446
446
  end
447
+
448
+ # Determines the name of the current process, including arguments, but stripping
449
+ # any shebang from the invoked script, and any additional path info from the
450
+ # executable.
451
+ #
452
+ # @return [String]
453
+ def sanitized_process_title
454
+ # get the effective process name from `ps`; this will include any changes
455
+ # from Process.setproctitle _or_ assigning to $0.
456
+ parent_process_name = `ps -ocommand= -p#{Process.pid}`.strip
457
+ # Effective process titles may be shorter than the actual
458
+ # command; truncate our ARGV[0] so that they are comparable
459
+ # for the next step
460
+ argv0 = if parent_process_name.length < Process.argv0.length
461
+ Process.argv0[0..parent_process_name.length]
462
+ else
463
+ Process.argv0
464
+ end
465
+
466
+ # when running via a shebang, the `ps` output will include the shebang
467
+ # (i.e. it will be "ruby bin/rails c"); attempt to strip it off.
468
+ # Note that argv0 in this case will _only_ be `bin/rails` (no shebang,
469
+ # no arguments). We want to preserve the arguments we got from `ps`
470
+ if (index = parent_process_name.index(argv0))
471
+ parent_process_name.slice!(0...index)
472
+ end
473
+
474
+ # remove directories from the main executable to make more room
475
+ # for additional info
476
+ argv = parent_process_name.shellsplit
477
+ argv[0] = File.basename(argv[0])
478
+ argv.shelljoin
479
+ end
447
480
  end
448
481
 
449
482
  def name
@@ -508,48 +541,39 @@ module Switchman
508
541
  end
509
542
 
510
543
  def drop_database
511
- raise("Cannot drop the database of the default shard") if default?
544
+ raise "Cannot drop the database of the default shard" if default?
512
545
  return unless read_attribute(:name)
513
546
 
514
547
  begin
515
- adapter = database_server.config[:adapter]
516
- sharding_config = Switchman.config || {}
517
- drop_statement = sharding_config[adapter]&.[](:drop_statement)
518
- drop_statement ||= sharding_config[:drop_statement]
519
- if drop_statement
520
- drop_statement = Array(drop_statement).dup
521
- .map { |statement| statement.gsub("%{name}", name) }
548
+ activate do
549
+ self.class.drop_database(name)
522
550
  end
551
+ rescue ::ActiveRecord::StatementInvalid => e
552
+ logger.error "Drop failed: #{e}"
553
+ end
554
+ end
523
555
 
524
- case adapter
525
- when "mysql", "mysql2"
526
- activate do
527
- ::GuardRail.activate(:deploy) do
528
- drop_statement ||= "DROP DATABASE #{name}"
529
- Array(drop_statement).each do |stmt|
530
- ::ActiveRecord::Base.connection.execute(stmt)
531
- end
532
- end
533
- end
534
- when "postgresql"
535
- activate do
536
- ::GuardRail.activate(:deploy) do
537
- # Shut up, Postgres!
538
- conn = ::ActiveRecord::Base.connection
539
- old_proc = conn.raw_connection.set_notice_processor {}
540
- begin
541
- drop_statement ||= "DROP SCHEMA #{name} CASCADE"
542
- Array(drop_statement).each do |stmt|
543
- ::ActiveRecord::Base.connection.execute(stmt)
544
- end
545
- ensure
546
- conn.raw_connection.set_notice_processor(&old_proc) if old_proc
547
- end
548
- end
556
+ #
557
+ # Drops a specific database/schema from the currently active connection
558
+ #
559
+ def self.drop_database(name)
560
+ sharding_config = Switchman.config || {}
561
+ drop_statement = sharding_config["postgresql"]&.[](:drop_statement)
562
+ drop_statement ||= sharding_config[:drop_statement]
563
+ drop_statement = Array(drop_statement).map { |statement| statement.gsub("%{name}", name) } if drop_statement
564
+
565
+ ::GuardRail.activate(:deploy) do
566
+ # Shut up, Postgres!
567
+ conn = ::ActiveRecord::Base.connection
568
+ old_proc = conn.raw_connection.set_notice_processor {}
569
+ begin
570
+ drop_statement ||= "DROP SCHEMA #{name} CASCADE"
571
+ Array(drop_statement).each do |stmt|
572
+ ::ActiveRecord::Base.connection.execute(stmt)
549
573
  end
574
+ ensure
575
+ conn.raw_connection.set_notice_processor(&old_proc) if old_proc
550
576
  end
551
- rescue
552
- logger.info "Drop failed: #{$!}"
553
577
  end
554
578
  end
555
579
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchman
4
- VERSION = "3.5.1"
4
+ VERSION = "3.5.3"
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.5.1
4
+ version: 3.5.3
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: 2023-05-09 00:00:00.000000000 Z
13
+ date: 2023-06-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord