database_slave 0.1.0 → 0.1.1
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 +14 -6
- data/CHANGELOG +4 -0
- data/README.md +19 -2
- data/TODOLIST +1 -0
- data/database_slave.gemspec +0 -1
- data/lib/database_slave/base.rb +1 -1
- data/lib/database_slave/relation.rb +85 -6
- data/lib/database_slave/version.rb +1 -1
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZjZhMzMxNjJjNmYzZmNmM2VkYTc0OGY1MmJjMjhkNzJhYzVlOTczZA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NzlkOGUyODcwOWVkOTQxNTYyN2VmYjcxMjY3ZTk1ZmE0NjcwN2YwZQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NWNkMzAzNjcwOTI5M2ViZjk0OGEwOWQyODlmMjcyNzg1YmMwZmRjZGZhMWFj
|
10
|
+
MjczMjc5ZWUxNjQzYzUzY2E2MjZjODI2YTQzODkxZmU0Yzg5YWIwYjdjZTc1
|
11
|
+
ZDUzYTMyYmE4MzFlZWQ4MjZiN2VmNmQ3Y2M4MGIzZjZkNDM1MTI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YTgzNWZiMjdmYjFjOTZkYTM0ZmM5YmJjNWI4MDk0NWFmYjE0YTQ5NjY1NDc3
|
14
|
+
M2YzMTQyZTYyZTA1ZjViZDhlMzUyMGQ0NWFkODExOTJkODMwMzllODk1Y2Fm
|
15
|
+
OTc3ZjhiYzNiZjEwNmI1NTQ0ZjA1OTc0MGQxN2I1Nzc5YzRmOWM=
|
data/CHANGELOG
ADDED
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
# Database_slave
|
2
|
+
[](https://github.com/Gnodiah/database_slave)
|
3
|
+

|
4
|
+
|
2
5
|
This gem provides master and slave databases support for Rails applications. It maintains a slave database configuration in config/shards.yml, and treats config/database.yml as master database. Then, you can choose which database you want to use in your ActiveRecord::Relation clauses.
|
3
6
|
|
4
7
|
For example, you can use slave database to execute complicated and time-consuming database queries to balance the performance of master database, or separate read and write database operations.
|
@@ -8,12 +11,26 @@ For example, you can use slave database to execute complicated and time-consumin
|
|
8
11
|
* Ruby >= 2.0.0
|
9
12
|
* Rails >= 3.2.x
|
10
13
|
|
14
|
+
# Installation
|
15
|
+
|
16
|
+
Put the following line into you Gemfile:
|
17
|
+
|
18
|
+
```bash
|
19
|
+
gem 'database_slave', '>= 0.1.0'
|
20
|
+
```
|
21
|
+
|
22
|
+
then execute:
|
23
|
+
|
24
|
+
```bash
|
25
|
+
bundle install
|
26
|
+
```
|
27
|
+
|
11
28
|
# Preparing
|
12
29
|
|
13
30
|
1. First of all, create a file named **shards.yml** in your Rails config directory,
|
14
31
|
its content is very similar to config/database.yml:
|
15
32
|
|
16
|
-
```
|
33
|
+
```yml
|
17
34
|
development:
|
18
35
|
slave_database1:
|
19
36
|
adapter: mysql2
|
@@ -40,7 +57,7 @@ For example, you can use slave database to execute complicated and time-consumin
|
|
40
57
|
|
41
58
|
2. Then add following to your settings.yml file:
|
42
59
|
|
43
|
-
```
|
60
|
+
```ruby
|
44
61
|
using_slave: true
|
45
62
|
```
|
46
63
|
|
data/TODOLIST
CHANGED
data/database_slave.gemspec
CHANGED
@@ -5,7 +5,6 @@ require 'database_slave/version'
|
|
5
5
|
Gem::Specification.new do |gem|
|
6
6
|
gem.name = 'database_slave'
|
7
7
|
gem.version = DatabaseSlave::VERSION
|
8
|
-
gem.date = '2015-03-17'
|
9
8
|
gem.summary = "Provide master and slave databases support for Rails applications."
|
10
9
|
gem.description = "Provide master and slave databases support for Rails applications."
|
11
10
|
gem.authors = ["Hayden Wei"]
|
data/lib/database_slave/base.rb
CHANGED
@@ -47,7 +47,7 @@ module DatabaseSlave
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
|
50
|
+
alias using using_slave
|
51
51
|
|
52
52
|
# === Description
|
53
53
|
#
|
@@ -55,18 +55,97 @@ module DatabaseSlave
|
|
55
55
|
#
|
56
56
|
# 这里我们重写ActiveRecord::Relation的to_a方法只是为了做一件事:
|
57
57
|
#
|
58
|
-
# 必须在当前relation返回后将是否使用从库的标识设置为否,
|
58
|
+
# 必须在当前relation返回后将是否使用从库的标识设置为否,
|
59
59
|
# 以免影响执行下一个relation时的主从库选择错误.
|
60
60
|
#
|
61
61
|
# 对应到代码即:
|
62
62
|
# DatabaseSlave::RuntimeRegistry.current_slave_name = nil
|
63
63
|
#
|
64
64
|
def to_a
|
65
|
-
|
65
|
+
# 该if语句的作用是: 确保在一条使用从库的查询中存在的其他先决条件的
|
66
|
+
# 查询也使用从库。例如:
|
67
|
+
#
|
68
|
+
# class Book < ActiveRecord::Base
|
69
|
+
# default_scope lambda { where(:tag_id => Tag.published.pluck(:id)) }
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
# 当我们使用如下查询
|
73
|
+
#
|
74
|
+
# Book.order('id DESC').limit(2).pluck(:id)
|
75
|
+
#
|
76
|
+
# 时, default_scope中的Tag需要被先查询出来. 为了Book和Tag的查询都使用从库,
|
77
|
+
# 避免查询Tag后便释放了从库连接而导致Book的查询使用的还是主库. 故在这里
|
78
|
+
# 加了条件判断: 如果父查询已经设置了使用从库, 那么内部的所有查询都使用从库,
|
79
|
+
# 直到父查询返回.
|
80
|
+
#
|
81
|
+
# Supports ActiveRecord::QueryMethods:
|
82
|
+
# select, group, order, reorder, joins, where, having,
|
83
|
+
# limit, offset, uniq
|
84
|
+
#
|
85
|
+
# And ActiveRecord::FinderMethods:
|
86
|
+
# first, first!, last, last!, find, all
|
87
|
+
#
|
88
|
+
# And ActiveRecord::Batches:
|
89
|
+
# find_each, find_in_batches
|
90
|
+
#
|
91
|
+
if !DatabaseSlave::RuntimeRegistry.current_slave_name
|
92
|
+
begin
|
93
|
+
DatabaseSlave::RuntimeRegistry.current_slave_name = slave_name if using_slave?
|
94
|
+
super
|
95
|
+
ensure
|
96
|
+
DatabaseSlave::RuntimeRegistry.current_slave_name = nil
|
97
|
+
end
|
98
|
+
else
|
99
|
+
super
|
100
|
+
end
|
101
|
+
end if defined?(Rails)
|
66
102
|
|
67
|
-
|
68
|
-
|
69
|
-
|
103
|
+
# Supports ActiveRecord::FinderMethods:
|
104
|
+
# exists?
|
105
|
+
#
|
106
|
+
def exists?(id = false)
|
107
|
+
if !DatabaseSlave::RuntimeRegistry.current_slave_name
|
108
|
+
begin
|
109
|
+
DatabaseSlave::RuntimeRegistry.current_slave_name = slave_name if using_slave?
|
110
|
+
super
|
111
|
+
ensure
|
112
|
+
DatabaseSlave::RuntimeRegistry.current_slave_name = nil
|
113
|
+
end
|
114
|
+
else
|
115
|
+
super
|
116
|
+
end
|
117
|
+
end if defined?(Rails)
|
118
|
+
|
119
|
+
# Supports ActiveRecord::Calculations:
|
120
|
+
# pluck
|
121
|
+
#
|
122
|
+
def pluck(column_name)
|
123
|
+
if !DatabaseSlave::RuntimeRegistry.current_slave_name
|
124
|
+
begin
|
125
|
+
DatabaseSlave::RuntimeRegistry.current_slave_name = slave_name if using_slave?
|
126
|
+
super
|
127
|
+
ensure
|
128
|
+
DatabaseSlave::RuntimeRegistry.current_slave_name = nil
|
129
|
+
end
|
130
|
+
else
|
131
|
+
super
|
132
|
+
end
|
133
|
+
end if defined?(Rails)
|
134
|
+
|
135
|
+
# Supports ActiveRecord::Calculations:
|
136
|
+
# count, average, minimun, maximum, sum, calculate
|
137
|
+
#
|
138
|
+
def calculate(operation, column_name, options = {})
|
139
|
+
if !DatabaseSlave::RuntimeRegistry.current_slave_name
|
140
|
+
begin
|
141
|
+
DatabaseSlave::RuntimeRegistry.current_slave_name = slave_name if using_slave?
|
142
|
+
super
|
143
|
+
ensure
|
144
|
+
DatabaseSlave::RuntimeRegistry.current_slave_name = nil
|
145
|
+
end
|
146
|
+
else
|
147
|
+
super
|
148
|
+
end
|
70
149
|
end if defined?(Rails)
|
71
150
|
end
|
72
151
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: database_slave
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hayden Wei
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Provide master and slave databases support for Rails applications.
|
14
14
|
email: haidongrun@gmail.com
|
@@ -16,7 +16,8 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
-
-
|
19
|
+
- .gitignore
|
20
|
+
- CHANGELOG
|
20
21
|
- LICENSE
|
21
22
|
- README.md
|
22
23
|
- TODOLIST
|
@@ -39,18 +40,19 @@ require_paths:
|
|
39
40
|
- lib
|
40
41
|
required_ruby_version: !ruby/object:Gem::Requirement
|
41
42
|
requirements:
|
42
|
-
- -
|
43
|
+
- - ! '>='
|
43
44
|
- !ruby/object:Gem::Version
|
44
45
|
version: 2.0.0
|
45
46
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
47
|
requirements:
|
47
|
-
- -
|
48
|
+
- - ! '>='
|
48
49
|
- !ruby/object:Gem::Version
|
49
50
|
version: '0'
|
50
51
|
requirements: []
|
51
52
|
rubyforge_project:
|
52
|
-
rubygems_version: 2.
|
53
|
+
rubygems_version: 2.0.5
|
53
54
|
signing_key:
|
54
55
|
specification_version: 4
|
55
56
|
summary: Provide master and slave databases support for Rails applications.
|
56
57
|
test_files: []
|
58
|
+
has_rdoc:
|