Skip to content

Allow `each_batch` to work with scopes that have preloading

Brian Williams requested to merge bwill/fix-each-batch-preloads into master

What does this MR do and why?

Allow each batch to work on scopes with have preloading.

Before:

[1] pry(main)> Sbom::Occurrence.preload(:component, :component_version, :source).each_batch(of: 100) do |batch|
  batch.each { |o| o }
  break
end
  Sbom::Occurrence Load (3.6ms)  SELECT "sbom_occurrences"."id" FROM "sbom_occurrences" ORDER BY "sbom_occurrences"."id" ASC LIMIT 1 /*application:console,db_config_name:main,console_hostname:caladan.lan,console_username:bwilliams,line:/app/models/concerns/each_batch.rb:62:in `each_batch'*/
ActiveModel::MissingAttributeError: missing attribute: component_id
from /Users/bwilliams/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/activemodel-7.0.6/lib/active_model/attribute.rb:226:in `value'

After:

[1] pry(main)> Sbom::Occurrence.preload(:component, :component_version, :source).each_batch(of: 100) do |batch|
  batch.each { |o| o }
  break
end
  Sbom::Occurrence Load (3.3ms)  SELECT "sbom_occurrences"."id" FROM "sbom_occurrences" ORDER BY "sbom_occurrences"."id" ASC LIMIT 1 /*application:console,db_config_name:main,console_hostname:caladan.lan,console_username:bwilliams,line:/app/models/concerns/each_batch.rb:62:in `each_batch'*/
  Sbom::Occurrence Load (0.4ms)  SELECT "sbom_occurrences"."id" FROM "sbom_occurrences" WHERE "sbom_occurrences"."id" >= 2 ORDER BY "sbom_occurrences"."id" ASC LIMIT 1 OFFSET 100 /*application:console,db_config_name:main,console_hostname:caladan.lan,console_username:bwilliams,line:/app/models/concerns/each_batch.rb:81:in `block in each_batch'*/
  Sbom::Occurrence Load (0.9ms)  SELECT "sbom_occurrences".* FROM "sbom_occurrences" WHERE "sbom_occurrences"."id" >= 2 AND "sbom_occurrences"."id" < 102 /*application:console,db_config_name:main,console_hostname:caladan.lan,console_username:bwilliams,line:(pry):2:in `block in __pry__'*/
  Sbom::Component Load (4.1ms)  SELECT "sbom_components".* FROM "sbom_components" WHERE "sbom_components"."id" IN (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96) /*application:console,db_config_name:main,console_hostname:caladan.lan,console_username:bwilliams,line:(pry):2:in `block in __pry__'*/
  Sbom::ComponentVersion Load (3.8ms)  SELECT "sbom_component_versions".* FROM "sbom_component_versions" WHERE "sbom_component_versions"."id" IN (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101) /*application:console,db_config_name:main,console_hostname:caladan.lan,console_username:bwilliams,line:(pry):2:in `block in __pry__'*/
=> nil

What are the relevant issues numbers?

Technical evaluation: each_batch does not work ... (#423307 - closed)

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Adam Cohen

Merge request reports

Loading