switchman 1.8.0 → 1.9.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e86d6e07988c45f8c70c42c92ece5263797af099
4
- data.tar.gz: 624f54c1a3735b12050b977177076fc3fc870027
3
+ metadata.gz: d2e63c8b9d8a43c1cf8716f775593bc0e5c7296b
4
+ data.tar.gz: c4cda98c0bbdf18d0c648adcf8d8465c90f2246e
5
5
  SHA512:
6
- metadata.gz: f30d8b5ee490486c13a4f800a09c2bc29ba266e98e839980242562b46f0e0e080e68d7039019d5a69e7944d232829bd592521e86f7206e5bd35cf42cf360a208
7
- data.tar.gz: 9e3b24c5493efce7a36e5da23a0dc471dc878a034125e8304dccb05c7beabef5daa009c87360f5e1c5bc9293f5b3892a5a624a86fffcbdea40fca44bb7878f8c
6
+ metadata.gz: f2e7e0889dce714d0d128eeca5b09434ba0cd313ebe9e09db2f1d58e7d706e6aa1353e03b54496d2f145fbd19467c74bd095870025eb6fca0321170c06ffd14a
7
+ data.tar.gz: e0614890e323dcedd8b272e1ec571be80de1640d27601d9c905208e4fca69931676ec03e4ef038775aa634e43751909bdf0b18f261fe32b1c8a394764c830d24
@@ -198,7 +198,7 @@ module Switchman
198
198
  scopes = Hash[database_servers.map do |server|
199
199
  server_scope = server.shards.merge(scope)
200
200
  if parallel == 1
201
- subscopes = [server_scope]
201
+ subscopes = server_scope
202
202
  else
203
203
  subscopes = []
204
204
  total = server_scope.count
@@ -0,0 +1,11 @@
1
+ class AddBackDefaultStringLimitsSwitchman < ActiveRecord::Migration
2
+ def up
3
+ add_string_limit_if_missing :switchman_shards, :name
4
+ add_string_limit_if_missing :switchman_shards, :database_server_id
5
+ end
6
+
7
+ def add_string_limit_if_missing(table, column)
8
+ return if column_exists?(table, column, :string, limit: 255)
9
+ change_column table, column, :string, limit: 255
10
+ end
11
+ end
@@ -1,16 +1,13 @@
1
1
  module Switchman
2
2
  module ActiveRecord
3
3
  module Calculations
4
- CALL_SUPER = Object.new.freeze
5
- private_constant :CALL_SUPER
6
4
 
7
5
  def pluck(*column_names)
8
- return super(*column_names[1..-1]) if column_names.first.equal?(CALL_SUPER)
9
6
  target_shard = Shard.current(klass.shard_category)
10
7
  shard_count = 0
11
8
  result = self.activate do |relation, shard|
12
9
  shard_count += 1
13
- results = relation.pluck(CALL_SUPER, *column_names)
10
+ results = relation.call_super(:pluck, Calculations, *column_names)
14
11
  if column_names.length > 1
15
12
  column_names.each_with_index do |column_name, idx|
16
13
  if klass.sharded_column?(column_name)
@@ -30,13 +27,12 @@ module Switchman
30
27
  result
31
28
  end
32
29
 
33
- def execute_simple_calculation(operation, column_name, distinct, super_method: false)
34
- return super(operation, column_name, distinct) if super_method
30
+ def execute_simple_calculation(operation, column_name, distinct)
35
31
  operation = operation.to_s.downcase
36
32
  if operation == "average"
37
33
  result = calculate_simple_average(column_name, distinct)
38
34
  else
39
- result = self.activate{ |relation| relation.send(:execute_simple_calculation, operation, column_name, distinct, super_method: true) }
35
+ result = self.activate{ |relation| relation.call_super(:execute_simple_calculation, Calculations, operation, column_name, distinct) }
40
36
  if result.is_a?(Array)
41
37
  case operation
42
38
  when "count", "sum"
@@ -1,9 +1,8 @@
1
1
  module Switchman
2
2
  module ActiveRecord
3
3
  module FinderMethods
4
- def find_one(id, call_super: false)
4
+ def find_one(id)
5
5
  return super(id) unless klass.integral_id?
6
- return super(id) if call_super
7
6
 
8
7
  if shard_source_value != :implicit
9
8
  current_shard = Shard.current(klass.shard_category)
@@ -14,7 +13,7 @@ module Switchman
14
13
  # skip the shard if the object can't be on it. unless we're only looking at one shard;
15
14
  # we might be expecting a shadow object
16
15
  next if current_id > Shard::IDS_PER_SHARD && self.all_shards.length > 1
17
- relation.send(:find_one, current_id, call_super: true)
16
+ relation.call_super(:find_one, FinderMethods, current_id)
18
17
  end
19
18
  if result.is_a?(Array)
20
19
  result = result.first
@@ -52,9 +51,9 @@ module Switchman
52
51
  relation = relation.where(table[primary_key].eq(conditions)) if conditions != :none
53
52
  end
54
53
 
55
- args = [relation, "#{name} Exists"]
56
- args << relation.bind_values
57
- relation.activate { return true if connection.select_value(*args) }
54
+ relation.activate do |shard_rel|
55
+ return true if connection.select_value(shard_rel, "#{name} Exists", shard_rel.bind_values)
56
+ end
58
57
  false
59
58
  end
60
59
  end
@@ -56,20 +56,40 @@ module Switchman
56
56
  SQL
57
57
  end
58
58
 
59
- def table_exists?(name)
59
+ method_name = ::Rails.version >= '5' ? :data_source_exists? : :table_exists?
60
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
61
+ def #{method_name}(name)
62
+ name = ::ActiveRecord::ConnectionAdapters::PostgreSQL::Utils.extract_schema_qualified_name(name.to_s)
63
+ return false unless name.identifier
64
+ if !name.schema && use_qualified_names?
65
+ name.instance_variable_set(:@schema, shard.name)
66
+ end
67
+
68
+ exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
69
+ SELECT COUNT(*)
70
+ FROM pg_class c
71
+ LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
72
+ WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view
73
+ AND c.relname = '\#{name.identifier}'
74
+ AND n.nspname = \#{name.schema ? "'\#{name.schema}'" : 'ANY (current_schemas(false))'}
75
+ SQL
76
+ end
77
+ RUBY
78
+
79
+ def view_exists?(name)
60
80
  name = ::ActiveRecord::ConnectionAdapters::PostgreSQL::Utils.extract_schema_qualified_name(name.to_s)
61
81
  return false unless name.identifier
62
82
  if !name.schema && use_qualified_names?
63
83
  name.instance_variable_set(:@schema, shard.name)
64
84
  end
65
85
 
66
- exec_query(<<-SQL, 'SCHEMA').rows.first[0].to_i > 0
67
- SELECT COUNT(*)
68
- FROM pg_class c
69
- LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
70
- WHERE c.relkind IN ('r','v','m') -- (r)elation/table, (v)iew, (m)aterialized view
71
- AND c.relname = '#{name.identifier}'
72
- AND n.nspname = #{name.schema ? "'#{name.schema}'" : 'ANY (current_schemas(false))'}
86
+ select_values(<<-SQL, 'SCHEMA').any?
87
+ SELECT c.relname
88
+ FROM pg_class c
89
+ LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
90
+ WHERE c.relkind IN ('v','m') -- (v)iew, (m)aterialized view
91
+ AND c.relname = '#{name.identifier}'
92
+ AND n.nspname = #{name.schema ? "'#{name.schema}'" : 'ANY (current_schemas(false))'}
73
93
  SQL
74
94
  end
75
95
 
@@ -175,7 +195,14 @@ module Switchman
175
195
  options[:on_delete] = extract_foreign_key_action(row['on_delete'])
176
196
  options[:on_update] = extract_foreign_key_action(row['on_update'])
177
197
 
178
- ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(table_name, row['to_table'], options)
198
+ # strip the schema name from to_table if it matches
199
+ to_table = row['to_table']
200
+ to_table_qualified_name = ::ActiveRecord::ConnectionAdapters::PostgreSQL::Utils.extract_schema_qualified_name(to_table)
201
+ if use_qualified_names? && to_table_qualified_name.schema == shard.name
202
+ to_table = to_table_qualified_name.identifier
203
+ end
204
+
205
+ ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(table_name, to_table, options)
179
206
  end
180
207
  end
181
208
  end
@@ -42,17 +42,15 @@ module Switchman
42
42
  primary_shard.activate(klass.shard_category) { super }
43
43
  end
44
44
 
45
- def explain(super_method: false)
46
- return super() if super_method
47
- self.activate { |relation| relation.explain(super_method: true) }
45
+ def explain
46
+ self.activate { |relation| relation.call_super(:explain, Relation) }
48
47
  end
49
48
 
50
49
  to_a_method = ::Rails.version >= '5' ? :records : :to_a
51
50
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
52
- def #{to_a_method}(super_method: false)
53
- return super() if super_method
51
+ def #{to_a_method}
54
52
  return @records if loaded?
55
- results = self.activate { |relation| relation.#{to_a_method}(super_method: true) }
53
+ results = self.activate { |relation| relation.call_super(#{to_a_method.inspect}, Relation) }
56
54
  case shard_value
57
55
  when Array, ::ActiveRecord::Relation, ::ActiveRecord::Base
58
56
  @records = results
@@ -62,14 +60,10 @@ module Switchman
62
60
  end
63
61
  RUBY
64
62
 
65
- CALL_SUPER = Object.new.freeze
66
- private_constant :CALL_SUPER
67
-
68
- %w{update_all delete_all}.each do |method|
63
+ %I{update_all delete_all}.each do |method|
69
64
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
70
65
  def #{method}(*args)
71
- return super(*args[1..-1]) if args.first.equal?(CALL_SUPER)
72
- result = self.activate { |relation| relation.#{method}(CALL_SUPER, *args) }
66
+ result = self.activate { |relation| relation.call_super(#{method.inspect}, Relation, *args) }
73
67
  result = result.sum if result.is_a?(Array)
74
68
  result
75
69
  end
@@ -0,0 +1,18 @@
1
+ module Switchman
2
+ module CallSuper
3
+ def super_method_above(method_name, above_module)
4
+ @super_methods ||= {}
5
+ @super_methods[[method_name, above_module]] ||= begin
6
+ method = method(method_name)
7
+ while method.owner != above_module
8
+ method = method.super_method
9
+ end
10
+ method.super_method
11
+ end
12
+ end
13
+
14
+ def call_super(method, above_module, *args, &block)
15
+ super_method_above(method, above_module).call(*args, &block)
16
+ end
17
+ end
18
+ end
@@ -84,6 +84,7 @@ module Switchman
84
84
  require "switchman/active_record/type_caster"
85
85
  require "switchman/active_record/where_clause_factory"
86
86
  require "switchman/arel"
87
+ require "switchman/call_super"
87
88
  require "switchman/rails"
88
89
  require "switchman/shackles/relation"
89
90
  require "switchman/shard_internal"
@@ -134,9 +135,10 @@ module Switchman
134
135
  ::ActiveRecord::Relation.prepend(ActiveRecord::Calculations)
135
136
  ::ActiveRecord::Relation.include(ActiveRecord::FinderMethods)
136
137
  ::ActiveRecord::Relation.include(ActiveRecord::QueryMethods)
138
+ ::ActiveRecord::Relation.prepend(Shackles::Relation)
137
139
  ::ActiveRecord::Relation.prepend(ActiveRecord::Relation)
138
140
  ::ActiveRecord::Relation.include(ActiveRecord::SpawnMethods)
139
- ::ActiveRecord::Relation.prepend(Shackles::Relation)
141
+ ::ActiveRecord::Relation.include(CallSuper)
140
142
 
141
143
  if ::Rails.version >= '5'
142
144
  ::ActiveRecord::Relation::WhereClauseFactory.prepend(ActiveRecord::WhereClauseFactory)
@@ -1,3 +1,3 @@
1
1
  module Switchman
2
- VERSION = "1.8.0"
2
+ VERSION = "1.9.0"
3
3
  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: 1.8.0
4
+ version: 1.9.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: 2016-11-08 00:00:00.000000000 Z
13
+ date: 2016-12-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: railties
@@ -190,6 +190,7 @@ files:
190
190
  - app/models/switchman/shard_internal.rb
191
191
  - db/migrate/20130328212039_create_switchman_shards.rb
192
192
  - db/migrate/20130328224244_create_default_shard.rb
193
+ - db/migrate/20161206323434_add_back_default_string_limits_switchman.rb
193
194
  - lib/switchman.rb
194
195
  - lib/switchman/action_controller/caching.rb
195
196
  - lib/switchman/active_record/abstract_adapter.rb
@@ -217,6 +218,7 @@ files:
217
218
  - lib/switchman/active_record/where_clause_factory.rb
218
219
  - lib/switchman/active_support/cache.rb
219
220
  - lib/switchman/arel.rb
221
+ - lib/switchman/call_super.rb
220
222
  - lib/switchman/connection_pool_proxy.rb
221
223
  - lib/switchman/database_server.rb
222
224
  - lib/switchman/default_shard.rb
@@ -246,7 +248,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
246
248
  requirements:
247
249
  - - ">="
248
250
  - !ruby/object:Gem::Version
249
- version: '2.0'
251
+ version: '2.3'
250
252
  required_rubygems_version: !ruby/object:Gem::Requirement
251
253
  requirements:
252
254
  - - ">="
@@ -254,7 +256,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
254
256
  version: '0'
255
257
  requirements: []
256
258
  rubyforge_project:
257
- rubygems_version: 2.5.1
259
+ rubygems_version: 2.5.2
258
260
  signing_key:
259
261
  specification_version: 4
260
262
  summary: Rails 4 sharding magic