Append `partition_id` to queries triggered through a build metadata association

Description

We are working on CI Partitioning: https://docs.gitlab.com/ee/architecture/blueprints/ci_data_decay/pipeline_partitioning.html.

One of the biggest technical challenges right now is how to augment our Rails associations to add partition_id filter using our uniform partition id mechanism.

We could use "lambda associations" Rails feature, but it doesn't work with .preload:

has_one :build_metadata, -> (build) { where(partition_id: build.partition_id) }
ArgumentError: The association scope 'builds' is instance dependent (the
scope block takes an argument). Preloading instance dependent scopes is not
supported.

We also can't backport Rails 7 path, since it is not compatible: #377521 (comment 1135847398)

Proposal

Find a way to append partition_id in a way that it works with preloads and does not change too much of ActiveRecord internals.

/cc @mbobin @morefice @jheimbuck_gl

Edited Oct 14, 2022 by Grzegorz Bizon
Assignee Loading
Time tracking Loading