distribute_reads 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -10
- data/LICENSE.txt +1 -1
- data/README.md +11 -5
- data/lib/distribute_reads.rb +4 -4
- data/lib/distribute_reads/global_methods.rb +7 -1
- data/lib/distribute_reads/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ad9e2810c10045d79634ef7988435c059e99f8585667edf59d831797b258925
|
4
|
+
data.tar.gz: 7e3f7bbb5c96192910dc6a114147ddcf0c1258bcaed74491efdfb6ad20aaf20b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 626a85f8a972d3e786ed1f705b2fef2e0da53c20d1a1d02d620d60de61014b493055f373f422050504c2760d86a4e71b0097f7a946c436dcc3ccb7fa80c5c00a
|
7
|
+
data.tar.gz: 76c312011e3c5bd4383ccf768b37a9d81800d022c7dfaeca29ad4384bf68f97c2789708609ea64b94a1a81d3e5c5b62836907d9d786150a3b1251069e9d7c14c
|
data/CHANGELOG.md
CHANGED
@@ -1,33 +1,38 @@
|
|
1
|
-
## 0.3.
|
1
|
+
## 0.3.2 (2020-01-02)
|
2
|
+
|
3
|
+
- Added `eager_load` option
|
4
|
+
- Removed warning when relation is loaded
|
5
|
+
|
6
|
+
## 0.3.1 (2019-10-28)
|
2
7
|
|
3
8
|
- Added source location to logging
|
4
9
|
|
5
|
-
## 0.3.0
|
10
|
+
## 0.3.0 (2019-06-14)
|
6
11
|
|
7
12
|
- Use logger instead of stderr
|
8
13
|
- Handle `NULL` replication lag for MySQL
|
9
14
|
- Fixed replication lag check running on primary when replicas blacklisted
|
10
15
|
|
11
|
-
## 0.2.4
|
16
|
+
## 0.2.4 (2018-11-14)
|
12
17
|
|
13
18
|
- Added support for Aurora MySQL replication lag
|
14
19
|
- Added more logging
|
15
20
|
|
16
|
-
## 0.2.3
|
21
|
+
## 0.2.3 (2018-05-24)
|
17
22
|
|
18
23
|
- Added support for Makara 0.4
|
19
24
|
|
20
|
-
## 0.2.2
|
25
|
+
## 0.2.2 (2018-03-29)
|
21
26
|
|
22
27
|
- Added support for MySQL replication lag
|
23
28
|
- Added `replica` option
|
24
29
|
|
25
|
-
## 0.2.1
|
30
|
+
## 0.2.1 (2017-12-14)
|
26
31
|
|
27
32
|
- Fixed lag check for Postgres 10
|
28
33
|
- Added `replication_lag` method
|
29
34
|
|
30
|
-
## 0.2.0
|
35
|
+
## 0.2.0 (2017-10-02)
|
31
36
|
|
32
37
|
Breaking
|
33
38
|
|
@@ -44,16 +49,16 @@ Other
|
|
44
49
|
- Added default options
|
45
50
|
- Improved lag query
|
46
51
|
|
47
|
-
## 0.1.2
|
52
|
+
## 0.1.2 (2017-09-20)
|
48
53
|
|
49
54
|
- Raise `ArgumentError` when missing block
|
50
55
|
- Improved lag query
|
51
56
|
- Warn if returning `ActiveRecord::Relation`
|
52
57
|
|
53
|
-
## 0.1.1
|
58
|
+
## 0.1.1 (2017-05-14)
|
54
59
|
|
55
60
|
- Added method for jobs
|
56
61
|
|
57
|
-
## 0.1.0
|
62
|
+
## 0.1.0 (2017-03-26)
|
58
63
|
|
59
64
|
- First release
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -80,12 +80,18 @@ ActiveRecord uses [lazy evaluation](https://www.theodinproject.com/courses/ruby-
|
|
80
80
|
users = distribute_reads { User.where(orders_count: 1) } # not executed yet
|
81
81
|
```
|
82
82
|
|
83
|
-
Call `to_a` inside the block ensure the query runs on a replica.
|
83
|
+
Call `to_a` or `load` inside the block to ensure the query runs on a replica.
|
84
84
|
|
85
85
|
```ruby
|
86
86
|
users = distribute_reads { User.where(orders_count: 1).to_a }
|
87
87
|
```
|
88
88
|
|
89
|
+
You can automatically load relations returned from `distribute_reads` blocks by creating an initializer with:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
DistributeReads.eager_load = true
|
93
|
+
```
|
94
|
+
|
89
95
|
## Options
|
90
96
|
|
91
97
|
### Replica Lag
|
@@ -181,7 +187,7 @@ end
|
|
181
187
|
|
182
188
|
## Rails 6
|
183
189
|
|
184
|
-
Rails 6 has [native support for replicas](https://
|
190
|
+
Rails 6 has [native support for replicas](https://guides.rubyonrails.org/active_record_multiple_databases.html) :tada:
|
185
191
|
|
186
192
|
```ruby
|
187
193
|
ActiveRecord::Base.connected_to(role: :reading) do
|
@@ -208,13 +214,13 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
|
|
208
214
|
- Write, clarify, or fix documentation
|
209
215
|
- Suggest or add new features
|
210
216
|
|
211
|
-
To
|
217
|
+
To get started with development and testing:
|
212
218
|
|
213
219
|
```sh
|
214
220
|
git clone https://github.com/ankane/distribute_reads.git
|
215
221
|
cd distribute_reads
|
216
222
|
createdb distribute_reads_test_primary
|
217
223
|
createdb distribute_reads_test_replica
|
218
|
-
bundle
|
219
|
-
bundle exec rake
|
224
|
+
bundle install
|
225
|
+
bundle exec rake test
|
220
226
|
```
|
data/lib/distribute_reads.rb
CHANGED
@@ -14,8 +14,7 @@ module DistributeReads
|
|
14
14
|
class NoReplicasAvailable < Error; end
|
15
15
|
|
16
16
|
class << self
|
17
|
-
attr_accessor :by_default
|
18
|
-
attr_accessor :default_options
|
17
|
+
attr_accessor :by_default, :default_options, :eager_load
|
19
18
|
attr_writer :logger
|
20
19
|
end
|
21
20
|
self.by_default = false
|
@@ -23,6 +22,7 @@ module DistributeReads
|
|
23
22
|
failover: true,
|
24
23
|
lag_failover: false
|
25
24
|
}
|
25
|
+
self.eager_load = false
|
26
26
|
|
27
27
|
def self.logger
|
28
28
|
unless defined?(@logger)
|
@@ -98,11 +98,11 @@ module DistributeReads
|
|
98
98
|
|
99
99
|
def self.log(message)
|
100
100
|
if logger
|
101
|
-
logger.info
|
101
|
+
logger.info { "[distribute_reads] #{message}" }
|
102
102
|
|
103
103
|
# show location like Active Record
|
104
104
|
source = backtrace_cleaner.clean(caller.lazy).first
|
105
|
-
logger.info
|
105
|
+
logger.info { " ↳ #{source}" } if source
|
106
106
|
end
|
107
107
|
end
|
108
108
|
|
@@ -54,7 +54,13 @@ module DistributeReads
|
|
54
54
|
end
|
55
55
|
|
56
56
|
value = yield
|
57
|
-
|
57
|
+
if value.is_a?(ActiveRecord::Relation) && !previous_value && !value.loaded?
|
58
|
+
if DistributeReads.eager_load
|
59
|
+
value = value.load
|
60
|
+
else
|
61
|
+
DistributeReads.log "Call `to_a` inside block to execute query on replica"
|
62
|
+
end
|
63
|
+
end
|
58
64
|
value
|
59
65
|
ensure
|
60
66
|
Thread.current[:distribute_reads] = previous_value
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: distribute_reads
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-01-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: makara
|
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
142
|
- !ruby/object:Gem::Version
|
143
143
|
version: '0'
|
144
144
|
requirements: []
|
145
|
-
rubygems_version: 3.
|
145
|
+
rubygems_version: 3.1.2
|
146
146
|
signing_key:
|
147
147
|
specification_version: 4
|
148
148
|
summary: Scale database reads with replicas in Rails
|