distribute_reads 0.3.1 → 0.3.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 044c145f24198d69d3f221698f3452ee301e947cefe7f675eb8e7b704effc353
4
- data.tar.gz: 1f7c381f92ae63fcd8dc0ced50fa317591d319fbb1473af87dab01ac2e77a855
3
+ metadata.gz: 9ad9e2810c10045d79634ef7988435c059e99f8585667edf59d831797b258925
4
+ data.tar.gz: 7e3f7bbb5c96192910dc6a114147ddcf0c1258bcaed74491efdfb6ad20aaf20b
5
5
  SHA512:
6
- metadata.gz: 88019e465c3f305b16eca8bdf05a6b75fb9eec4b5d8b598167cadd904b86e403af9a460d36865286ece8224218f4e93a7249cb81a26a90a8d63c7a6f65fb5f03
7
- data.tar.gz: 1c336a5dbd584864aae1a422300e1468b456521a170754b6f2c138c6f4a10997401db9ae729eb799c3ffad64255b227e3d02cf54ea60f7e39e1ee759128f29bb
6
+ metadata.gz: 626a85f8a972d3e786ed1f705b2fef2e0da53c20d1a1d02d620d60de61014b493055f373f422050504c2760d86a4e71b0097f7a946c436dcc3ccb7fa80c5c00a
7
+ data.tar.gz: 76c312011e3c5bd4383ccf768b37a9d81800d022c7dfaeca29ad4384bf68f97c2789708609ea64b94a1a81d3e5c5b62836907d9d786150a3b1251069e9d7c14c
@@ -1,33 +1,38 @@
1
- ## 0.3.1
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
@@ -1,4 +1,4 @@
1
- Copyright (c) 2017-2019 Andrew Kane
1
+ Copyright (c) 2017-2020 Andrew Kane
2
2
 
3
3
  MIT License
4
4
 
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://edgeguides.rubyonrails.org/active_record_multiple_databases.html) :tada:
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 test, run:
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
  ```
@@ -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("[distribute_reads] #{message}")
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(" ↳ #{source}") if source
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
- DistributeReads.log "Call `to_a` inside block to execute query on replica" if value.is_a?(ActiveRecord::Relation) && !previous_value
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
@@ -1,3 +1,3 @@
1
1
  module DistributeReads
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
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.1
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: 2019-10-28 00:00:00.000000000 Z
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.0.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