Use PipelinePreloader for ScheduledPipelineExecutionPolicyTestRun
What does this MR do and why?
Converts the with_associations scope on Security::ScheduledPipelineExecutionPolicyTestRun to use Ci::Preloaders::PipelinePreloader for partition-aware pipeline preloading, instead of relying on ActiveRecord's default preload(:pipeline).
Why
Ci::Pipeline is stored in a partitioned table (p_ci_pipelines). When ActiveRecord's default preloader loads a belongs_to :pipeline association in bulk, it issues a single SELECT ... WHERE id IN (...) query without a partition_id filter. This forces PostgreSQL to scan all partitions, which is inefficient and can cause N+1-style cross-partition queries as the number of partitions grows.
Ci::Preloaders::PipelinePreloader solves this by using Gitlab::Ci::Pipeline::BulkByIdLookup, which resolves the correct partition_id for each pipeline ID (via the cached Ci::Partition ranges) and issues partition-scoped queries. This avoids full partition scans and keeps pipeline lookups efficient as the CI sliding-list partitioning scheme adds new partitions over time.
What
- Extracts a new
preload_pipelinescope onScheduledPipelineExecutionPolicyTestRunthat chainswith_partition_aware_preload.preload(:pipeline), delegating to the partition-aware preloader. - Refactors
with_associationsto callpreload_pipelinefirst, then chains the remaining associations (project: :route,security_policy: :security_policy_management_project) and theorder(id: :desc)clause. - Adds a spec asserting that
with_associationspreloads thesecurity_policyassociation (a previously untested association in the scope).
Related: #601100
References
- Issue: #601100
Ci::Preloaders::PipelinePreloader:app/models/ci/preloaders/pipeline_preloader.rb
Screenshots or screen recordings
Not applicable — backend-only change with no UI impact.
| Before | After |
|---|---|
How to set up and validate locally
- Create a project with a security policy that has scheduled pipeline execution policy test runs.
- Query
Security::ScheduledPipelineExecutionPolicyTestRun.with_associationsand verify no N+1 queries are issued againstp_ci_pipelinesacross partitions. - Run the model spec:
bundle exec rspec ee/spec/models/security/scheduled_pipeline_execution_policy_test_run_spec.rb
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.