Backport :disable_joins option from Rails main
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
-
I have included changelog trailers, or none are needed. (Does this MR need a changelog?) -
I have added/updated documentation, or it's not needed. (Is documentation required?) -
I have properly separated EE content from FOSS, or this MR is FOSS only. (Where should EE code go?) -
I have added information for database reviewers in the MR description, or it's not needed. (Does this MR have database related changes?) -
I have self-reviewed this MR per code review guidelines. -
This MR does not harm performance, or I have asked a reviewer to help assess the performance impact. (Merge request performance guidelines) -
I have followed the style guides. -
This change is backwards compatible across updates, or this does not apply.
Availability and Testing
-
I have added/updated tests following the Testing Guide, or it's not needed. (Consider all test levels. See the Test Planning Process.) -
I have tested this MR in all supported browsers, or it's not needed. -
I have informed the Infrastructure department of a default or new setting change per definition of done, or it's not needed.
Edited by Patrick Bair