Skip to content

Simplify where item query - #8068

Merged
janmeier merged 5 commits into
sequelize:masterfrom
yonjah:simplify-whereItemQuery
Sep 18, 2017
Merged

janmeier merged 5 commits into
sequelize:masterfrom
yonjah:simplify-whereItemQuery

Conversation

@yonjah

@yonjah yonjah commented Aug 7, 2017

Copy link
Copy Markdown
Contributor

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Abstract dialect query-generator whereItemQuery method was more than 400 lines and had a cyclic complexity of 85. That made it really hard to understand what's going on.
Since I have a few other issues which require some changes to this method I thought it would be a lot better if I (and other developers) have better understanding of what's going on before I do even minor logic changes.
I splited the method to smaller private methods.
I tried to converge logic and simplify logic where it was possible.
I also noticed a few bugs in the original implementation which I solved and added tests for
(Issue #3824 which was actually fixed in #4298 but than got overwritten in #7094)

yonjah added 3 commits August 4, 2017 11:03
…ry-generator whereItemQuery by

whereItemQuery had cyclic complexity of 85 reduced to 26 to make logic a bit easier to follow.
Logic is almost identical but at places where logic looked like an obvoius error it was tweeked, like -
	_traverseJSON had weird logic if the item was plain object -
		cast wast set by either path (ok) or the first value of the first property of an object (doesn't make sense)
		where value passed into whereItemQuery was in the main function scope causing it to accumulate properties on each iteration (doesn't make sense)
All test are passing with no issues but since there were minor logic changes there might be some edge cases needed to be addressed.
Though it's more resonable to assume changes will fix bugs related to those edge cases than cause them
… sqlite casting and handling of

postgres and sqlite handle casting and JSONB formats a bit diffrently yet some previous commits
didn't took this changes into consideration when handling casting and didn't test casting properly
@yonjah

yonjah commented Aug 7, 2017

Copy link
Copy Markdown
Contributor Author

Working on this I noticed Sequelize eslint rules can benefit from a few extra rules.
It might be useful to extend the "eslint:recommended" rules which protect against using duplicate keys in objects and reassigning values to const among other things (those are the two I noticed that were missing).
Also I used the complexity rule to calculate the cyclic complexity of the methods ( but I'm not sure if this is as important as the other rules)

@alitaheri

Copy link
Copy Markdown
Contributor

Great job 👍 👍 I've had a really hard time when I was working on range types. Thanks a lot 😍

@mickhansen

Copy link
Copy Markdown
Contributor

I skimmed it, and it looks like a decent way to go.
One thing i considered back when i wrote that monstrosity was to split off the logic into a seperate class/object with methods there, i see you added a few new methods to the general QueryGenerator class so i think it might make sense to split it out into a WHERE query builder.

@yonjah

yonjah commented Aug 7, 2017

Copy link
Copy Markdown
Contributor Author

@mickhansen I thought of separating the logic to a different class but didn't want to diverge too much of the original implementation.
If you think it will be better I can move it out and update the pull request.

One small thing though is that currently each dialect have their own query-generator which is being extended by the dialect and being loaded into it.
Adding a Where Query Builder will mean that each query generator will load its own extension to that as well. It's not a major issue but this design feels a bit awkward to work with.

@mickhansen

Copy link
Copy Markdown
Contributor

If you think it will be better I can move it out and update the pull request.

I think it would be great if we could seperate it.

Adding a Where Query Builder will mean that each query generator will load its own extension to that as well. It's not a major issue but this design feels a bit awkward to work with.

It definitely feels a bit off sometimes, open to suggestions.

@yonjah

yonjah commented Aug 7, 2017

Copy link
Copy Markdown
Contributor Author

It definitely feels a bit off sometimes, open to suggestions.

I wish I had some concrete suggestion but I have nothing specific to offer at the moment

I think it would be great if we could seperate it.

I looked into it a bit more.
whereItemQuery whereItemsQuery and all the private methods I added are using a few other methods on the query-generator like -
escape, handleSequelizeMethod, quoteIdentifier, quoteTable, jsonPathExtractionQuery

And untangling them out of the query-generator will not be simple (or necessarily make sense).
I think some major refactoring will need to be done to actually get something that feels consistent and make sense across all dialects. Not really sure if I currently have the time to do such a major change ( But I really hope I'll have it in the future I think Sequelize can benefit a lot from a simple and extendable dialect api that will allow external dialects to be loaded in).

I can still move the where logic to a separate module but there will need to be some kind of cyclic dependency between it and the query-generator which again feels awkward

@yonjah

yonjah commented Aug 17, 2017

Copy link
Copy Markdown
Contributor Author

@mickhansen do you have an insight on how to separate it or should I leave it as is ?

@yonjah yonjah mentioned this pull request Sep 1, 2017
5 tasks done
@codecov

codecov Bot commented Sep 14, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #8068 into master will decrease coverage by 0.03%.
The diff coverage is 97.9%.

@sushantdhiman
sushantdhiman requested a review from a team September 14, 2017 16:30
@sushantdhiman sushantdhiman added the type: refactor DEPRECATED: replace with the "meta" issue type label Sep 14, 2017
@sushantdhiman sushantdhiman mentioned this pull request Sep 14, 2017
5 tasks done

testsql('data', {
price: 5,
name: 'Product'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you add unit tests for boolean and date matching as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you mean to duplicate each $between and $in tests to include all possible casting types ?
Cause we have tests for those castings in Line 934 and 949.

I added the $between and $in tests because previous logic didn't apply casting at all to them

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Cause we have tests for those castings in Line 934 and 949.

Roger that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: refactor DEPRECATED: replace with the "meta" issue type

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants