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

Classification parent #18301

Merged
merged 1 commit into from
Jun 13, 2019
Merged

Classification parent #18301

merged 1 commit into from
Jun 13, 2019

Conversation

kbrock
Copy link
Member

@kbrock kbrock commented Dec 18, 2018

blocked:

Extracted from #17210
Thanks for pinging me on this one @Fryguy

Before:

Currently we use Classification#parent_id == 0 to mean there is a Category and it has no parent.

After

ActiveRecord has the convention of having parent_id == nil to mean there is no parent.
So we follow that convention: Classification#parent_id == nil when this is a Category and it has no parent.
We now pass around nil when there is no parent (we were passing 0). But we still handle passing a 0 in case some code is not properly updated across the various repos.

This PR:

  • cleans up our use of name2tag.
  • cleans up our interface for Tag.find_by_classification_name.
  • relies less upon parent_id value, using more scopes.
  • more lenient code handling parent_id of 0 or nil.

@Fryguy
Copy link
Member

Fryguy commented Dec 18, 2018

@kbrock There's a lot going on here, but I can't tell if this is all drive-by cleanups or required to get parent_id => nil working.

Also as I mentioned in the migration PR, I'd prefer we just cut off parent_id 0 altogether, with the migration of 0 to nil being the cutover point.

@kbrock kbrock force-pushed the classification_parent branch from 12bfba1 to 2d8e92c Compare December 19, 2018 00:50
@kbrock
Copy link
Member Author

kbrock commented Dec 19, 2018

Re: Rubocop

The rubocop comments don't make sense for this PR.

