active_record_shards 3.1.0 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b63e3aedfb88c46446154302a20fffb1064c186
4
+ data.tar.gz: 3876c52d8d919ea210fce8c0697ec65475d6ca70
5
+ SHA512:
6
+ metadata.gz: bfb0ab5ee2325a86ed7058e3fc0131984e1ff4730a630a518d10bb815d25bc664f6e289657ccec9c2b58707402510cdc7d345657f458dc0793712ea7502ccf1c
7
+ data.tar.gz: 304ea258e43a354cca27bbf611d657db0c0f102f619cf7ddfdbb5f6854e6938069da7ad453bf1ba2c61ef25329359f83224cfa40287e56058a2db7c625f1316e
@@ -32,10 +32,14 @@ if ActiveRecord.const_defined?(:Relation)
32
32
  ActiveRecord::Relation.send(:include, ActiveRecordShards::DefaultSlavePatches::ActiveRelationPatches)
33
33
  end
34
34
 
35
- if ActiveRecord::Associations.const_defined?(:Preloader)
35
+ if ActiveRecord::Associations.const_defined?(:Preloader) && ActiveRecord::Associations::Preloader.const_defined?(:HasAndBelongsToMany)
36
36
  ActiveRecord::Associations::Preloader::HasAndBelongsToMany.send(:include, ActiveRecordShards::DefaultSlavePatches::HasAndBelongsToManyPreloaderPatches)
37
37
  end
38
38
 
39
+ if ActiveRecord::VERSION::MAJOR >= 4 && ActiveRecord::VERSION::MINOR >= 1
40
+ ActiveRecord::Associations::Builder::HasAndBelongsToMany.send(:include, ActiveRecordShards::DefaultSlavePatches::Rails41HasAndBelongsToManyBuilderExtension)
41
+ end
42
+
39
43
  ActiveRecord::Associations::CollectionProxy.send(:include, ActiveRecordShards::AssociationCollectionConnectionSelection)
40
44
 
41
45
  if ActiveRecord::VERSION::MAJOR >= 4 && RUBY_VERSION >= '2'
@@ -1,7 +1,14 @@
1
1
  class ActiveRecord::Base
2
2
  def self.establish_connection(spec = ENV["DATABASE_URL"])
3
- resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new spec, configurations
4
- spec = resolver.spec
3
+ if ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 1
4
+ spec ||= DEFAULT_ENV.call
5
+ spec = spec.to_sym if spec.is_a?(String)
6
+ resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new configurations
7
+ spec = resolver.spec(spec)
8
+ else
9
+ resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new spec, configurations
10
+ spec = resolver.spec
11
+ end
5
12
 
6
13
  unless respond_to?(spec.adapter_method)
7
14
  raise AdapterNotFound, "database configuration specifies nonexistent #{spec.config[:adapter]} adapter"
@@ -1,3 +1,5 @@
1
+ require 'active_record_shards/shard_support'
2
+
1
3
  module ActiveRecordShards
2
4
  module ConnectionSwitcher
3
5
  def self.extended(klass)
@@ -23,6 +25,10 @@ module ActiveRecordShards
23
25
  on_shard(shard_name) { yield }
24
26
  end
25
27
 
28
+ def shards
29
+ ShardSupport.new(self == ActiveRecord::Base ? nil : where(nil))
30
+ end
31
+
26
32
  def on_all_shards(&block)
27
33
  old_options = current_shard_selection.options
28
34
  if supports_sharding?
@@ -173,8 +179,13 @@ module ActiveRecordShards
173
179
  # note that since we're subverting the standard establish_connection path, we have to handle the funky autoloading of the
174
180
  # connection adapter ourselves.
175
181
  specification_cache[name] ||= begin
176
- resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new spec, configurations
177
- resolver.spec
182
+ if ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 1
183
+ resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new configurations
184
+ resolver.spec(spec)
185
+ else
186
+ resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new spec, configurations
187
+ resolver.spec
188
+ end
178
189
  end
179
190
 
180
191
  if ActiveRecord::VERSION::MAJOR >= 4
@@ -69,8 +69,10 @@ module ActiveRecordShards
69
69
  alias_method_chain :transaction, :slave_off
70
70
  end
71
71
  end
72
- ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, ActiveRecord::Associations::HasAndBelongsToManyAssociation, :construct_sql)
73
- ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, ActiveRecord::Associations::HasAndBelongsToManyAssociation, :construct_find_options!)
72
+ if ActiveRecord::Associations.const_defined?(:HasAndBelongsToManyAssociation)
73
+ ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, ActiveRecord::Associations::HasAndBelongsToManyAssociation, :construct_sql)
74
+ ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, ActiveRecord::Associations::HasAndBelongsToManyAssociation, :construct_find_options!)
75
+ end
74
76
  end
75
77
 
76
78
  def on_slave_unless_tx(&block)
@@ -106,5 +108,24 @@ module ActiveRecordShards
106
108
  on_slave_unless_tx { exists_without_default_slave?(*args, &block) }
107
109
  end
108
110
  end
