Skip to content
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

Make MiqExpression#contains work in Rails 5.2 #20051

Closed
wants to merge 3 commits into from

Conversation

kbrock
Copy link
Member

@kbrock kbrock commented Apr 6, 2020

make MiqExpression#contains work in rails 5.2

MiqExpression CONTAINS built up the sql for a contains clause.
This would only work with direct simple relations and broke in the upgrade to rails 5.2.

In this PR, rails was asked to build the sql for a query that is close. Then the extra nodes are removed.
There were bind variables that needed to be removed. We typically use just compile the query and turn into a sql literal. Unfortunately, sql IN clauses are picky about the parameter that they accept, so we needed to pass arel.

The merge conditions code broke as well. We are patching it in this PR as well

@kbrock kbrock added the rails5.2 label Apr 6, 2020
@kbrock kbrock requested review from Fryguy and gtanzillo as code owners April 6, 2020 17:37
@kbrock kbrock force-pushed the miq_expression_contains_2 branch from 171bc0a to 2ab6ce3 Compare April 6, 2020 19:26
@kbrock
Copy link
Member Author

kbrock commented Apr 6, 2020

^ fixed cops

@jrafanie
Copy link
Member

jrafanie commented Apr 7, 2020

Will this replace this commit on the rails-5-2 branch? jrafanie@1829461

@NickLaMuro
Copy link
Member

Followup: Is this Rails 5.2 only, or can we merge this into master now?

@chessbyte chessbyte changed the title Miq expression contains 5.2 compatible Make MiqExpression#contains work in Rails 5.2 Apr 7, 2020
Copy link
Member

@NickLaMuro NickLaMuro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I am 50/50 on grasping this, but left some comments to start with for now. Mostly are docs/clarification since I am still trying to understand this PR.

lib/miq_expression.rb Outdated Show resolved Hide resolved
lib/miq_expression.rb Outdated Show resolved Hide resolved
lib/miq_expression.rb Outdated Show resolved Hide resolved
lib/miq_expression.rb Outdated Show resolved Hide resolved
spec/lib/miq_expression_spec.rb Outdated Show resolved Hide resolved
spec/lib/miq_expression_spec.rb Outdated Show resolved Hide resolved
spec/lib/miq_expression_spec.rb Outdated Show resolved Hide resolved
lib/miq_expression.rb Outdated Show resolved Hide resolved
lib/miq_expression.rb Outdated Show resolved Hide resolved
lib/miq_expression.rb Show resolved Hide resolved
@NickLaMuro
Copy link
Member

@kbrock I think the PR description could really use some example cases that are being fixed here. The specs you add only test the private methods, and don't really paint a picture at what the over "SQL" or MiqExpression might be.

@kbrock kbrock force-pushed the miq_expression_contains_2 branch 3 times, most recently from 2a29306 to e8600f7 Compare April 9, 2020 06:17
@kbrock
Copy link
Member Author

kbrock commented Apr 9, 2020

heh. added a few more use cases and test
I'm having way too much fun with this
(I already have my next PR which adds multi level tagging.)

@NickLaMuro This started out trying to get this functionality working on rails 5.2. It was tricky to then get it to work on both versions.

This was a "simplification" by using rails to build the query for us, so we didn't have to create the active record objects or handle 2 different active record interfaces.

It ended up becoming an enhancement because rails did so much for us. I started adding tests/use cases for the edge cases (that had no chance of working before)

@kbrock kbrock force-pushed the miq_expression_contains_2 branch 2 times, most recently from 277ce65 to 806177f Compare April 9, 2020 16:38
Copy link
Member

@NickLaMuro NickLaMuro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments after talking with @kbrock out of band.

lib/miq_expression.rb Outdated Show resolved Hide resolved
lib/miq_expression/query_helper.rb Outdated Show resolved Hide resolved
lib/miq_expression/query_helper.rb Outdated Show resolved Hide resolved
lib/miq_expression.rb Outdated Show resolved Hide resolved
lib/miq_expression.rb Outdated Show resolved Hide resolved
lib/miq_expression.rb Outdated Show resolved Hide resolved
spec/lib/miq_expression/query_helper_spec.rb Outdated Show resolved Hide resolved
@kbrock kbrock force-pushed the miq_expression_contains_2 branch 5 times, most recently from 3b8b650 to b932ef2 Compare April 14, 2020 02:35
@kbrock
Copy link
Member Author

kbrock commented Apr 14, 2020

Added the visitor.
fixed the remaining rubocop.

@NickLaMuro is this closer to what you had in mind?

Copy link
Member

@NickLaMuro NickLaMuro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I need to look at the visitor a bit more to make sure I fully understand things before I sign off, but I think I have enough critiques with what I have already to provide a first round of feedback.

