bullet 8.1.1 → 8.1.3
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 +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +2 -0
- data/lib/bullet/active_record4.rb +2 -4
- data/lib/bullet/active_record41.rb +4 -4
- data/lib/bullet/active_record42.rb +9 -8
- data/lib/bullet/active_record5.rb +9 -8
- data/lib/bullet/active_record52.rb +19 -16
- data/lib/bullet/active_record60.rb +23 -16
- data/lib/bullet/active_record61.rb +23 -16
- data/lib/bullet/detector/n_plus_one_query.rb +20 -2
- data/lib/bullet/detector/unused_eager_loading.rb +1 -1
- data/lib/bullet/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ebbf526d81da50f5e04ae03c0e33af01ab71071747711f62dd3790a6debc0551
|
|
4
|
+
data.tar.gz: 7a1551a54344f9c363402854bf8129bc58ec21927b1ee9a9ce2f0b6a5b8f3e16
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5b24c5aea3986b30399e0456268bdb00462540324b745aa4ccc451f77b0e4d78222fec5c4fb781b72114f5f497c12069ad5daf3b3678ae674ae7c58d688d824a
|
|
7
|
+
data.tar.gz: 449c1c448f20f778053ec7ea032956f7446c2a14c1ea11293f3bc13cf7cf0d794beecf46dfb8052d58c644f35679a5d671f7def8b6d3b6f8eafd0a4689f7b565
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
## Unreleased
|
|
2
|
+
|
|
3
|
+
## 8.1.3 (06/02/2026)
|
|
4
|
+
|
|
5
|
+
* Handle inversed polymorphic belongs_to false positives
|
|
6
|
+
|
|
7
|
+
## 8.1.2 (05/25/2026)
|
|
8
|
+
|
|
9
|
+
* Skip N+1 detection for optional polymorphic `belongs_to` whose `*_type` column is nil. ActiveRecord short-circuits the reader to nil without issuing SQL, so the access cannot represent an N+1 query and preloading would be a no-op.
|
|
10
|
+
* Fix `Set#<<` corruption in `UnusedEagerLoading#add_eager_loadings` split branch
|
|
11
|
+
|
|
1
12
|
## 8.1.1 (04/23/2026)
|
|
2
13
|
|
|
3
14
|
* Fix ActiveRecord 8.1 patch-level method signature compatibility; test against Rails 8.1.3.
|
data/README.md
CHANGED
|
@@ -259,6 +259,8 @@ config.after_initialize do
|
|
|
259
259
|
end
|
|
260
260
|
```
|
|
261
261
|
|
|
262
|
+
**Note:** Controller and integration tests work automatically because Bullet's Rack middleware wraps each HTTP request with `Bullet.start_request` and `Bullet.end_request`. However, model tests and other tests that don't go through the Rack middleware (like direct model method calls) require manual wrapping with the test helper code below.
|
|
263
|
+
|
|
262
264
|
Then wrap each test in the Bullet api.
|
|
263
265
|
|
|
264
266
|
With RSpec:
|
|
@@ -166,10 +166,8 @@ module Bullet
|
|
|
166
166
|
def reader(force_reload = false)
|
|
167
167
|
result = origin_reader(force_reload)
|
|
168
168
|
if Bullet.start?
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
|
|
172
|
-
end
|
|
169
|
+
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name, inversed: @inversed)
|
|
170
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) unless @inversed
|
|
173
171
|
end
|
|
174
172
|
result
|
|
175
173
|
end
|
|
@@ -134,7 +134,7 @@ module Bullet
|
|
|
134
134
|
# call one to many associations
|
|
135
135
|
alias_method :origin_load_target, :load_target
|
|
136
136
|
def load_target
|
|
137
|
-
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name) if Bullet.start?
|
|
137
|
+
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name, inversed: @inversed) if Bullet.start?
|
|
138
138
|
origin_load_target
|
|
139
139
|
end
|
|
140
140
|
|
|
@@ -159,9 +159,9 @@ module Bullet
|
|
|
159
159
|
def reader(force_reload = false)
|
|
160
160
|
result = origin_reader(force_reload)
|
|
161
161
|
if Bullet.start?
|
|
162
|
-
if @owner.class.name !~ /^HABTM_/
|
|
163
|
-
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
|
|
164
|
-
Bullet::Detector::NPlusOneQuery.add_possible_objects(result)
|
|
162
|
+
if @owner.class.name !~ /^HABTM_/
|
|
163
|
+
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name, inversed: @inversed)
|
|
164
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) unless @inversed
|
|
165
165
|
end
|
|
166
166
|
end
|
|
167
167
|
result
|
|
@@ -179,7 +179,7 @@ module Bullet
|
|
|
179
179
|
records = origin_load_target
|
|
180
180
|
|
|
181
181
|
if Bullet.start?
|
|
182
|
-
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name
|
|
182
|
+
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name, inversed: @inversed)
|
|
183
183
|
if records.first.class.name !~ /^HABTM_/
|
|
184
184
|
if records.size > 1
|
|
185
185
|
Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
|
|
@@ -215,13 +215,14 @@ module Bullet
|
|
|
215
215
|
result = origin_reader(force_reload)
|
|
216
216
|
|
|
217
217
|
if Bullet.start?
|
|
218
|
-
if @owner.class.name !~ /^HABTM_/
|
|
219
|
-
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name)
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
218
|
+
if @owner.class.name !~ /^HABTM_/
|
|
219
|
+
Bullet::Detector::NPlusOneQuery.call_association(@owner, @reflection.name, inversed: @inversed)
|
|
220
|
+
unless @inversed
|
|
221
|
+
if Bullet::Detector::NPlusOneQuery.impossible?(@owner)
|
|
222
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
|
|
223
|
+
else
|
|
224
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
|
|
225
|
+
end
|
|
225
226
|
end
|
|
226
227
|
end
|
|
227
228
|
end
|
|
@@ -202,7 +202,7 @@ module Bullet
|
|
|
202
202
|
end
|
|
203
203
|
end
|
|
204
204
|
end
|
|
205
|
-
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name
|
|
205
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, inversed: @inversed)
|
|
206
206
|
if records.first.class.name !~ /^HABTM_/
|
|
207
207
|
if records.size > 1
|
|
208
208
|
Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
|
|
@@ -237,13 +237,14 @@ module Bullet
|
|
|
237
237
|
result = super()
|
|
238
238
|
|
|
239
239
|
if Bullet.start?
|
|
240
|
-
if owner.class.name !~ /^HABTM_/
|
|
241
|
-
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
240
|
+
if owner.class.name !~ /^HABTM_/
|
|
241
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, inversed: @inversed)
|
|
242
|
+
unless @inversed
|
|
243
|
+
if Bullet::Detector::NPlusOneQuery.impossible?(owner)
|
|
244
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
|
|
245
|
+
else
|
|
246
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
|
|
247
|
+
end
|
|
247
248
|
end
|
|
248
249
|
end
|
|
249
250
|
end
|
|
@@ -173,7 +173,7 @@ module Bullet
|
|
|
173
173
|
end
|
|
174
174
|
end
|
|
175
175
|
end
|
|
176
|
-
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name
|
|
176
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, inversed: @inversed)
|
|
177
177
|
if records.first.class.name !~ /^HABTM_/
|
|
178
178
|
if records.size > 1
|
|
179
179
|
Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
|
|
@@ -208,24 +208,27 @@ module Bullet
|
|
|
208
208
|
result = super()
|
|
209
209
|
|
|
210
210
|
if Bullet.start?
|
|
211
|
-
if owner.class.name !~ /^HABTM_/
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
211
|
+
if owner.class.name !~ /^HABTM_/
|
|
212
|
+
unless @inversed
|
|
213
|
+
if is_a? ::ActiveRecord::Associations::ThroughAssociation
|
|
214
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
|
|
215
|
+
association = owner.association reflection.through_reflection.name
|
|
216
|
+
Array.wrap(association.target).each do |through_record|
|
|
217
|
+
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
|
|
218
|
+
end
|
|
218
219
|
|
|
219
|
-
|
|
220
|
-
|
|
220
|
+
if reflection.through_reflection != through_reflection
|
|
221
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
|
|
222
|
+
end
|
|
221
223
|
end
|
|
222
224
|
end
|
|
223
|
-
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
225
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, inversed: @inversed)
|
|
226
|
+
unless @inversed
|
|
227
|
+
if Bullet::Detector::NPlusOneQuery.impossible?(owner)
|
|
228
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
|
|
229
|
+
else
|
|
230
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
|
|
231
|
+
end
|
|
229
232
|
end
|
|
230
233
|
end
|
|
231
234
|
end
|
|
@@ -200,7 +200,11 @@ module Bullet
|
|
|
200
200
|
end
|
|
201
201
|
end
|
|
202
202
|
end
|
|
203
|
-
|
|
203
|
+
if @inversed
|
|
204
|
+
Bullet::Detector::Association.add_call_object_associations(owner, reflection.name)
|
|
205
|
+
else
|
|
206
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
|
207
|
+
end
|
|
204
208
|
if records.first.class.name !~ /^HABTM_/
|
|
205
209
|
if records.size > 1
|
|
206
210
|
Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
|
|
@@ -235,24 +239,27 @@ module Bullet
|
|
|
235
239
|
result = super()
|
|
236
240
|
|
|
237
241
|
if Bullet.start?
|
|
238
|
-
if owner.class.name !~ /^HABTM_/
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
242
|
+
if owner.class.name !~ /^HABTM_/
|
|
243
|
+
unless @inversed
|
|
244
|
+
if is_a? ::ActiveRecord::Associations::ThroughAssociation
|
|
245
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
|
|
246
|
+
association = owner.association(reflection.through_reflection.name)
|
|
247
|
+
Array.wrap(association.target).each do |through_record|
|
|
248
|
+
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
|
|
249
|
+
end
|
|
245
250
|
|
|
246
|
-
|
|
247
|
-
|
|
251
|
+
if reflection.through_reflection != through_reflection
|
|
252
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
|
|
253
|
+
end
|
|
248
254
|
end
|
|
249
255
|
end
|
|
250
|
-
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, inversed: @inversed)
|
|
257
|
+
unless @inversed
|
|
258
|
+
if Bullet::Detector::NPlusOneQuery.impossible?(owner)
|
|
259
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
|
|
260
|
+
else
|
|
261
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
|
|
262
|
+
end
|
|
256
263
|
end
|
|
257
264
|
end
|
|
258
265
|
end
|
|
@@ -200,7 +200,11 @@ module Bullet
|
|
|
200
200
|
end
|
|
201
201
|
end
|
|
202
202
|
end
|
|
203
|
-
|
|
203
|
+
if @inversed
|
|
204
|
+
Bullet::Detector::Association.add_call_object_associations(owner, reflection.name)
|
|
205
|
+
else
|
|
206
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
|
207
|
+
end
|
|
204
208
|
if records.first.class.name !~ /^HABTM_/
|
|
205
209
|
if records.size > 1
|
|
206
210
|
Bullet::Detector::NPlusOneQuery.add_possible_objects(records)
|
|
@@ -235,24 +239,27 @@ module Bullet
|
|
|
235
239
|
result = super()
|
|
236
240
|
|
|
237
241
|
if Bullet.start?
|
|
238
|
-
if owner.class.name !~ /^HABTM_/
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
242
|
+
if owner.class.name !~ /^HABTM_/
|
|
243
|
+
unless @inversed
|
|
244
|
+
if is_a? ::ActiveRecord::Associations::ThroughAssociation
|
|
245
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.through_reflection.name)
|
|
246
|
+
association = owner.association(reflection.through_reflection.name)
|
|
247
|
+
Array.wrap(association.target).each do |through_record|
|
|
248
|
+
Bullet::Detector::NPlusOneQuery.call_association(through_record, source_reflection.name)
|
|
249
|
+
end
|
|
245
250
|
|
|
246
|
-
|
|
247
|
-
|
|
251
|
+
if reflection.through_reflection != through_reflection
|
|
252
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, through_reflection.name)
|
|
253
|
+
end
|
|
248
254
|
end
|
|
249
255
|
end
|
|
250
|
-
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name)
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
+
Bullet::Detector::NPlusOneQuery.call_association(owner, reflection.name, inversed: @inversed)
|
|
257
|
+
unless @inversed
|
|
258
|
+
if Bullet::Detector::NPlusOneQuery.impossible?(owner)
|
|
259
|
+
Bullet::Detector::NPlusOneQuery.add_impossible_object(result) if result
|
|
260
|
+
else
|
|
261
|
+
Bullet::Detector::NPlusOneQuery.add_possible_objects(result) if result
|
|
262
|
+
end
|
|
256
263
|
end
|
|
257
264
|
end
|
|
258
265
|
end
|
|
@@ -13,15 +13,20 @@ module Bullet
|
|
|
13
13
|
# first, it keeps this method call for object.association.
|
|
14
14
|
# then, it checks if this associations call is unpreload.
|
|
15
15
|
# if it is, keeps this unpreload associations and caller.
|
|
16
|
-
def call_association(object, associations, caller_stack = nil)
|
|
16
|
+
def call_association(object, associations, caller_stack = nil, inversed: false)
|
|
17
17
|
return unless Bullet.start?
|
|
18
18
|
return unless Bullet.n_plus_one_query_enable?
|
|
19
19
|
return unless object.bullet_primary_key_value
|
|
20
|
-
return if inversed_objects.include?(object.bullet_key, associations)
|
|
21
20
|
|
|
21
|
+
# Record before early-returns so legitimate reads that bypass SQL
|
|
22
|
+
# (inversed, nil-polymorphic) aren't flagged as unused preloads.
|
|
22
23
|
add_call_object_associations(object, associations)
|
|
23
24
|
call_stacks.add(object.bullet_key, caller_stack) if caller_stack
|
|
24
25
|
|
|
26
|
+
return if inversed
|
|
27
|
+
return if inversed_objects.include?(object.bullet_key, associations)
|
|
28
|
+
return if optional_polymorphic_belongs_to_with_nil_type?(object, associations)
|
|
29
|
+
|
|
25
30
|
Bullet.debug(
|
|
26
31
|
'Detector::NPlusOneQuery#call_association',
|
|
27
32
|
"object: #{object.bullet_key}, associations: #{associations}"
|
|
@@ -116,6 +121,19 @@ module Bullet
|
|
|
116
121
|
|
|
117
122
|
private
|
|
118
123
|
|
|
124
|
+
def optional_polymorphic_belongs_to_with_nil_type?(object, associations)
|
|
125
|
+
return false unless associations.is_a?(Symbol) || associations.is_a?(String)
|
|
126
|
+
return false unless object.class.respond_to?(:reflect_on_association)
|
|
127
|
+
|
|
128
|
+
reflection = object.class.reflect_on_association(associations.to_sym)
|
|
129
|
+
return false unless reflection
|
|
130
|
+
return false unless reflection.respond_to?(:polymorphic?) && reflection.polymorphic?
|
|
131
|
+
return false unless reflection.respond_to?(:foreign_type)
|
|
132
|
+
|
|
133
|
+
foreign_type = reflection.foreign_type
|
|
134
|
+
foreign_type && object.respond_to?(:[]) && object[foreign_type].nil?
|
|
135
|
+
end
|
|
136
|
+
|
|
119
137
|
def create_notification(callers, klazz, associations)
|
|
120
138
|
notify_associations = Array.wrap(associations) - Bullet.get_safelist_associations(:n_plus_one_query, klazz)
|
|
121
139
|
|
|
@@ -50,7 +50,7 @@ module Bullet
|
|
|
50
50
|
if key_objects_overlap == k
|
|
51
51
|
to_add << [k, associations]
|
|
52
52
|
else
|
|
53
|
-
to_merge << [key_objects_overlap,
|
|
53
|
+
to_merge << [key_objects_overlap, eager_loadings[k].dup.merge(Array.wrap(associations))]
|
|
54
54
|
|
|
55
55
|
keys_without_objects = k - key_objects_overlap
|
|
56
56
|
to_merge << [keys_without_objects, eager_loadings[k]]
|
data/lib/bullet/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: bullet
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 8.1.
|
|
4
|
+
version: 8.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Richard Huang
|
|
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
113
113
|
- !ruby/object:Gem::Version
|
|
114
114
|
version: 1.3.6
|
|
115
115
|
requirements: []
|
|
116
|
-
rubygems_version: 4.0.
|
|
116
|
+
rubygems_version: 4.0.6
|
|
117
117
|
specification_version: 4
|
|
118
118
|
summary: help to kill N+1 queries and unused eager loading.
|
|
119
119
|
test_files: []
|