111
+
112
+ # in rails 4.1+, they create a join class that's used to pull in records for HABTM.
113
+ # this simplifies the hell out of our existence, because all we have to do is inerit on-slave-by-default
114
+ # down from the parent now.
115
+ module Rails41HasAndBelongsToManyBuilderExtension
116
+ def self.included(base)
117
+ base.class_eval do
118
+ alias_method_chain :through_model, :inherit_default_slave_from_lhs
119
+ end
120
+ end
121
+
122
+ def through_model_with_inherit_default_slave_from_lhs
123
+ model = through_model_without_inherit_default_slave_from_lhs
124
+ def model.on_slave_by_default?
125
+ left_reflection.klass.on_slave_by_default?
126
+ end
127
+ model
128
+ end
129
+ end
109
130
  end
110
131
  end
@@ -0,0 +1,48 @@
1
+ module ActiveRecordShards
2
+ class ShardSupport
3
+ class ShardEnumerator
4
+ include Enumerable
5
+
6
+ def each(&block)
7
+ ActiveRecord::Base.on_all_shards(&block)
8
+ end
9
+ end
10
+
11
+ def initialize(scope)
12
+ @scope = scope
13
+ end
14
+
15
+ def enum
16
+ ShardEnumerator.new
17
+ end
18
+
19
+ def find(*find_args)
20
+ ensure_concrete!
21
+
22
+ exception = nil
23
+ enum.each do
24
+ begin
25
+ record = @scope.find(*find_args)
26
+ return record if record
27
+ rescue ActiveRecord::RecordNotFound => e
28
+ exception = e
29
+ end
30
+ end
31
+ raise exception
32
+ end
33
+
34
+ def count
35
+ enum.inject(0) { |accum, shard| @scope.clone.count + accum }
36
+ end
37
+
38
+ def to_a
39
+ enum.flat_map { @scope.clone.to_a }
40
+ end
41
+
42
+ private
43
+ def ensure_concrete!
44
+ raise "Please call this method on a concrete model, not an abstract class!" if @scope.abstract_class?
45
+ end
46
+ end
47
+ end
48
+
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_shards
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
5
- prerelease:
4
+ version: 3.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Mick Staugaard
@@ -11,30 +10,146 @@ authors:
11
10
  autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2014-07-09 00:00:00.000000000 Z
13
+ date: 2014-11-11 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: activerecord
18
17
  requirement: !ruby/object:Gem::Requirement
19
- none: false
20
18
  requirements:
21
- - - ! '>='
19
+ - - ">="
22
20
  - !ruby/object:Gem::Version
23
21
  version: 3.2.16
24
- - - <=
22
+ - - "<"
25
23
  - !ruby/object:Gem::Version
26
- version: '4.1'
24
+ version: '4.2'
27
25
  type: :runtime
28
26
  prerelease: false
29
27
  version_requirements: !ruby/object:Gem::Requirement
30
- none: false
31
28
  requirements:
32
- - - ! '>='
29
+ - - ">="
33
30
  - !ruby/object:Gem::Version
34
31
  version: 3.2.16
35
- - - <=
32
+ - - "<"
36
33
  - !ruby/object:Gem::Version
37
- version: '4.1'
34
+ version: '4.2'
35
+ - !ruby/object:Gem::Dependency
36
+ name: activesupport
37
+ requirement: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 3.2.16
42
+ - - "<"
43
+ - !ruby/object:Gem::Version
44
+ version: '4.2'
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 3.2.16
52
+ - - "<"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: wwtd
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mysql2
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bump
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: minitest
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: minitest-rg
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: mocha
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
38
153
  description: Easily run queries on shard and slave databases.
39
154
  email:
40
155
  - mick@staugaard.com
@@ -44,6 +159,8 @@ executables: []
44
159
  extensions: []
45
160
  extra_rdoc_files: []
46
161
  files:
162
+ - README.md
163
+ - lib/active_record_shards.rb
47
164
  - lib/active_record_shards/association_collection_connection_selection.rb
48
165
  - lib/active_record_shards/configuration_parser.rb
49
166
  - lib/active_record_shards/connection_handler.rb
@@ -55,32 +172,30 @@ files:
55
172
  - lib/active_record_shards/model.rb
56
173
  - lib/active_record_shards/schema_dumper_extension.rb
57
174
  - lib/active_record_shards/shard_selection.rb
175
+ - lib/active_record_shards/shard_support.rb
58
176
  - lib/active_record_shards/tasks.rb
59
- - lib/active_record_shards.rb
60
- - README.md
61
177
  homepage: https://github.com/zendesk/active_record_shards
62
178
  licenses:
63
179
  - MIT
180
+ metadata: {}
64
181
  post_install_message:
65
182
  rdoc_options: []
66
183
  require_paths:
67
184
  - lib
68
185
  required_ruby_version: !ruby/object:Gem::Requirement
69
- none: false
70
186
  requirements:
71
- - - ! '>='
187
+ - - ">="
72
188
  - !ruby/object:Gem::Version
73
189
  version: '0'
74
190
  required_rubygems_version: !ruby/object:Gem::Requirement
75
- none: false
76
191
  requirements:
77
- - - ! '>='
192
+ - - ">="
78
193
  - !ruby/object:Gem::Version
79
194
  version: '0'
80
195
  requirements: []
81
196
  rubyforge_project:
82
- rubygems_version: 1.8.23.2
197
+ rubygems_version: 2.2.2
83
198
  signing_key:
84
- specification_version: 3
199
+ specification_version: 4
85
200
  summary: Simple database switching for ActiveRecord.
86
201
  test_files: []