activerecord-slave 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +3 -0
- data/.rubocop.yml +56 -0
- data/.ruby-version +1 -0
- data/.travis.yml +31 -0
- data/Gemfile +4 -0
- data/Guardfile +46 -0
- data/LICENSE.txt +22 -0
- data/README.md +141 -0
- data/Rakefile +6 -0
- data/activerecord-slave.gemspec +40 -0
- data/lib/active_record/slave/config.rb +20 -0
- data/lib/active_record/slave/database_tasks.rb +30 -0
- data/lib/active_record/slave/model.rb +44 -0
- data/lib/active_record/slave/railtie.rb +9 -0
- data/lib/active_record/slave/replication_config.rb +28 -0
- data/lib/active_record/slave/replication_router.rb +21 -0
- data/lib/active_record/slave/version.rb +5 -0
- data/lib/activerecord-slave.rb +26 -0
- data/lib/tasks/activerecord-slave.rake +13 -0
- data/spec/active_record/slave/config_spec.rb +17 -0
- data/spec/active_record/slave/database_tasks_spec.rb +21 -0
- data/spec/active_record/slave/model_spec.rb +98 -0
- data/spec/active_record/slave/replication_config_spec.rb +46 -0
- data/spec/active_record/slave/replication_router_spec.rb +41 -0
- data/spec/active_record/slave/version_spec.rb +5 -0
- data/spec/models.rb +49 -0
- data/spec/schema.rb +26 -0
- data/spec/spec_helper.rb +71 -0
- data/spec/tasks/activerecord-slave_spec.rb +48 -0
- metadata +294 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3bf6fde1390af45c8e95d55e2405f19d092127c8
|
4
|
+
data.tar.gz: f5ab29a82e819cba72cdb3f6803c1c4be457d454
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4501a56c331b50a00083accf835dd77ece806d469a8de242f3fa52b6d1ee4ff8bcfa6e43060d4c007b5856bf3044cac0a2128e7952106b6da9adf8389ecfa732
|
7
|
+
data.tar.gz: b0433107d0c77d13215581c9d2541307b01cf06edac34682a6588c4aa38c48986392c9166b1b2ac7fb0d7f6eae09e8aa666259483d8761d9024969df3424e6d1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- "vendor/**/*"
|
4
|
+
DisplayCopNames: true
|
5
|
+
|
6
|
+
Style/AsciiComments:
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Style/BracesAroundHashParameters:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Style/Documentation:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Style/ExtraSpacing:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Style/GuardClause:
|
19
|
+
MinBodyLength: 5
|
20
|
+
|
21
|
+
Style/MethodDefParentheses:
|
22
|
+
EnforcedStyle: require_parentheses
|
23
|
+
|
24
|
+
Style/ModuleFunction:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Style/StringLiterals:
|
28
|
+
EnforcedStyle: double_quotes
|
29
|
+
|
30
|
+
Style/HashSyntax:
|
31
|
+
EnforcedStyle: ruby19_no_mixed_keys
|
32
|
+
Exclude:
|
33
|
+
- "**/*.rake"
|
34
|
+
- "Rakefile"
|
35
|
+
|
36
|
+
Metrics/LineLength:
|
37
|
+
Max: 160
|
38
|
+
Exclude:
|
39
|
+
- "db/migrate/*.rb"
|
40
|
+
|
41
|
+
NumericLiterals:
|
42
|
+
MinDigits: 7
|
43
|
+
|
44
|
+
Lint/AssignmentInCondition:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
Lint/BlockAlignment:
|
48
|
+
Enabled: false
|
49
|
+
|
50
|
+
Metrics/MethodLength:
|
51
|
+
Max: 12
|
52
|
+
|
53
|
+
FileName:
|
54
|
+
Exclude:
|
55
|
+
- 'lib/activerecord-slave.rb'
|
56
|
+
- 'spec/tasks/activerecord-slave_spec.rb'
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.3
|
data/.travis.yml
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
language: ruby
|
2
|
+
install: bundle install --jobs=3 --retry=3
|
3
|
+
cache:
|
4
|
+
directories:
|
5
|
+
- vendor/bundle
|
6
|
+
rvm:
|
7
|
+
- 2.2.3
|
8
|
+
- 2.1.7
|
9
|
+
- 2.0.0-p647
|
10
|
+
services:
|
11
|
+
- mysql
|
12
|
+
addons:
|
13
|
+
apt:
|
14
|
+
packages:
|
15
|
+
- libaio-dev
|
16
|
+
- libaio1
|
17
|
+
env:
|
18
|
+
- DEBIAN_FRONTEND=noninteractive PERL_MM_USE_DEFAULT=1 PERL5LIB=$HOME/perl5/lib/perl5 PATH=$HOME/bin:$HOME/perl5/bin/:$PATH
|
19
|
+
before_install:
|
20
|
+
- mkdir ~/bin
|
21
|
+
- cd ~/bin
|
22
|
+
- curl -O https://github.com/miyagawa/cpanminus/raw/master/cpanm
|
23
|
+
- curl -L https://cpanmin.us/ -o cpanm
|
24
|
+
- chmod +x cpanm
|
25
|
+
- cd -
|
26
|
+
- cpanm MySQL::Sandbox
|
27
|
+
- wget http://ftp.kaist.ac.kr/mysql/Downloads/MySQL-5.6/mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz
|
28
|
+
- make_replication_sandbox mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz
|
29
|
+
- gem install bundler -v 1.10.6
|
30
|
+
script:
|
31
|
+
- bundle exec rake spec
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features) \
|
6
|
+
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
7
|
+
|
8
|
+
## Note: if you are using the `directories` clause above and you are not
|
9
|
+
## watching the project directory ('.'), then you will want to move
|
10
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
11
|
+
#
|
12
|
+
# $ mkdir config
|
13
|
+
# $ mv Guardfile config/
|
14
|
+
# $ ln -s config/Guardfile .
|
15
|
+
#
|
16
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
17
|
+
|
18
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
19
|
+
# rspec may be run, below are examples of the most common uses.
|
20
|
+
# * bundler: 'bundle exec rspec'
|
21
|
+
# * bundler binstubs: 'bin/rspec'
|
22
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
23
|
+
# installed the spring binstubs per the docs)
|
24
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
25
|
+
# * 'just' rspec: 'rspec'
|
26
|
+
|
27
|
+
guard :rspec, cmd: "bundle exec rspec", all_on_start: true, all_after_pass: true do
|
28
|
+
require "guard/rspec/dsl"
|
29
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
30
|
+
|
31
|
+
# Feel free to open issues for suggestions and improvements
|
32
|
+
|
33
|
+
# RSpec files
|
34
|
+
rspec = dsl.rspec
|
35
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
36
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
37
|
+
watch(rspec.spec_files)
|
38
|
+
|
39
|
+
# Ruby files
|
40
|
+
ruby = dsl.ruby
|
41
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
42
|
+
|
43
|
+
watch(%r{^spec/.+_spec\.rb$})
|
44
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
45
|
+
watch("spec/spec_helper.rb") { "spec" }
|
46
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 hirocaster
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
# Activerecord::Slave
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/hirocaster/activerecord-slave.svg?branch=master)](https://travis-ci.org/hirocaster/activerecord-slave) [![Coverage Status](https://coveralls.io/repos/hirocaster/activerecord-slave/badge.svg?branch=master&service=github)](https://coveralls.io/github/hirocaster/activerecord-slave?branch=master) [![Code Climate](https://codeclimate.com/github/hirocaster/activerecord-slave/badges/gpa.svg)](https://codeclimate.com/github/hirocaster/activerecord-slave) [![Dependency Status](https://gemnasium.com/hirocaster/activerecord-slave.svg)](https://gemnasium.com/hirocaster/activerecord-slave)
|
4
|
+
|
5
|
+
ActiveRecord for MySQL Replication databases(master/slave).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'activerecord-slave'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install activerecord-slave
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Add database connections to your application's config/database.yml:
|
24
|
+
|
25
|
+
```yaml
|
26
|
+
default: &default
|
27
|
+
adapter: mysql2
|
28
|
+
encoding: utf8
|
29
|
+
pool: 5
|
30
|
+
database: user
|
31
|
+
username: root
|
32
|
+
password:
|
33
|
+
host: localhost
|
34
|
+
|
35
|
+
user_master:
|
36
|
+
<<: *default
|
37
|
+
host: master.db.example.com
|
38
|
+
|
39
|
+
user_slave_001:
|
40
|
+
<<: *default
|
41
|
+
host: slave_001.db.example.com
|
42
|
+
|
43
|
+
user_slave_002:
|
44
|
+
<<: *default
|
45
|
+
host: slave_002.db.example.com
|
46
|
+
```
|
47
|
+
|
48
|
+
Add this example, your application's config/initializers/active_record_slave.rb:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
ActiveRecord::Slave.configure do |config|
|
52
|
+
config.define_replication(:user) do |replication| # replication name
|
53
|
+
replication.register_master(:user_master) # master connection
|
54
|
+
|
55
|
+
replication.register_slave(:user_slave_001, 70) # slave connection, weight
|
56
|
+
replication.register_slave(:user_slave_002, 30)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
### Model
|
62
|
+
|
63
|
+
app/model/user.rb
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class User < ActiveRecord::Base
|
67
|
+
has_many :items
|
68
|
+
include ActiveRecord::Slave::Model
|
69
|
+
use_slave :user # replicaition name
|
70
|
+
end
|
71
|
+
|
72
|
+
class Item < ActiveRecord::Base
|
73
|
+
belongs_to :user
|
74
|
+
include ActiveRecord::Slave::Model
|
75
|
+
use_slave :user # replicaition name
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
Query for master database.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
User.all
|
83
|
+
User.find(1)
|
84
|
+
User.where(name: "foobar")
|
85
|
+
```
|
86
|
+
|
87
|
+
Query for slave databases.
|
88
|
+
|
89
|
+
distrebute(load-balance) connection by configured weight settings.
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
User.slave_for.all
|
93
|
+
User.slave_for.find(1)
|
94
|
+
User.slave_for.where(name: "foobar")
|
95
|
+
```
|
96
|
+
|
97
|
+
Association case. If select from slave, should use #slave_for. Not use assosiaion daynamic methods.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
User.find(1).items # items from master database
|
101
|
+
|
102
|
+
user = User.find(1)
|
103
|
+
Item.slave_for(user_id: user.id) # items from slave databases
|
104
|
+
```
|
105
|
+
|
106
|
+
### Tasks
|
107
|
+
|
108
|
+
Database create/drop tasks.
|
109
|
+
|
110
|
+
```
|
111
|
+
rake active_record:slave:db_create[replicaition] # Create database for replicaition master
|
112
|
+
rake active_record:slave:db_drop[replicaition] # Drop database for replicaition master
|
113
|
+
```
|
114
|
+
|
115
|
+
### Migration
|
116
|
+
|
117
|
+
You shuld write `#connection` to migration file for replication databases.
|
118
|
+
|
119
|
+
```
|
120
|
+
class CreateUsers < ActiveRecord::Migration
|
121
|
+
def connection
|
122
|
+
User.connection
|
123
|
+
end
|
124
|
+
|
125
|
+
def change
|
126
|
+
create_table :users do |t|
|
127
|
+
t.string :name
|
128
|
+
|
129
|
+
t.timestamps null: false
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
## Contributing
|
136
|
+
|
137
|
+
1. Fork it ( http://github.com/hirocaster/activerecord-slave/fork )
|
138
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
139
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
140
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
141
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "active_record/slave/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activerecord-slave"
|
8
|
+
spec.version = ActiveRecord::Slave::VERSION
|
9
|
+
spec.authors = ["hirocaster"]
|
10
|
+
spec.email = ["hohtsuka@gmail.com"]
|
11
|
+
spec.summary = "MySQL Replicaition(master/slave) for ActiveRecord"
|
12
|
+
spec.description = "MySQL Replicaition(master/slave) for ActiveRecord"
|
13
|
+
spec.homepage = "https://github.com/hirocaster/activerecord-slave"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = ">= 2.0"
|
22
|
+
|
23
|
+
spec.add_dependency "activerecord", ">= 4.1.0"
|
24
|
+
spec.add_dependency "pickup"
|
25
|
+
|
26
|
+
spec.add_development_dependency "awesome_print"
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
28
|
+
spec.add_development_dependency "codeclimate-test-reporter"
|
29
|
+
|
30
|
+
spec.add_development_dependency "coveralls"
|
31
|
+
spec.add_development_dependency "database_rewinder"
|
32
|
+
spec.add_development_dependency "guard-rspec"
|
33
|
+
spec.add_development_dependency "mysql2", "~> 0.3.18"
|
34
|
+
spec.add_development_dependency "pry"
|
35
|
+
spec.add_development_dependency "pry-byebug"
|
36
|
+
spec.add_development_dependency "rake"
|
37
|
+
spec.add_development_dependency "rspec"
|
38
|
+
spec.add_development_dependency "rspec-retry"
|
39
|
+
spec.add_development_dependency "simplecov"
|
40
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Slave
|
3
|
+
class Config
|
4
|
+
def initialize
|
5
|
+
@replication_configs = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def define_replication(replication_name, &block)
|
9
|
+
replication_config = ReplicationConfig.new(replication_name)
|
10
|
+
replication_config.instance_eval(&block)
|
11
|
+
replication_config.validate_config!
|
12
|
+
@replication_configs[replication_name] = replication_config
|
13
|
+
end
|
14
|
+
|
15
|
+
def fetch_replication_config(replication_name)
|
16
|
+
@replication_configs.fetch replication_name
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Slave
|
3
|
+
module DatabaseTasks
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def create_database(args)
|
7
|
+
configuration = database_configuration args
|
8
|
+
ActiveRecord::Tasks::DatabaseTasks.create configuration
|
9
|
+
end
|
10
|
+
|
11
|
+
def drop_database(args)
|
12
|
+
configuration = database_configuration args
|
13
|
+
ActiveRecord::Tasks::DatabaseTasks.drop configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
def database_configuration(args)
|
17
|
+
replication_name = args[:replicaition]
|
18
|
+
replication_config = fetch_replication_config replication_name.to_sym
|
19
|
+
connection_name = replication_config.master_connection_name
|
20
|
+
ActiveRecord::Base.configurations[connection_name.to_s]
|
21
|
+
end
|
22
|
+
|
23
|
+
def fetch_replication_config(replication_name)
|
24
|
+
ActiveRecord::Slave.config.fetch_replication_config replication_name
|
25
|
+
rescue KeyError
|
26
|
+
raise "Not exist #{replication_name} replicaition."
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|