activerecord-shard_for 0.4.1 → 0.5.0

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
  SHA1:
3
- metadata.gz: 4922fc214f9808a86225606f05fec0236ec3bc51
4
- data.tar.gz: 6cdc0fa7c6bd162b71a163344b306ed16e8165a6
3
+ metadata.gz: 7598d89f3c2df373c8936c3df0e9e77e6b1f96f6
4
+ data.tar.gz: f9b9a4405849837c138184f68ad12e8374340c8f
5
5
  SHA512:
6
- metadata.gz: 20fb33271d1d5b08213e09c34b4ca4d0fc7455cbe92a2a60f0b2b9edc16b4dda4c21939fada4ed9c68f2da07db50370a6faf399304827d15d88006175b859c2e
7
- data.tar.gz: 749685bb5b2402582a73f9fc3672a2e3efb699d2bcfd839df834a27f65da50a48d74083e2b41e7b5156dfaa6d7175f8eb0be2cd9a01b7a231d0cbf6bbdd76d68
6
+ metadata.gz: 1f95be02a5787e073cb98f04059633cfb629c00e6c9854e22e0a590408162e0abd1299f5039f0f3710e1b79c7963f3d7c799094fe1e052668bbb848394df9089
7
+ data.tar.gz: 5f56d475a656397f86282a5930bbc2b0ab9c5aa2609bf453ca28ad92432251421331683041be42381ef9d57e5b438d23adc7f3a18f95807d44149882bb5b293d
data/.rubocop.yml CHANGED
@@ -27,3 +27,7 @@ Style/ParallelAssignment:
27
27
 
28
28
  Style/Alias:
29
29
  EnforcedStyle: prefer_alias_method
30
+
31
+ Metrics/BlockLength:
32
+ Exclude:
33
+ - 'spec/**/*.rb'
data/.travis.yml CHANGED
@@ -3,18 +3,20 @@ sudo: false
3
3
  rvm:
4
4
  - 2.2.4
5
5
  - 2.3.1
6
+ - 2.4.0
6
7
  branches:
7
8
  only:
8
9
  - master
9
- before_install: gem install bundler -v 1.12.5
10
+ before_install:
11
+ - gem update --system
12
+ - gem update --remote bundler
10
13
  cache: bundler
11
14
  script:
12
- - CODECLIMATE_REPO_TOKEN=dfc951e2d70b56ee62ffe989eb6286a6f21bf57209ccbd111bd336c372d8d06e bundle exec rspec
13
15
  - bundle exec rubocop
14
16
  - bundle exec ruby spec/performance_test.rb
17
+ - bundle exec rspec
18
+ - CODECLIMATE_REPO_TOKEN=dfc951e2d70b56ee62ffe989eb6286a6f21bf57209ccbd111bd336c372d8d06e bundle exec codeclimate-test-reporter
15
19
  gemfile:
16
- - gemfiles/ar_4.1.0.gemfile
17
- - gemfiles/ar_4.1.7.gemfile
18
20
  - gemfiles/ar_4.1.8.gemfile
19
21
  - gemfiles/ar_4.2.gemfile
20
22
  - gemfiles/ar_5.0.gemfile
@@ -27,6 +29,8 @@ matrix:
27
29
  gemfile: gemfiles/rails_edge.gemfile
28
30
  - rvm: 2.1.9
29
31
  gemfile: gemfiles/ar_5.0.gemfile
32
+ - rvm: 2.4.0
33
+ gemfile: gemfiles/ar_4.1.8.gemfile
30
34
  notifications:
31
35
  slack:
