slavery 1.0.1 → 1.1.0
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.
- data/README.md +11 -7
- data/lib/slavery.rb +4 -1
- data/lib/slavery/version.rb +1 -1
- data/slavery.gemspec +2 -2
- metadata +4 -5
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# Slavery - Simple, conservative slave
|
1
|
+
# Slavery - Simple, conservative slave reads for ActiveRecord
|
2
2
|
|
3
|
-
Slavery is a simple, easy to use plugin for ActiveRecord that enables conservative slave reads, which means it doesn't automatically redirect all SELECTs to slaves. Instead, it lets you specify `Slavery.on_slave
|
3
|
+
Slavery is a simple, easy to use plugin for ActiveRecord that enables conservative slave reads, which means it doesn't automatically redirect all SELECTs to slaves. Instead, it lets you specify `Slavery.on_slave` to send a particular query to a slave.
|
4
4
|
|
5
5
|
Probably you just start off with one single database. As your app grows, you would move to master-slave replication for redundancy. At this point, all queries still go to the master and slaves are just backups. With that configuration, it's tempting to run some long-running queries on the slave. And that's exactly what Slavery does.
|
6
6
|
|
@@ -34,7 +34,7 @@ development_slave:
|
|
34
34
|
|
35
35
|
By convention, config keys with `[env]_slave` are automatically used for slave reads.
|
36
36
|
|
37
|
-
Notice that
|
37
|
+
Notice that we just copied the settings of `development` to `development_slave`. For `development` and `test`, it's actually recommended as probably you don't want to have replicating multiple databases on your machine. Two connections to the same identical database should be fine for testing purpose.
|
38
38
|
|
39
39
|
At this point, Slavery does nothing. Run tests and confirm that anything isn't broken.
|
40
40
|
|
@@ -69,21 +69,23 @@ development_slave:
|
|
69
69
|
database: myapp_development
|
70
70
|
```
|
71
71
|
|
72
|
-
With MySQL, `GRANT SELECT` creates
|
72
|
+
With MySQL, `GRANT SELECT` creates a read-only user.
|
73
73
|
|
74
74
|
```SQL
|
75
75
|
GRANT SELECT ON *.* TO 'readonly'@'localhost';
|
76
76
|
```
|
77
77
|
|
78
|
-
With this
|
78
|
+
With this user, writes on slave should raises an exception.
|
79
79
|
|
80
80
|
```ruby
|
81
81
|
Slavery.on_slave { User.create } # => ActiveRecord::StatementInvalid: Mysql2::Error: INSERT command denied...
|
82
82
|
```
|
83
83
|
|
84
|
+
It is a good idea to confirm this behavior in your test code.
|
85
|
+
|
84
86
|
## Database failure
|
85
87
|
|
86
|
-
When one of the master or the slave goes down, you would rewrite `database.yml` to make all queries go to the surviving database, until you
|
88
|
+
When one of the master or the slave goes down, you would rewrite `database.yml` to make all queries go to the surviving database, until you restore or rebuild the failed one.
|
87
89
|
|
88
90
|
In such an event, you don't want to manually remove `Slavery.on_slave` from your code. Instead, just put the following line in `config/initializers/slavery.rb`.
|
89
91
|
|
@@ -91,7 +93,7 @@ In such an event, you don't want to manually remove `Slavery.on_slave` from your
|
|
91
93
|
Slavely.disabled = true
|
92
94
|
```
|
93
95
|
|
94
|
-
With this line, Slavery stops connection switching and all queries go to the new master
|
96
|
+
With this line, Slavery stops connection switching and all queries go to the new master.
|
95
97
|
|
96
98
|
## Support for non-Rails apps
|
97
99
|
|
@@ -100,6 +102,8 @@ If you're using ActiveRecord in a non-Rails app (e.g. Sinatra), be sure to set `
|
|
100
102
|
```ruby
|
101
103
|
Slavery.env = 'development'
|
102
104
|
|
105
|
+
ActiveRecord::Base.send(:include, Slavery)
|
106
|
+
|
103
107
|
ActiveRecord::Base.configurations = {
|
104
108
|
'development' => { adapter: 'mysql2', ... },
|
105
109
|
'development_slave' => { adapter: 'mysql2', ... }
|
data/lib/slavery.rb
CHANGED
@@ -62,7 +62,10 @@ module Slavery
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def slaveryable?
|
65
|
-
|
65
|
+
base_transaction_depth = defined?(ActiveSupport::TestCase) &&
|
66
|
+
ActiveSupport::TestCase.respond_to?(:use_transactional_fixtures) &&
|
67
|
+
ActiveSupport::TestCase.try(:use_transactional_fixtures) ? 1 : 0
|
68
|
+
inside_transaction = master_connection.open_transactions > base_transaction_depth
|
66
69
|
raise Error.new('on_slave cannot be used inside transaction block!') if inside_transaction
|
67
70
|
|
68
71
|
!Slavery.disabled
|
data/lib/slavery/version.rb
CHANGED
data/slavery.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.version = Slavery::VERSION
|
9
9
|
gem.authors = ['Kenn Ejima']
|
10
10
|
gem.email = ['kenn.ejima@gmail.com']
|
11
|
-
gem.description = %q{Simple, conservative slave
|
12
|
-
gem.summary = %q{Simple, conservative slave
|
11
|
+
gem.description = %q{Simple, conservative slave reads for ActiveRecord}
|
12
|
+
gem.summary = %q{Simple, conservative slave reads for ActiveRecord}
|
13
13
|
gem.homepage = 'https://github.com/kenn/slavery'
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slavery
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -59,7 +59,7 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
-
description: Simple, conservative slave
|
62
|
+
description: Simple, conservative slave reads for ActiveRecord
|
63
63
|
email:
|
64
64
|
- kenn.ejima@gmail.com
|
65
65
|
executables: []
|
@@ -98,11 +98,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
98
|
version: '0'
|
99
99
|
requirements: []
|
100
100
|
rubyforge_project:
|
101
|
-
rubygems_version: 1.8.
|
101
|
+
rubygems_version: 1.8.24
|
102
102
|
signing_key:
|
103
103
|
specification_version: 3
|
104
|
-
summary: Simple, conservative slave
|
104
|
+
summary: Simple, conservative slave reads for ActiveRecord
|
105
105
|
test_files:
|
106
106
|
- spec/slavery_spec.rb
|
107
107
|
- spec/spec_helper.rb
|
108
|
-
has_rdoc:
|