-
-
Notifications
You must be signed in to change notification settings - Fork 828
Fix Ransack redundant left join statements #1612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
5
commits into
main
Choose a base branch
from
copilot/fix-ec6bbd02-18bf-41b3-af8a-f9165e40a90f
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2ade54f
Initial plan
Copilot b4b52fe
Fix redundant LEFT JOIN generation when INNER JOINs exist
Copilot 39b5d60
Changes before error encountered
Copilot dab7eb8
Merge branch 'main' into copilot/fix-ec6bbd02-18bf-41b3-af8a-f9165e40…
scarroll32 0e25523
Simplify fix: track all join types in @joined_tables to prevent dupli…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
spec/ransack/adapters/active_record/redundant_joins_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| require 'spec_helper' | ||
|
|
||
| module Ransack | ||
| module Adapters | ||
| module ActiveRecord | ||
| describe 'Redundant Join Prevention' do | ||
| describe 'with nested associations' do | ||
| context 'when relation already has INNER JOINs' do | ||
| it 'should not generate redundant LEFT OUTER JOINs for nested associations' do | ||
| # Test case from issue: Comment.joins(article: :author).ransack(article_author_name_eq: 'abc') | ||
| # This was generating both INNER JOIN and LEFT OUTER JOIN to authors table | ||
| search = Comment.joins(article: :author).ransack(article_author_name_eq: 'test') | ||
| sql = search.result.to_sql | ||
|
|
||
| # Should contain only one JOIN to authors table | ||
| authors_joins = sql.scan(/JOIN "authors"/).length | ||
| expect(authors_joins).to eq(1), "Expected 1 join to authors table, but found #{authors_joins} in SQL: #{sql}" | ||
|
|
||
| # Should not contain LEFT OUTER JOIN with aliased authors table | ||
| expect(sql).not_to match(/LEFT OUTER JOIN "authors" "authors_\w+"/), | ||
| "SQL should not contain LEFT OUTER JOIN with aliased authors table: #{sql}" | ||
|
|
||
| # Should contain WHERE clause referencing authors.name | ||
| expect(sql).to match(/"authors"\."name" = 'test'/), | ||
| "SQL should contain WHERE clause for authors.name: #{sql}" | ||
| end | ||
|
|
||
| it 'should work correctly with simple single-level joins' do | ||
| # Test case from issue: Comment.joins(:article).ransack(article_title_eq: 'abc') | ||
| # This should continue to work as before (only one JOIN to articles) | ||
| search = Comment.joins(:article).ransack(article_title_eq: 'test') | ||
| sql = search.result.to_sql | ||
|
|
||
| # Should contain only one JOIN to articles table | ||
| articles_joins = sql.scan(/JOIN "articles"/).length | ||
| expect(articles_joins).to eq(1), "Expected 1 join to articles table, but found #{articles_joins} in SQL: #{sql}" | ||
|
|
||
| # Should contain WHERE clause referencing articles.title | ||
| expect(sql).to match(/"articles"\."title" = 'test'/), | ||
| "SQL should contain WHERE clause for articles.title: #{sql}" | ||
| end | ||
| end | ||
|
|
||
| context 'when relation has no existing joins' do | ||
| it 'should generate appropriate LEFT OUTER JOINs for search conditions' do | ||
| # When no existing joins, Ransack should create necessary LEFT OUTER JOINs | ||
| search = Comment.ransack(article_author_name_eq: 'test') | ||
| sql = search.result.to_sql | ||
|
|
||
| # Should contain LEFT OUTER JOINs for both articles and authors | ||
| expect(sql).to match(/LEFT OUTER JOIN "articles"/), | ||
| "SQL should contain LEFT OUTER JOIN for articles: #{sql}" | ||
| expect(sql).to match(/LEFT OUTER JOIN "authors"/), | ||
| "SQL should contain LEFT OUTER JOIN for authors: #{sql}" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NoMethodError: undefined method 'populate_joined_tables_from_existing_joins' for an instance of ActiveRecord::Associations::JoinDependency