The .zero? cause null object errors and the find_by_name was introduced a long time ago and is not a simple find_by(:name => like the comment suggests

@kbrock kbrock changed the title Classification parent [WIP] Classification parent Dec 20, 2018
@kbrock
Copy link
Member Author

kbrock commented Dec 20, 2018

WIP: splitting this up

@miq-bot miq-bot added the wip label Dec 20, 2018
@cben
Copy link
Contributor

cben commented Dec 20, 2018

I did see your WIP message but finally found some time so reviewed first commit ("Tag#find_by_classification_name"). LGTM on that, with couple of optional suggestions.

[The root cause of many things that annoy me around namespaces is that Classification pretends it knows its namespace but actually gets it from slicing the name of the associated tag (unless it's a new classification that first knows its ns, then uses that to compute save_tag).
In an ideal world, categories would have a parent — the namespace — and a lot of code passing ns around could be unnecessary. That'd be a hugely invasive change, out of scope possibly forever.]

app/models/classification.rb Outdated Show resolved Hide resolved
@@ -395,7 +398,7 @@ def self.import_from_hash(classification, parent = nil)

stats = {"categories" => 0, "entries" => 0}

if classification["parent_id"] == 0 # category
if parent.nil?
Copy link
Contributor

Choose a reason for hiding this comment

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

So the hash contains (or might contain?) a "parent_id" key, and we get a parent arg as well?
Checking parent looks fine, but are the two always in sync? Below (L411) we create(classification) which would use classification["parent_id"], ignoring parent.

Looking at db/fixtures/classifications.yml, categories come with :parent_id: 0, and entries without :parent_id.
OK, I think I see. The recursive call goes into the other branch, which sets "parent_id" => parent.id.

=> Assuming the imported yaml is always rooted at categories, LGTM.
Consider also forcing "parent_id" => parent.id in the categories branch too.

P.S. db/fixtures/classifications.yml is not really relevant. That file is loaded by .seed which uses completely separate code 🤦‍♂️

Copy link
Contributor

@cben cben Jan 8, 2019

Choose a reason for hiding this comment

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

This import_from_yaml->import_from_hash code path is only used by tools/import_tags.rb. I don't think we have tests or examples what input looks like, although it's pretty obvious it should be symmetric to tools/export_tags.rb -> Classification.export_to_yaml. That one is always rooted at categories, omitting "parent_id" from entries => satisfies assumptions here 👍

There is also a 3rd completely separate importing code in lib/task_helpers/imports/tags.rb.
And a 4th completely separate https://github.com/rhtconsulting/cfme-rhconsulting-scripts/blob/master/rhconsulting_tags.rake
😕 🤦‍♂️ 😆
oh well, consolidating is totally not the goal here, I'm just amused.
EDIT: 3rd was based on 4th (#16983)

Copy link
Contributor

@cben cben Jan 8, 2019

Choose a reason for hiding this comment

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

  • However you also need to update lib/task_helpers/exports/tags.rb, which has :parent_id => 0.
  • lib/task_helpers/imports/tags.rb looks unaffected to me?

app/models/classification.rb Outdated Show resolved Hide resolved
Tag.in_region(region_id).find_by(:name => tag_name)
end

def save_tag
tag_name = Classification.name2tag(name, parent_id, ns)
tag_name = Classification.name2tag(name, parent, ns)
Copy link
Contributor

Choose a reason for hiding this comment

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

orthogonal to this PR but valid optimization, avoids unnecessary find 👍

Copy link
Member Author

@kbrock kbrock Jan 14, 2019

Choose a reason for hiding this comment

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

yes, this was unnecessary

(but I can't bring myself to remove it :) )

app/models/classification.rb Outdated Show resolved Hide resolved
@cben cben mentioned this pull request Jan 8, 2019
@kbrock kbrock force-pushed the classification_parent branch 2 times, most recently from 1de6131 to 524f7b4 Compare January 14, 2019 19:20
@kbrock kbrock force-pushed the classification_parent branch from 524f7b4 to 1ad70ac Compare January 14, 2019 20:23
@cben
Copy link
Contributor

cben commented Jan 14, 2019

Looks great! 💯

Didn't look deep into Travis failures. Must be tested locally with schema change.
But one thing there stood out — Classification#ns method contains following logic before this change:

if category?
  @ns = tag2ns(tag.name)
else
  @ns = tag2ns(parent.tag.name) unless parent_id.nil?
end

Huh. So there are presently situations when parent_id is nil, which is distinct from parent_id==0 meaning a category?! 😕

@kbrock
Copy link
Member Author

kbrock commented Jan 16, 2019

Huh. So there are presently situations when parent_id is nil, which is distinct from parent_id==0 meaning a category?! 😕
-- @cben

Yea, think that is present when creating records.
I seem to remember removing that check and a few specs blowing up.

Merging nil and 0 will make this case disappear.

re: travis failures

From what I can tell, I think this is failing travis because the database itself has a category_id default of 0. So they really need to be applied together.

But this assumption is also why I want to get as much code out of here.
So stuff doesn't sneak through.

@kbrock kbrock force-pushed the classification_parent branch from 1ad70ac to 07800d4 Compare January 30, 2019 01:00
@kbrock kbrock force-pushed the classification_parent branch from 07800d4 to dd5238d Compare January 30, 2019 23:26
@kbrock kbrock force-pushed the classification_parent branch 2 times, most recently from db65f63 to 88d5077 Compare January 31, 2019 00:44
@cben
Copy link
Contributor

cben commented Jan 31, 2019

[I've gone silent partly because I hoped to look deeper into "present situations when parent_id is nil" but never found the time, but also because I wasn't sure if this is finished. Please ping me explicitly if you want another look.]

@bdunne bdunne self-assigned this Jun 13, 2019
@kbrock kbrock force-pushed the classification_parent branch from 88d5077 to 21aec89 Compare June 13, 2019 18:09
@kbrock kbrock changed the title [WIP] Classification parent Classification parent Jun 13, 2019
@kbrock kbrock force-pushed the classification_parent branch from 21aec89 to 7c5f3d1 Compare June 13, 2019 19:14
@miq-bot
Copy link
Member

miq-bot commented Jun 13, 2019

Checked commit kbrock@7c5f3d1 with ruby 2.3.3, rubocop 0.69.0, haml-lint 0.20.0, and yamllint 1.10.0
4 files checked, 3 offenses detected

app/models/classification.rb

@bdunne bdunne merged commit 5dcc435 into ManageIQ:master Jun 13, 2019
@bdunne bdunne added this to the Sprint 114 Ending Jun 24, 2019 milestone Jun 13, 2019
@kbrock kbrock deleted the classification_parent branch June 13, 2019 20:33
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.

5 participants