switch_point 0.4.4 → 0.5.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -1
- data/Appraisals +4 -0
- data/CHANGELOG.md +5 -0
- data/README.md +69 -2
- data/gemfiles/rails_edge.gemfile +7 -0
- data/lib/switch_point.rb +4 -4
- data/lib/switch_point/model.rb +4 -2
- data/lib/switch_point/proxy.rb +3 -3
- data/lib/switch_point/version.rb +1 -1
- data/spec/models.rb +12 -0
- data/spec/switch_point/model_spec.rb +31 -2
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d937f9d15338cf8a36af264c6c5ff63c985edd11
|
4
|
+
data.tar.gz: 77fb0385c14264cb283ed32f41493dde3aa2ec7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3dfd6368a9967952b2eb83770ae3e700fe2f2299965364d3ebdca23f986f14303fb565a54b71c32b459df632067dcad8dd941dc1c6d3bc4cd6f97dc95fd3b1f8
|
7
|
+
data.tar.gz: 750d9ea20a474e5d453208dc95fef6df37fa3f6e4ce466a6d341a5ada8b6709a3797abbbef9e37340a1da03f8d9f0e640e55bb607d9cb06a37c6315850764551
|
data/.travis.yml
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
3
|
- 2.0.0
|
4
|
-
- 2.1
|
4
|
+
- 2.1
|
5
|
+
- 2.2
|
5
6
|
- ruby-head
|
6
7
|
gemfile:
|
7
8
|
- gemfiles/rails_3.2.gemfile
|
8
9
|
- gemfiles/rails_4.0.gemfile
|
9
10
|
- gemfiles/rails_4.1.gemfile
|
11
|
+
- gemfiles/rails_edge.gemfile
|
10
12
|
matrix:
|
11
13
|
allow_failures:
|
12
14
|
- rvm: ruby-head
|
15
|
+
- gemfile: gemfiles/rails_edge.gemfile
|
data/Appraisals
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
## 0.5.0 (XXXX-XX-XX)
|
2
|
+
- Rename `SwitchPoint.with_connection` to `SwitchPoint.with_mode`
|
3
|
+
- To avoid confusion with `ActiveRecord::ConnectionPool#with_connection`
|
4
|
+
- Inherit superclass' switch_point configuration
|
5
|
+
|
1
6
|
## 0.4.4 (2014-07-14)
|
2
7
|
- Memorize switch_point config to ConnectionSpecification#config instead of ConnectionPool
|
3
8
|
- To support multi-threaded environment since Rails 4.0.
|
data/README.md
CHANGED
@@ -21,8 +21,75 @@ Or install it yourself as:
|
|
21
21
|
$ gem install switch_point
|
22
22
|
|
23
23
|
## Usage
|
24
|
-
|
25
|
-
|
24
|
+
Suppose you have 4 databases: db-blog-master, db-blog-slave, db-comment-master and db-comment-slave.
|
25
|
+
Article model and Category model are stored in db-blog-{master,slave} and Comment model is stored in db-comment-{master,slave}.
|
26
|
+
|
27
|
+
### Configuration
|
28
|
+
In database.yml:
|
29
|
+
|
30
|
+
```yaml
|
31
|
+
production_blog_master:
|
32
|
+
adapter: mysql2
|
33
|
+
username: blog_writable
|
34
|
+
host: db-blog-master
|
35
|
+
production_blog_slave:
|
36
|
+
adapter: mysql2
|
37
|
+
username: blog_readonly
|
38
|
+
host: db-blog-slave
|
39
|
+
production_comment_master:
|
40
|
+
...
|
41
|
+
```
|
42
|
+
|
43
|
+
In initializer:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
SwitchPoint.configure do |config|
|
47
|
+
config.define_switch_point :blog,
|
48
|
+
readonly: :"#{Rails.env}_blog_slave",
|
49
|
+
writable: :"#{Rails.env}_blog_master"
|
50
|
+
config.define_switch_point :comment,
|
51
|
+
readonly: :"#{Rails.env}_comment_slave",
|
52
|
+
writable: :"#{Rails.env}_comment_master"
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
In models:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class Article < ActiveRecord::Base
|
60
|
+
use_switch_point :blog
|
61
|
+
end
|
62
|
+
|
63
|
+
class Category < ActiveRecord::Base
|
64
|
+
use_switch_point :blog
|
65
|
+
end
|
66
|
+
|
67
|
+
class Comment < ActiveRecord::Base
|
68
|
+
use_switch_point :comment
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
### Switching connections
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
Article.with_readonly { Article.first } # Read from db-blog-slave
|
76
|
+
Category.with_readonly { Category.first } # Also read from db-blog-slave
|
77
|
+
Comment.with_readonly { Comment.first } # Read from db-comment-slave
|
78
|
+
|
79
|
+
Article.with_readonly do
|
80
|
+
article = Article.first # Read from db-blog-slave
|
81
|
+
article.title = 'new title'
|
82
|
+
Article.with_writable do
|
83
|
+
article.save! # Write to db-blog-master
|
84
|
+
article.reload # Read from db-blog-master
|
85
|
+
Category.first # Read from db-blog-master
|
86
|
+
end
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
Note that Article and Category shares their connections.
|
91
|
+
|
92
|
+
## Notes
|
26
93
|
|
27
94
|
### auto_writable
|
28
95
|
`auto_writable` is disabled by default.
|
data/lib/switch_point.rb
CHANGED
@@ -33,17 +33,17 @@ module SwitchPoint
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def with_readonly(*names, &block)
|
36
|
-
|
36
|
+
with_mode(:readonly, *names, &block)
|
37
37
|
end
|
38
38
|
|
39
39
|
def with_writable(*names, &block)
|
40
|
-
|
40
|
+
with_mode(:writable, *names, &block)
|
41
41
|
end
|
42
42
|
|
43
|
-
def
|
43
|
+
def with_mode(mode, *names, &block)
|
44
44
|
names.reverse.inject(block) do |func, name|
|
45
45
|
lambda do
|
46
|
-
ProxyRepository.checkout(name).
|
46
|
+
ProxyRepository.checkout(name).with_mode(mode, &func)
|
47
47
|
end
|
48
48
|
end.call
|
49
49
|
end
|
data/lib/switch_point/model.rb
CHANGED
@@ -11,7 +11,7 @@ module SwitchPoint
|
|
11
11
|
|
12
12
|
module ClassMethods
|
13
13
|
def connection_with_switch_point
|
14
|
-
if
|
14
|
+
if switch_point_proxy
|
15
15
|
switch_point_proxy.connection
|
16
16
|
else
|
17
17
|
connection_without_switch_point
|
@@ -42,8 +42,10 @@ module SwitchPoint
|
|
42
42
|
def switch_point_proxy
|
43
43
|
if @switch_point_name
|
44
44
|
ProxyRepository.checkout(@switch_point_name)
|
45
|
-
|
45
|
+
elsif self == ActiveRecord::Base
|
46
46
|
nil
|
47
|
+
else
|
48
|
+
superclass.switch_point_proxy
|
47
49
|
end
|
48
50
|
end
|
49
51
|
|
data/lib/switch_point/proxy.rb
CHANGED
@@ -84,14 +84,14 @@ module SwitchPoint
|
|
84
84
|
end
|
85
85
|
|
86
86
|
def with_readonly(&block)
|
87
|
-
|
87
|
+
with_mode(:readonly, &block)
|
88
88
|
end
|
89
89
|
|
90
90
|
def with_writable(&block)
|
91
|
-
|
91
|
+
with_mode(:writable, &block)
|
92
92
|
end
|
93
93
|
|
94
|
-
def
|
94
|
+
def with_mode(new_mode, &block)
|
95
95
|
unless AVAILABLE_MODES.include?(new_mode)
|
96
96
|
raise ArgumentError.new("Unknown mode: #{new_mode}")
|
97
97
|
end
|
data/lib/switch_point/version.rb
CHANGED
data/spec/models.rb
CHANGED
@@ -73,6 +73,18 @@ class Nanika3 < ActiveRecord::Base
|
|
73
73
|
use_switch_point :nanika3
|
74
74
|
end
|
75
75
|
|
76
|
+
class AbstractNanika < ActiveRecord::Base
|
77
|
+
use_switch_point :main
|
78
|
+
self.abstract_class = true
|
79
|
+
end
|
80
|
+
|
81
|
+
class DerivedNanika1 < AbstractNanika
|
82
|
+
end
|
83
|
+
|
84
|
+
class DerivedNanika2 < AbstractNanika
|
85
|
+
use_switch_point :main2
|
86
|
+
end
|
87
|
+
|
76
88
|
base = { adapter: 'sqlite3' }
|
77
89
|
ActiveRecord::Base.configurations = {
|
78
90
|
'main_readonly' => base.merge(database: 'main_readonly.sqlite3'),
|
@@ -101,6 +101,35 @@ RSpec.describe SwitchPoint::Model do
|
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
104
|
+
context 'when superclass uses use_switch_point' do
|
105
|
+
context 'without use_switch_point in derived class' do
|
106
|
+
it 'inherits switch_point configuration' do
|
107
|
+
expect(DerivedNanika1).to connect_to('main_readonly.sqlite3')
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'shares connection with superclass' do
|
111
|
+
expect(DerivedNanika1.connection).to equal(AbstractNanika.connection)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'with use_switch_point in derived class' do
|
116
|
+
it 'overrides superclass' do
|
117
|
+
expect(DerivedNanika2).to connect_to('main2_readonly.sqlite3')
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'when superclass changes switch_point' do
|
122
|
+
after do
|
123
|
+
AbstractNanika.use_switch_point :main
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'follows' do
|
127
|
+
AbstractNanika.use_switch_point :main2
|
128
|
+
expect(DerivedNanika1).to connect_to('main2_readonly.sqlite3')
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
104
133
|
context 'without :writable' do
|
105
134
|
it 'sends destructive queries to ActiveRecord::Base' do
|
106
135
|
expect(Nanika1).to connect_to('main_readonly.sqlite3')
|
@@ -233,9 +262,9 @@ RSpec.describe SwitchPoint::Model do
|
|
233
262
|
end
|
234
263
|
end
|
235
264
|
|
236
|
-
describe '#
|
265
|
+
describe '#with_mode' do
|
237
266
|
it 'raises error if unknown mode is given' do
|
238
|
-
expect { SwitchPoint::ProxyRepository.checkout(:main).
|
267
|
+
expect { SwitchPoint::ProxyRepository.checkout(:main).with_mode(:typo) }.to raise_error
|
239
268
|
end
|
240
269
|
end
|
241
270
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: switch_point
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0.pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kohei Suzuki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: appraisal
|
@@ -128,6 +128,7 @@ files:
|
|
128
128
|
- gemfiles/rails_3.2.gemfile
|
129
129
|
- gemfiles/rails_4.0.gemfile
|
130
130
|
- gemfiles/rails_4.1.gemfile
|
131
|
+
- gemfiles/rails_edge.gemfile
|
131
132
|
- lib/switch_point.rb
|
132
133
|
- lib/switch_point/config.rb
|
133
134
|
- lib/switch_point/connection.rb
|
@@ -155,9 +156,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
155
156
|
version: '0'
|
156
157
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
157
158
|
requirements:
|
158
|
-
- - "
|
159
|
+
- - ">"
|
159
160
|
- !ruby/object:Gem::Version
|
160
|
-
version:
|
161
|
+
version: 1.3.1
|
161
162
|
requirements: []
|
162
163
|
rubyforge_project:
|
163
164
|
rubygems_version: 2.2.2
|