32
36
  secure: S1W/Lw+dH3wb8FfkMIWPZmr6M4Q6S2WMkSlanpKva1HM7K5QL7hXdmUl2yBUxJE26BHSsb1ScozMEzadyda2+i/W34UvZ7LXgKeHkUKEdjy/AmsSJPK1ZjMfgnv10tVgbEIusNb4bF/sSuChdZKK3ILwOlqIPDlQNdMwF1xRA2xt5J7tb26UgyIzoCI4P3bJYMULWsEkk+UwHiJH0YO9ulkTZI/j0N+hLXQLJTZPjmKtMk/tE0NbBmFVL4md89hUcR5gKTGGrNzEMJ58K+zqeDG/DubkcIbA5ZuqKv+oE5m0pDODZExxnC+oeENTvq/VfYwOfD0pTDrBNYjj+Bm3YiyGDzQAgov9XPDG8g/fKEs/LNAT79UZXkZlFO99Yn/vrYH9o5DKpOE9smENUXylb55MgLTUiYe17CTp7pB3trbJl3wwIbLjSmTjAdSUNgPv8qDP4uk3K4U32mknXCDDkU9EI7f6F731ocdoxsGarEBcPcgjs73Y84iwDteQp847Gigtgo4Y4TCWH657uzLolR2O8NSw+vWT0VNI9qtR5PZD7iVYtSp1qHtPAKowCztodewY2Nu+Ds9Z95udf4GPUkFg/SNEJPTPrQFLiiJZ8UYP8NJEuA+IP1tc2zG3zU/ADrjenRC1ZiupQG7OMH82y11408U6PcHFSlF+7NuDkac=
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG for activerecord-shard_for
2
2
 
