active_record_slave 1.3.0 → 1.4.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.
- checksums.yaml +5 -5
- data/README.md +8 -5
- data/lib/active_record_slave/active_record_slave.rb +23 -0
- data/lib/active_record_slave/version.rb +1 -1
- data/test/test.sqlite3 +0 -0
- data/test/test_helper.rb +0 -4
- data/test/test_slave.sqlite3 +0 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6235db3e4c19fc1b58a00eaacde74d778e7cb8a8ef3672fe2ca63ca20f10702c
|
4
|
+
data.tar.gz: cad224213432afad1c87358923595f992ce61d63c1d467ce7dbc84b6af5d37cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e26f4116785cc6ef8777d4850d44b2034e52448c20b13c6f1a920e47d41a6a9ceded5a47d8aaed5bb12fc9ee327013f0fa40214b720bc51e0fe88c666b153a2
|
7
|
+
data.tar.gz: cef0b388839d44b90e4c34c1c0a9fa9978e2155621042742ab58c6fb7b5ccd40b811d746e6b3509934accd0d5654006e1e96bf0637417d106baa70947b3825cf
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
#
|
2
|
-
[](https://rubygems.org/gems/active_record_slave) [](https://travis-ci.org/rocketjob/active_record_slave) [](https://rubygems.org/gems/active_record_slave)  [-Support-brightgreen.svg)](https://gitter.im/rocketjob/support)
|
1
|
+
# Active Record Slave
|
2
|
+
[](https://rubygems.org/gems/active_record_slave) [](https://travis-ci.org/rocketjob/active_record_slave) [](https://rubygems.org/gems/active_record_slave) [](http://opensource.org/licenses/Apache-2.0)  [-Support-brightgreen.svg)](https://gitter.im/rocketjob/support)
|
3
3
|
|
4
4
|
Redirect ActiveRecord (Rails) reads to slave databases while ensuring all writes go to the master database.
|
5
5
|
|
@@ -7,8 +7,9 @@ Redirect ActiveRecord (Rails) reads to slave databases while ensuring all writes
|
|
7
7
|
|
8
8
|
## Introduction
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
active_record_slave redirects all database reads to slave instances while ensuring
|
11
|
+
that all writes go to the master database. active_record_slave ensures that
|
12
|
+
any reads that are performed within a database transaction are by default directed to the master
|
12
13
|
database to ensure data consistency.
|
13
14
|
|
14
15
|
## Status
|
@@ -17,7 +18,7 @@ Production Ready. Actively used in large production environments.
|
|
17
18
|
|
18
19
|
## Features
|
19
20
|
|
20
|
-
*
|
21
|
+
* Redirecting reads to a single slave database.
|
21
22
|
* Works with any database driver that works with ActiveRecord.
|
22
23
|
* Supports all Rails 3, 4, or 5 read apis.
|
23
24
|
* Including dynamic finders, AREL, and ActiveRecord::Base.select.
|
@@ -254,6 +255,8 @@ production:
|
|
254
255
|
|
255
256
|
## Dependencies
|
256
257
|
|
258
|
+
* Tested on Rails 3 and Rails 4
|
259
|
+
|
257
260
|
See [.travis.yml](https://github.com/reidmorrison/active_record_slave/blob/master/.travis.yml) for the list of tested Ruby platforms
|
258
261
|
|
259
262
|
## Possible Future Enhancements
|
@@ -45,11 +45,34 @@ module ActiveRecordSlave
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
+
#
|
49
|
+
# The default behavior can also set to read/write operations against master
|
50
|
+
# Create an initializer file config/initializer/active_record_slave.rb
|
51
|
+
# and set ActiveRecordSlave.read_from_master! to force read from master.
|
52
|
+
# Then use this method and supply block to read from the slave database
|
53
|
+
# Only applies to calls made within the current thread
|
54
|
+
def self.read_from_slave
|
55
|
+
return yield if read_from_slave?
|
56
|
+
begin
|
57
|
+
# Set nil indicator in thread local storage so that it is visible
|
58
|
+
# during the select call
|
59
|
+
read_from_slave!
|
60
|
+
yield
|
61
|
+
ensure
|
62
|
+
read_from_master!
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
48
66
|
# Whether this thread is currently forcing all reads to go against the master database
|
49
67
|
def self.read_from_master?
|
50
68
|
thread_variable_get(:active_record_slave) == :master
|
51
69
|
end
|
52
70
|
|
71
|
+
# Whether this thread is currently forcing all reads to go against the slave database
|
72
|
+
def self.read_from_slave?
|
73
|
+
thread_variable_get(:active_record_slave) == nil
|
74
|
+
end
|
75
|
+
|
53
76
|
# Force all subsequent reads on this thread and any fibers called by this thread to go the master
|
54
77
|
def self.read_from_master!
|
55
78
|
thread_variable_set(:active_record_slave, :master)
|
data/test/test.sqlite3
CHANGED
Binary file
|
data/test/test_helper.rb
CHANGED
@@ -2,9 +2,5 @@ ENV['RAILS_ENV'] = 'test'
|
|
2
2
|
|
3
3
|
require 'active_record'
|
4
4
|
require 'minitest/autorun'
|
5
|
-
require 'minitest/reporters'
|
6
|
-
require 'minitest/stub_any_instance'
|
7
5
|
require 'active_record_slave'
|
8
6
|
require 'awesome_print'
|
9
|
-
|
10
|
-
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
|
data/test/test_slave.sqlite3
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_slave
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Reid Morrison
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -65,14 +65,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
65
|
version: '0'
|
66
66
|
requirements: []
|
67
67
|
rubyforge_project:
|
68
|
-
rubygems_version: 2.
|
68
|
+
rubygems_version: 2.7.3
|
69
69
|
signing_key:
|
70
70
|
specification_version: 4
|
71
71
|
summary: Redirect ActiveRecord (Rails) reads to slave databases while ensuring all
|
72
72
|
writes go to the master database.
|
73
73
|
test_files:
|
74
|
+
- test/test_slave.sqlite3
|
74
75
|
- test/active_record_slave_test.rb
|
75
|
-
- test/database.yml
|
76
76
|
- test/test.sqlite3
|
77
|
+
- test/database.yml
|
77
78
|
- test/test_helper.rb
|
78
|
-
- test/test_slave.sqlite3
|