n_plus_one_control 0.1.3 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/README.md +14 -2
- data/lib/n_plus_one_control/executor.rb +12 -3
- data/lib/n_plus_one_control/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 942639e351ee999dc4a113c237d9604878d296cc
|
4
|
+
data.tar.gz: 76d9b2b2fd28b02925ce4d219e3b5c6ac316d1fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 980d3feab9ebd9d9b51da847526a83ae0ed2b36e50509494d14bfad75578086b4c2ca6a6da60d5f518a6a2deb87c80af848c792046804e96b62249404f09535c
|
7
|
+
data.tar.gz: ef2d79c707712f2a578ba4ff513c1f03b6c4cf351b72381875188ac6a3730c1f73b3fd11ab7a0538f5d76f2d67a5b6fb4f57775218e1ac8b1a0877e6936bd0a9
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -167,6 +167,19 @@ NPlusOneControl.ignore = /^(BEGIN|COMMIT|SAVEPOINT|RELEASE)/
|
|
167
167
|
# We track ActiveRecord event by default,
|
168
168
|
# but can also track rom-rb events ('sql.rom') as well.
|
169
169
|
NPlusOneControl.event = 'sql.active_record'
|
170
|
+
|
171
|
+
# configure transactional behavour for populate method
|
172
|
+
# in case of use multiple database connections
|
173
|
+
NPlusOneControl::Executor.tap do |executor|
|
174
|
+
connections = ActiveRecord::Base.connection_handler.connection_pool_list.map(&:connection)
|
175
|
+
|
176
|
+
executor.transaction_begin = -> do
|
177
|
+
connections.each { |connection| connection.begin_transaction(joinable: false) }
|
178
|
+
end
|
179
|
+
executor.transaction_rollback = -> do
|
180
|
+
connections.each(&:rollback_transaction)
|
181
|
+
end
|
182
|
+
end
|
170
183
|
```
|
171
184
|
|
172
185
|
## How does it work?
|
@@ -180,7 +193,7 @@ It may be useful to provide more matchers/assertions, for example:
|
|
180
193
|
```ruby
|
181
194
|
|
182
195
|
# Actually, that means that it is N+1))
|
183
|
-
assert_linear_number_of_queries { ... }
|
196
|
+
assert_linear_number_of_queries { ... }
|
184
197
|
|
185
198
|
# But we can tune it with `coef` and handle such cases as selecting in batches
|
186
199
|
assert_linear_number_of_queries(coef: 0.1) do
|
@@ -211,4 +224,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/palkan
|
|
211
224
|
## License
|
212
225
|
|
213
226
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
214
|
-
|
@@ -26,6 +26,9 @@ module NPlusOneControl
|
|
26
26
|
end
|
27
27
|
|
28
28
|
class << self
|
29
|
+
attr_accessor :transaction_begin
|
30
|
+
attr_accessor :transaction_rollback
|
31
|
+
|
29
32
|
def call(population:, scale_factors: nil, matching: nil)
|
30
33
|
raise ArgumentError, "Block is required!" unless block_given?
|
31
34
|
|
@@ -44,12 +47,18 @@ module NPlusOneControl
|
|
44
47
|
private
|
45
48
|
|
46
49
|
def with_transaction
|
47
|
-
|
48
|
-
ActiveRecord::Base.connection.begin_transaction(joinable: false)
|
50
|
+
transaction_begin.call
|
49
51
|
yield
|
50
52
|
ensure
|
51
|
-
|
53
|
+
transaction_rollback.call
|
52
54
|
end
|
53
55
|
end
|
56
|
+
|
57
|
+
self.transaction_begin = -> do
|
58
|
+
ActiveRecord::Base.connection.begin_transaction(joinable: false)
|
59
|
+
end
|
60
|
+
self.transaction_rollback = -> do
|
61
|
+
ActiveRecord::Base.connection.rollback_transaction
|
62
|
+
end
|
54
63
|
end
|
55
64
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: n_plus_one_control
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- palkan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|