3
+ ## 0.5.0
4
+
5
+ - Support Single Table Inheritance. [#14](https://github.com/yuemori/activerecord-shard_for/pull/14)
6
+
3
7
  ## 0.4.1
4
8
 
5
9
  - Fix defined_enums is empty. [#11](https://github.com/yuemori/activerecord-shard_for/pull/11)
@@ -26,14 +26,10 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'bundler', '~> 1.11'
27
27
  spec.add_development_dependency 'rake', '~> 10.0'
28
28
  spec.add_development_dependency 'rspec', '~> 3.0'
29
- spec.add_development_dependency 'pry'
30
- spec.add_development_dependency 'pry-doc'
31
- spec.add_development_dependency 'pry-stack_explorer'
32
- spec.add_development_dependency 'pry-inline'
33
- spec.add_development_dependency 'pry-rescue'
29
+ spec.add_development_dependency 'pry-byebug'
34
30
  spec.add_development_dependency 'rubocop'
35
31
  spec.add_development_dependency 'appraisal'
36
32
  spec.add_development_dependency 'sqlite3'
37
33
  spec.add_development_dependency 'rspec-parameterized'
38
- spec.add_development_dependency 'codeclimate-test-reporter'
34
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0.0'
39
35
  end
@@ -1,6 +1,7 @@
1
1
  require 'active_record'
2
2
  require 'expeditor'
3
3
  require 'activerecord/shard_for/version'
4
+ require 'activerecord/shard_for/abstract_shard_repository'
4
5
  require 'activerecord/shard_for/config'
5
6
  require 'activerecord/shard_for/cluster_config'
6
7
  require 'activerecord/shard_for/model'
@@ -14,6 +15,8 @@ require 'activerecord/shard_for/all_shards_in_parallel'
14
15
  require 'activerecord/shard_for/replication_mapping'
15
16
  require 'activerecord/shard_for/railtie' if defined?(Rails)
16
17
  require 'activerecord/shard_for/patch'
18
+ require 'activerecord/shard_for/sti'
19
+ require 'activerecord/shard_for/sti_shard_repository'
17
20
 
18
21
  module ActiveRecord
19
22
  module ShardFor
@@ -0,0 +1,37 @@
1
+ module ActiveRecord
2
+ module ShardFor
3
+ class AbstractShardRepository
4
+ attr_reader :shards
5
+
6
+ # @param [Symbol] connection_name
7
+ # @return [Class] A model class for this shard
8
+ def fetch(connection_name)
9
+ shards.fetch(connection_name)
10
+ end
11
+
12
+ # @param [Object] key sharding key object for connection
13
+ # @return [Class, nil] A AR model class.
14
+ def fetch_by_key(key)
15
+ shards.values.find do |model|
16
+ case model.assigned_key
17
+ when Range then model.assigned_key.include?(key)
18
+ else model.assigned_key == key
19
+ end
20
+ end
21
+ end
22
+
23
+ # @return [Array<Class>]
24
+ def all
25
+ shards.values
26
+ end
27
+
28
+ private
29
+
30
+ # @param [Symbol] connection_name
31
+ # @return [String]
32
+ def generate_class_name(connection_name)
33
+ "ShardFor#{connection_name.to_s.tr('-', '_').classify}"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -44,6 +44,13 @@ module ActiveRecord
44
44
  shard_repository.fetch(connection_name)
45
45
  end
46
46
 
47
+ # Evaluate block in all shard instances.
48
+ def shard_eval(&block)
49
+ all_shards.each do |shard|
50
+ shard.class_eval(&block)
51
+ end
52
+ end
53
+
47
54
  # Create new record with given attributes in proper shard for given key.
48
55
  # When distkey value is empty, raises ActiveRecord::ShardFor::MissingDistkeyAttribute
49
56
  # error.
@@ -1,6 +1,6 @@
1
1
  module ActiveRecord
2
2
  module ShardFor
3
- class ShardRepogitory
3
+ class ShardRepogitory < AbstractShardRepository
4
4
  attr_reader :base_class
5
5
 
6
6
  # @param [ClusterConfig] cluster_config
@@ -8,33 +8,11 @@ module ActiveRecord
8
8
  def initialize(cluster_config, base_class)
9
9
  @base_class = base_class
10
10
 
11
- shards = cluster_config.connection_registry.map do |key, connection_name|
12
- [connection_name, generate_model_for_shard(connection_name, key)]
11
+ @shards = cluster_config.connection_registry.each_with_object({}) do |(key, connection_name), hash|
12
+ model = generate_model_for_shard(connection_name, key)
13
+ base_class.const_set(:"#{generate_class_name(connection_name)}", model)
14
+ hash[connection_name] = model
13
15
  end
14
-
15
- @shards = Hash[shards]
16
- end
17
-
18
- # @param [Symbol] connection_name
19
- # @return [Class] A model class for this shard
20
- def fetch(connection_name)
21
- @shards.fetch(connection_name)
22
- end
23
-
24
- # @param [Object] key sharding key object for connection
25
- # @return [Class, nil] A AR model class.
26
- def fetch_by_key(key)
27
- @shards.values.find do |model|
28
- case model.assigned_key
29
- when Range then model.assigned_key.include?(key)
30
- else model.assigned_key == key
31
- end
32
- end
33
- end
34
-
35
- # @return [Array<Class>]
36
- def all
37
- @shards.values
38
16
  end
39
17
 
40
18
  private
@@ -63,12 +41,6 @@ module ActiveRecord
63
41
  model.class_eval { establish_connection(connection_name) }
64
42
  model
65
43
  end
66
-
67
- # @param [Symbol] connection_name
68
- # @return [String]
69
- def generate_class_name(connection_name)
70
- "ShardFor#{connection_name.to_s.tr('-', '_').classify}"
71
- end
72
44
  end
73
45
  end
74
46
  end
@@ -0,0 +1,12 @@
1
+ module ActiveRecord
2
+ module ShardFor
3
+ module STI
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ self.abstract_class = true
8
+ self.shard_repository = STIShardRepository.new(self, superclass.shard_repository.shards)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,40 @@
1
+ module ActiveRecord
2
+ module ShardFor
3
+ class STIShardRepository < AbstractShardRepository
4
+ attr_reader :inherited_class
5
+
6
+ # @param [Class] A sub class of AR model.
7
+ # @base_shards [Array<Class>] An array of shard models.
8
+ def initialize(inherited_class, base_shards)
9
+ @inherited_class = inherited_class
10
+
11
+ @shards = base_shards.each_with_object({}) do |(connection_name, base_model), hash|
12
+ model = generate_model_from_shard(connection_name, base_model)
13
+ inherited_class.const_set(:"#{generate_class_name(connection_name)}", model)
14
+ hash[connection_name] = model
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ # @param [Symbol] connection_name
21
+ # @param [Class] A class of shard model.
22
+ # @return [Class] A sub class of given model.
23
+ def generate_model_from_shard(connection_name, base_model)
24
+ class_name = generate_class_name(connection_name)
25
+ module_name = inherited_class.name
26
+
27
+ model = Class.new(base_model) do
28
+ @assigned_key = base_model.assigned_key
29
+
30
+ module_eval <<-RUBY, __FILE__, __LINE__ + 1
31
+ def self.name
32
+ "#{module_name}::#{class_name}"
33
+ end
34
+ RUBY
35
+ end
36
+ model
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module ShardFor
3
- VERSION = '0.4.1'.freeze
3
+ VERSION = '0.5.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-shard_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yuemori
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-05 00:00:00.000000000 Z
11
+ date: 2017-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -95,63 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '3.0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: pry
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: pry-doc
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: pry-stack_explorer
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: pry-inline
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'
153
- - !ruby/object:Gem::Dependency
154
- name: pry-rescue
98
+ name: pry-byebug
155
99
  requirement: !ruby/object:Gem::Requirement
156
100
  requirements:
157
101
  - - ">="
@@ -224,16 +168,16 @@ dependencies:
224
168
  name: codeclimate-test-reporter
225
169
  requirement: !ruby/object:Gem::Requirement
226
170
  requirements:
227
- - - ">="
171
+ - - "~>"
228
172
  - !ruby/object:Gem::Version
229
- version: '0'
173
+ version: 1.0.0
230
174
  type: :development
231
175
  prerelease: false
232
176
  version_requirements: !ruby/object:Gem::Requirement
233
177
  requirements:
234
- - - ">="
178
+ - - "~>"
235
179
  - !ruby/object:Gem::Version
236
- version: '0'
180
+ version: 1.0.0
237
181
  description: Database sharding library for ActiveRecord
238
182
  email:
239
183
  - yuemori@aiming-inc.com
@@ -263,6 +207,7 @@ files:
263
207
  - gemfiles/ar_5.0.gemfile
264
208
  - gemfiles/rails_edge.gemfile
265
209
  - lib/activerecord/shard_for.rb
210
+ - lib/activerecord/shard_for/abstract_shard_repository.rb
266
211
  - lib/activerecord/shard_for/all_shards_in_parallel.rb
267
212
  - lib/activerecord/shard_for/cluster_config.rb
268
213
  - lib/activerecord/shard_for/config.rb
@@ -276,6 +221,8 @@ files:
276
221
  - lib/activerecord/shard_for/railtie.rb
277
222
  - lib/activerecord/shard_for/replication_mapping.rb
278
223
  - lib/activerecord/shard_for/shard_repogitory.rb
224
+ - lib/activerecord/shard_for/sti.rb
225
+ - lib/activerecord/shard_for/sti_shard_repository.rb
279
226
  - lib/activerecord/shard_for/version.rb
280
227
  - lib/activerecord/tasks/activerecord_shard_for.rake
281
228
  homepage: https://github.com/yuemori/activerecord-shard_for
@@ -298,7 +245,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
298
245
  version: '0'
299
246
  requirements: []
300
247
  rubyforge_project:
301
- rubygems_version: 2.5.1
248
+ rubygems_version: 2.6.10
302
249
  signing_key:
303
250
  specification_version: 4
304
251
  summary: Database sharding library for ActiveRecord