Skip to content

Backport :disable_joins option from Rails main

Patrick Bair requested to merge pb-backport-disable-joins-from-rails into master

What does this MR do?

Related issue: #336420 (closed)

Allow a disable_join for has_one :through and has_many :through associations, so that it will use two separate queries instead of a join. See the related issue for more details.

Example query with the feature flag disabled (current behavior):

SELECT "deployments".*
FROM "deployments"
INNER JOIN "ci_builds"
    ON "deployments"."deployable_id" = "ci_builds"."id"
WHERE "ci_builds"."type" = 'Ci::Build'
    AND "ci_builds"."commit_id" = 1
    AND "deployments"."deployable_type" = 'CommitStatus'

And with the feature flag enabled, the two queries:

SELECT "ci_builds"."id"
FROM "ci_builds"
WHERE "ci_builds"."commit_id" = 1
  AND "ci_builds"."type" = 'Ci::Build'
SELECT "deployments".*
FROM "deployments"
WHERE "deployments"."deployable_id" IN (1, 2, 3, 4, 5, 6, 7, 8)
  AND "deployments"."deployable_type" = 'CommitStatus'

I also added an example feature flag for demonstration and to test how it works. I'll remove the example flag before merging. Example with the feature flag enabled shows the two raw queries:

[2] pry(main)> p.deployments
  Feature::FlipperGate Load (0.7ms)  SELECT "feature_gates".* FROM "feature_gates" WHERE "feature_gates"."feature_key" = $1 /*application:console,line:/lib/feature.rb:84:in `enabled?'*/  [["feature_key", "disable_joins_on_ci_pipeline"]]
   (3.9ms)  SELECT "ci_builds"."id" FROM "ci_builds" WHERE "ci_builds"."commit_id" = $1 AND "ci_builds"."type" = $2 /*application:console,line:/config/initializers/00_rails_disable_joins.rb:137:in `block in last_scope_chain'*/  [["commit_id", 1], ["type", "Ci::Build"]]
  Deployment Load (8.7ms)  SELECT "deployments".* FROM "deployments" WHERE "deployments"."deployable_id" IN ($1, $2, $3, $4, $5, $6, $7, $8) AND "deployments"."deployable_type" = $9 /*application:console,line:/config/initializers/00_rails_disable_joins.rb:93:in `find_target'*/  [[nil, 1], [nil, 2], [nil, 3], [nil, 4], [nil, 5], [nil, 6], [nil, 7], [nil, 8], ["deployable_type", "CommitStatus"]]

Screenshots or Screencasts (strongly suggested)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Edited by Patrick Bair

Merge request reports