Skip to content

Change disable_joins to apply limit scope

Patrick Bair requested to merge pb-allow-disable-joins-limit into master

What does this MR do and why?

The disable_joins flag on :through associations causes joins to be split into individual queries, for cross-database access. Internally, disable_joins does pluck on each step of the association to retrieve the keys to field to the next query.

In some cases, we have has_one relationships where we want to limit the internal pluck to return only the first result (where we can prove this has the correct behavior).

This MR adds an option that allows disable_joins to allow that behavior.

Related to #339908

How to set up and validate locally

  1. Create objects in the database from the rails console
    project = Project.first
    environment = FactoryBot.create(:environment, project: project)
    pipeline = FactoryBot.create(:ci_pipeline, project: project)
    build = FactoryBot.create(:ci_build, pipeline: pipeline)
    FactoryBot.create(:deployment, :success, project: project, environment: environment, deployable: build)  
  2. Check the last_visible_deployable relation to verify the limit is applied to each query
    environment.last_visible_deployable
      (0.5ms)  SELECT "deployments"."deployable_id" FROM "deployments" WHERE "deployments"."environment_id" = 4 AND "deployments"."status" IN (1, 2, 3, 4) AND "deployments"."deployable_type" = 'CommitStatus' ORDER BY environment_id, deployments.id DESC LIMIT 1
      CommitStatus Load (0.4ms)  SELECT "ci_builds".* FROM "ci_builds" WHERE "ci_builds"."id" = 2 LIMIT 1 
  3. Check the last_visible_pipeline relation to verify the limit is applied to each query
    environment.last_visible_pipeline
      (0.7ms)  SELECT "deployments"."deployable_id" FROM "deployments" WHERE "deployments"."environment_id" = 4 AND "deployments"."status" IN (1, 2, 3, 4) AND "deployments"."deployable_type" = 'CommitStatus' ORDER BY environment_id, deployments.id DESC LIMIT 1
      (0.3ms)  SELECT "ci_builds"."commit_id" FROM "ci_builds" WHERE "ci_builds"."id" = 2 LIMIT 1
      Ci::Pipeline Load (0.3ms)  SELECT "ci_pipelines".* FROM "ci_pipelines" WHERE "ci_pipelines"."id" = 2 ORDER BY "ci_pipelines"."id" ASC LIMIT 1

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 Patrick Bair

Merge request reports

Loading