lib/miq_expression/query_helper.rb Outdated Show resolved Hide resolved
lib/miq_expression/query_helper.rb Outdated Show resolved Hide resolved
lib/miq_expression/query_helper.rb Outdated Show resolved Hide resolved
lib/miq_expression/query_helper.rb Outdated Show resolved Hide resolved
spec/lib/miq_expression/query_helper_spec.rb Outdated Show resolved Hide resolved
spec/lib/miq_expression/query_helper_spec.rb Outdated Show resolved Hide resolved

# query: "hosts"."type" = 'abc'
# result: "hosts"."type" = 'abc'
# This doesn't make sense, but we saw this example
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"saw this example" where? In specs? In production? In a magical fairy land where raw SQL is allowed to roam freely?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how the parameter was coming into our code for our code example

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in miq test suite (thought it could have been another problem. who knows

expect(result).to be_nil
end

# query: <invalid>
Copy link
Member

@NickLaMuro NickLaMuro Apr 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: Wow... that didn't come out right... let me try that again...

I feel like <invalid> is not serving much of a purpose, helping to describe what <invalid> is supposed to be, or where it might happen. Again, similar comment to the "but we saw this example" one.

spec/lib/miq_expression/query_helper_spec.rb Outdated Show resolved Hide resolved
spec/lib/miq_expression_spec.rb Outdated Show resolved Hide resolved
@kbrock kbrock force-pushed the miq_expression_contains_2 branch 4 times, most recently from 22ef577 to 07334ad Compare April 15, 2020 16:30
@kbrock kbrock force-pushed the miq_expression_contains_2 branch 2 times, most recently from 90e0640 to ac6aae8 Compare April 15, 2020 19:53
Copy link
Member

@NickLaMuro NickLaMuro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI about Arel::Visitors::Visitor

end
end
# rubocop:disable Naming/MethodName
class RemoveEqualityVisitor < Arel::Visitors::Reduce
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this exists in Rails 6... possibly in Rails 5.2 depending on what version of ActiveRecord we bumped to in the upgrade:

rails/arel#512

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, it looks like it was effectively Arel::Visitors::Visitor anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can cross that bridge when we upgrade to 6.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a "I did a bundle open activerecord and couldn't find this class", not a "this might be an issue in the future" thing. I see this being a problem right now, not later.

Copy link
Member Author

@kbrock kbrock Apr 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kbrock kbrock force-pushed the miq_expression_contains_2 branch from 0ee8cdb to d81cb97 Compare April 22, 2020 03:38
@kbrock
Copy link
Member Author

kbrock commented Apr 22, 2020

updated the typo and rebased to recent version of miq. hoping that fixes the rubygem fetch issue

@Fryguy Fryguy removed their request for review April 24, 2020 22:28
this is needed to support rails 5.2
@kbrock kbrock force-pushed the miq_expression_contains_2 branch from d81cb97 to c54488b Compare April 25, 2020 01:02
@kbrock
Copy link
Member Author

kbrock commented Apr 25, 2020

sorry nick. this contains a few rails 5.2 vs 5.1 switches.
once we get this all set, I can remove them. I just wanted this off of master and the other one off of 5.2.

also, thinking I'll drop the traverser commit. Not really doing any good besides making it difficult to rebase stuff.

@NickLaMuro
Copy link
Member

also, thinking I'll drop the traverser commit. Not really doing any good besides making it difficult to rebase stuff.

That is fine. What I tend to do, though (and you don't have to), is copy the branch prior to dropping a commit like that, and push it to my fork for historical reference, and link it in the PR (assuming you don't prune it later). That way there is a way for someone to take a look at what was considered previously when looking back on this PR.

@NickLaMuro
Copy link
Member

sorry nick. this contains a few rails 5.2 vs 5.1 switches.

Okay, if that is the case, I won't be reviewing further then. I have made my case against this multiple times, and still disagree with you on this. I don't think it will be productive having me review further.

@kbrock
Copy link
Member Author

kbrock commented Apr 28, 2020

reverted this PR back to previous form with a visitor.

I prefer new way #20110

@kbrock kbrock force-pushed the miq_expression_contains_2 branch from 6005c51 to c97bafe Compare April 28, 2020 13:18
@miq-bot
Copy link
Member

miq-bot commented Apr 29, 2020

Checked commits kbrock/manageiq@6f099e9~...c97bafe with ruby 2.5.7, rubocop 0.69.0, haml-lint 0.28.0, and yamllint
4 files checked, 1 offense detected

lib/miq_expression/remove_equality_visitor.rb

@miq-bot
Copy link
Member

miq-bot commented Apr 29, 2020

This pull request is not mergeable. Please rebase and repush.

@NickLaMuro
Copy link
Member

Closing this since this is the old form of the PR.

@NickLaMuro NickLaMuro closed this Apr 29, 2020
@kbrock kbrock deleted the miq_expression_contains_2 branch April 30, 2020 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants