mixed_gauge 0.1.2 → 0.1.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
  SHA1:
3
- metadata.gz: af67019109e77025f6dcc080900efb12622b9f7e
4
- data.tar.gz: be1c25c01c401d06286e26e1075bbd29f5df5d6c
3
+ metadata.gz: 4dc2c3036f197760c0c28b3fe7f6dd28519a2076
4
+ data.tar.gz: f2098922ed048918a03ade6b860447bbe2eb519a
5
5
  SHA512:
6
- metadata.gz: 70a434108a0fecee84f08f63d79a74a751c91396352dac7682be3031fbd80249a7dac288cdd2a137993411ff40b375697889d4c01a0409686bc4486421e88bf7
7
- data.tar.gz: 0b415db64f93f88422fab59c290d504a65e546a2e4d49cb33bf75178f5f825a4e1fb4590ff199c25c6342407622412b700bfc01448c1d3f06400b8127d210028
6
+ metadata.gz: 7ff5439bd44a28a0a15f6ec630bdd3d3afb46fb3d3437c64b6e8a8acc98815fa78b983f4a1c7213a143a89196b646e93e9de395ad8e0d34e5c5150e26b73bbef
7
+ data.tar.gz: 7161650054c1a237bd7b9e12418afaef05d886b837cd60c88fa86d4b1c9d07df73bf30798c213e4b8fd958c24eafb5daa6155c6fe20ac9ab10da2f3f268fd6c0
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /gemfiles/*.lock
data/.rspec CHANGED
@@ -1,2 +1 @@
1
- --format documentation
2
1
  --color
@@ -1,5 +1,24 @@
1
1
  language: ruby
2
+ sudo: false
3
+ branches:
4
+ only:
5
+ - master
2
6
  rvm:
3
- - '2.0'
4
- - '2.1'
5
- - '2.2'
7
+ - 2.0
8
+ - 2.1
9
+ - 2.2
10
+ gemfile:
11
+ - gemfiles/ar_4.1.0.gemfile
12
+ - gemfiles/ar_4.1.7.gemfile
13
+ - gemfiles/ar_4.1.8.gemfile
14
+ - gemfiles/ar_4.2.gemfile
15
+ - gemfiles/rails_edge.gemfile
16
+ matrix:
17
+ allow_failures:
18
+ - gemfile: gemfiles/rails_edge.gemfile
19
+ exclude:
20
+ # Rails 5 requires to run on Ruby 2.2.0 or newer.
21
+ - rvm: 2.0
22
+ gemfile: gemfiles/rails_edge.gemfile
23
+ - rvm: 2.1
24
+ gemfile: gemfiles/rails_edge.gemfile
@@ -0,0 +1,22 @@
1
+ appraise 'ar-4.1.0' do
2
+ gem 'activerecord', '4.1.0'
3
+ end
4
+
5
+ appraise 'ar-4.1.7' do
6
+ gem 'activerecord', '4.1.7'
7
+ end
8
+
9
+ appraise 'ar-4.1.8' do
10
+ gem 'activerecord', '4.1.8'
11
+ end
12
+
13
+ appraise 'ar-4.2' do
14
+ gem 'activerecord', '~> 4.2'
15
+ end
16
+
17
+ appraise 'rails-edge' do
18
+ gem 'activerecord', github: 'rails/rails'
19
+ gem 'arel', github: 'rails/arel'
20
+ end
21
+
22
+ # vim: set ft=ruby:
@@ -1,3 +1,8 @@
1
+ # CHANGELOG for mixed_gauge
2
+
3
+ ## 0.1.3
4
+ - Rake tasks to setup database cluster.
5
+
1
6
  ## 0.1.2
2
7
  - Enable to register before hook on `.put!`.
3
8
  - Enable to define arbitrary methods to model class.
data/README.md CHANGED
@@ -1,7 +1,17 @@
1
1
  # MixedGauge
2
2
  [![Build Status](https://travis-ci.org/taiki45/mixed_gauge.svg?branch=master)](https://travis-ci.org/taiki45/mixed_gauge) [![Coverage Status](https://coveralls.io/repos/taiki45/mixed_gauge/badge.svg?branch=master)](https://coveralls.io/r/taiki45/mixed_gauge?branch=master) [![Code Climate](https://codeclimate.com/github/taiki45/mixed_gauge/badges/gpa.svg)](https://codeclimate.com/github/taiki45/mixed_gauge) [![Gem Version](https://badge.fury.io/rb/mixed_gauge.svg)](http://badge.fury.io/rb/mixed_gauge)
3
3
 
4
- An ActiveRecord extension for database sharding.
4
+ A simple and robust ActiveRecord extension for database sharding.
5
+ mixed_gauge offers shards management with hash slots, re-sharding support,
6
+ KVS actions, some ActiveRecord::Base actions.
7
+
8
+ ## Concept
9
+ Shard management is based on hash slots mechanism.
10
+
11
+ TODO: more doc.
12
+
13
+ ## Why and When to use mixed_gauge
14
+ TODO
5
15
 
6
16
  ## Usage
7
17
 
data/Rakefile CHANGED
@@ -1,3 +1,6 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
1
4
  require 'bundler/gem_tasks'
2
5
 
3
6
  begin
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "4.1.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "4.1.7"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "4.1.8"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.2"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", :github => "rails/rails"
6
+ gem "arel", :github => "rails/arel"
7
+
8
+ gemspec :path => "../"
@@ -20,3 +20,6 @@ module MixedGauge
20
20
  end
21
21
  end
22
22
  end
23
+
24
+ require 'mixed_gauge/database_tasks'
25
+ require 'mixed_gauge/railtie' if defined? Rails
@@ -2,7 +2,7 @@ module MixedGauge
2
2
  class Config
3
3
  DEFAULT_HASH_FUNCTION = -> (key) { Digest::MD5.hexdigest(key).to_i(16) }
4
4
 
5
- attr_reader :hash_proc
5
+ attr_reader :hash_proc, :cluster_configs
6
6
 
7
7
  def initialize
8
8
  @cluster_configs = {}
@@ -0,0 +1,183 @@
1
+ module MixedGauge
2
+ module DatabaseTasks
3
+ extend self
4
+
5
+ # @return [Boolean]
6
+ def ar5?
7
+ ActiveRecord::VERSION::MAJOR == 5
8
+ end
9
+
10
+ # @return [Boolean]
11
+ def ar4?
12
+ ActiveRecord::VERSION::MAJOR == 4
13
+ end
14
+
15
+ # @return [Boolean]
16
+ def ar42?
17
+ ar4? && ActiveRecord::VERSION::MINOR == 2
18
+ end
19
+
20
+ # @return [Boolean]
21
+ def ar41?
22
+ ar4? && ActiveRecord::VERSION::MINOR == 1
23
+ end
24
+
25
+ # @return [Boolean]
26
+ def ar417_above?
27
+ ar41? && ActiveRecord::VERSION::TINY > 7
28
+ end
29
+
30
+ # Show information of database sharding config.
31
+ def info
32
+ puts "All clusters registered to mixed_gauge"
33
+ puts
34
+ clusters.each do |cluster|
35
+ puts "= Cluster: #{cluster.name} ="
36
+ cluster.connections.each do |name|
37
+ puts "- #{name}"
38
+ end
39
+ puts
40
+ end
41
+ end
42
+
43
+ # @private
44
+ # @param [String] task_name
45
+ # @return [Rake::Task]
46
+ def to_rake_task(task_name)
47
+ Rake::Task[task_name]
48
+ end
49
+
50
+ # @private
51
+ # @return [Array<Symbol>]
52
+ def cluster_names
53
+ MixedGauge.config.cluster_configs.keys
54
+ end
55
+
56
+ # @private
57
+ # @return [Array<MixedGauge::ClusterConfig>]
58
+ def clusters
59
+ MixedGauge.config.cluster_configs.values
60
+ end
61
+
62
+ # @private
63
+ # @return [MixedGauge::ClusterConfig]
64
+ # @raise [KeyError]
65
+ def fetch_cluster_config(cluster_name)
66
+ MixedGauge.config.fetch_cluster_config(cluster_name)
67
+ end
68
+
69
+ # For mock-ablity
70
+ # @private
71
+ def exit_with_error
72
+ exit 1
73
+ end
74
+
75
+ module TasksForMultipleClusters
76
+ # @param [String] task_name
77
+ def invoke_task_for_all_clusters(task_name)
78
+ cluster_names.each do |cluster_name|
79
+ invoke_task(task_name, cluster_name)
80
+ end
81
+ end
82
+
83
+ # @private
84
+ # @param [String] name
85
+ # @param [Symbol] cluster_name
86
+ def invoke_task(name, cluster_name)
87
+ task_name = "mixed_gauge:#{name}"
88
+ to_rake_task(task_name).invoke(cluster_name.to_s)
89
+ to_rake_task(task_name).reenable
90
+ end
91
+ end
92
+ extend TasksForMultipleClusters
93
+
94
+ # Organize cluster config and handle error for invalid args, call single
95
+ # cluster task with each single connection config.
96
+ module TaskOrganizerForSingleClusterTask
97
+ # @param [Hash{Symbol => String}] args
98
+ def create_all_databases(args)
99
+ exec_task_for_all_databases('create', args)
100
+ end
101
+
102
+ # @param [Hash{Symbol => String}] args
103
+ def drop_all_databases(args)
104
+ exec_task_for_all_databases('drop', args)
105
+ end
106
+
107
+ # @param [Hash{Symbol => String}] args
108
+ def load_schema_all_databases(args)
109
+ exec_task_for_all_databases('load_schema', args)
110
+ end
111
+
112
+ private
113
+
114
+ # @param [String] task_name
115
+ # @param [Hash{Symbol => String}] args
116
+ def exec_task_for_all_databases(task_name, args)
117
+ cluster_name = cluster_name_or_error(task_name, args)
118
+ cluster = cluster_or_error(cluster_name)
119
+ cluster.connections.each do |connection_name|
120
+ __send__(task_name, connection_name.to_s)
121
+ end
122
+ end
123
+
124
+ # @param [String] name A task name
125
+ # @param [Hash{Symbol => String}] args
126
+ # @return [String]
127
+ def cluster_name_or_error(name, args)
128
+ unless cluster_name = args[:cluster_name]
129
+ $stderr.puts <<-MSG
130
+ Missing cluster_name. Find cluster_name via `rake mixed_gauge:info` then call `rake "mixed_gauge:#{name}[$cluster_name]"`.
131
+ MSG
132
+ exit_with_error
133
+ end
134
+ cluster_name
135
+ end
136
+
137
+ # @param [String] cluster_name
138
+ # @return [MixedGauge::ClusterConfig]
139
+ def cluster_or_error(cluster_name)
140
+ fetch_cluster_config(cluster_name.to_sym)
141
+ rescue KeyError
142
+ $stderr.puts %!cluster name "#{cluster_name}" not found.!
143
+ exit_with_error
144
+ end
145
+ end
146
+ extend TaskOrganizerForSingleClusterTask
147
+
148
+ # Create, drop, load_schema for single connection config.
149
+ module TasksForSingleConnection
150
+ # @param [String] connection_name
151
+ def create(connection_name)
152
+ configuration = ActiveRecord::Base.configurations[connection_name]
153
+ ActiveRecord::Tasks::DatabaseTasks.create(configuration)
154
+ # Re-configure using configuration with database
155
+ ActiveRecord::Base.establish_connection(configuration)
156
+ end
157
+
158
+ # @param [String] connection_name
159
+ def drop(connection_name)
160
+ configuration = ActiveRecord::Base.configurations[connection_name]
161
+ ActiveRecord::Tasks::DatabaseTasks.drop(configuration)
162
+ end
163
+
164
+ # @param [String] connection_name
165
+ def load_schema(connection_name)
166
+ configuration = ActiveRecord::Base.configurations[connection_name]
167
+
168
+ case
169
+ when ar5?
170
+ ActiveRecord::Tasks::DatabaseTasks.load_schema(configuration, :ruby)
171
+ when ar42? || ar417_above?
172
+ ActiveRecord::Tasks::DatabaseTasks.load_schema_for(configuration, :ruby)
173
+ when ar41?
174
+ ActiveRecord::Base.establish_connection(configuration)
175
+ ActiveRecord::Tasks::DatabaseTasks.load_schema(:ruby)
176
+ else
177
+ raise "This version of ActiveRecord is not supported: v#{ActiveRecord::VERSION::STRING}"
178
+ end
179
+ end
180
+ end
181
+ extend TasksForSingleConnection
182
+ end
183
+ end
@@ -0,0 +1,8 @@
1
+ module MixedGauge
2
+ # Railtie of mixed_gauge
3
+ class Railtie < ::Rails::Railtie
4
+ rake_tasks do
5
+ load File.expand_path('../../tasks/mixed_gauge.rake', __FILE__)
6
+ end
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module MixedGauge
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
@@ -0,0 +1,40 @@
1
+ namespace :mixed_gauge do
2
+ desc 'Show all defined clusters and their detail'
3
+ task info: %i(environment) do
4
+ MixedGauge::DatabaseTasks.info
5
+ end
6
+
7
+ desc 'Setup all databases in all clusters'
8
+ task setup: %i(create_all load_schema_all) do
9
+ end
10
+
11
+ desc 'Create all databases in all clusters'
12
+ task :create_all => :environment do
13
+ MixedGauge::DatabaseTasks.invoke_task_for_all_clusters('create')
14
+ end
15
+
16
+ desc 'Drop all databases in all clusters'
17
+ task :drop_all => :environment do
18
+ MixedGauge::DatabaseTasks.invoke_task_for_all_clusters('drop')
19
+ end
20
+
21
+ desc 'Load schema to all databases in all clusters'
22
+ task :load_schema_all => :environment do
23
+ MixedGauge::DatabaseTasks.invoke_task_for_all_clusters('load_schema')
24
+ end
25
+
26
+ desc 'Create all databases in specific cluster'
27
+ task :create, %i(cluster_name) => %i(environment) do |_, args|
28
+ MixedGauge::DatabaseTasks.create_all_databases(args)
29
+ end
30
+
31
+ desc 'Drop all databases in specific cluster'
32
+ task :drop, %i(cluster_name) => %i(environment) do |_, args|
33
+ MixedGauge::DatabaseTasks.drop_all_databases(args)
34
+ end
35
+
36
+ desc 'Load schema to all databases in specific cluster'
37
+ task :load_schema, %i(cluster_name) => %i(environment) do |_, args|
38
+ MixedGauge::DatabaseTasks.load_schema_all_databases(args)
39
+ end
40
+ end
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ['Taiki Ono']
10
10
  spec.email = ['taiks.4559@gmail.com']
11
11
 
12
- spec.summary = %q{An ActiveRecord extension for database sharding.}
13
- spec.description = %q{Offers simple key-value action, node management with hash slots, allow to use some AR::B actions.}
12
+ spec.summary = %{An ActiveRecord extension for database sharding including re-sharding support.}
13
+ spec.description = %{#{spec.summary} Offers shards management with hash slots, re-sharding support, key-value action, some ActiveRecord::Base actions.}
14
14
  spec.homepage = 'https://github.com/taiki45/mixed_gauge'
15
15
  spec.license = 'MIT'
16
16
 
@@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ['lib']
21
21
 
22
- spec.add_dependency 'activesupport', '>= 4.0.0'
23
- spec.add_dependency 'activerecord', '>= 4.0.0'
22
+ spec.add_dependency 'activerecord', '>= 4.1.0'
23
+ spec.add_development_dependency 'appraisal'
24
24
  spec.add_development_dependency 'coveralls'
25
25
  spec.add_development_dependency 'pry'
26
26
  spec.add_development_dependency 'pry-doc'
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mixed_gauge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taiki Ono
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-27 00:00:00.000000000 Z
11
+ date: 2015-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activesupport
14
+ name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.0.0
19
+ version: 4.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 4.0.0
26
+ version: 4.1.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: activerecord
28
+ name: appraisal
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 4.0.0
34
- type: :runtime
33
+ version: '0'
34
+ type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 4.0.0
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: coveralls
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -150,8 +150,9 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
- description: Offers simple key-value action, node management with hash slots, allow
154
- to use some AR::B actions.
153
+ description: An ActiveRecord extension for database sharding including re-sharding
154
+ support. Offers shards management with hash slots, re-sharding support, key-value
155
+ action, some ActiveRecord::Base actions.
155
156
  email:
156
157
  - taiks.4559@gmail.com
157
158
  executables: []
@@ -162,6 +163,7 @@ files:
162
163
  - ".gitignore"
163
164
  - ".rspec"
164
165
  - ".travis.yml"
166
+ - Appraisals
165
167
  - CHANGELOG.md
166
168
  - Gemfile
167
169
  - LICENSE.txt
@@ -169,14 +171,22 @@ files:
169
171
  - Rakefile
170
172
  - bin/console
171
173
  - bin/setup
174
+ - gemfiles/ar_4.1.0.gemfile
175
+ - gemfiles/ar_4.1.7.gemfile
176
+ - gemfiles/ar_4.1.8.gemfile
177
+ - gemfiles/ar_4.2.gemfile
178
+ - gemfiles/rails_edge.gemfile
172
179
  - lib/mixed_gauge.rb
173
180
  - lib/mixed_gauge/cluster_config.rb
174
181
  - lib/mixed_gauge/config.rb
182
+ - lib/mixed_gauge/database_tasks.rb
175
183
  - lib/mixed_gauge/errors.rb
176
184
  - lib/mixed_gauge/model.rb
185
+ - lib/mixed_gauge/railtie.rb
177
186
  - lib/mixed_gauge/routing.rb
178
187
  - lib/mixed_gauge/sub_model_repository.rb
179
188
  - lib/mixed_gauge/version.rb
189
+ - lib/tasks/mixed_gauge.rake
180
190
  - mixed_gauge.gemspec
181
191
  homepage: https://github.com/taiki45/mixed_gauge
182
192
  licenses:
@@ -201,6 +211,6 @@ rubyforge_project:
201
211
  rubygems_version: 2.2.2
202
212
  signing_key:
203
213
  specification_version: 4
204
- summary: An ActiveRecord extension for database sharding.
214
+ summary: An ActiveRecord extension for database sharding including re-sharding support.
205
215
  test_files: []
206
216
  has_rdoc: