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

fix: bug importing tags with the same value as an existing one #141

Conversation

rpenido
Copy link
Contributor

@rpenido rpenido commented Jan 9, 2024

openedx/modular-learning#170

Description

If we try to import tags over a taxonomy that shares the same Tag Value, an error is thrown about the conflict tag value.

It can be reproduced by importing the Template Taxonomy over the "Lightcast Open Skills Taxonomy".
Both taxonomies have a tag with the value "piano". In the plan, it tries to create the tag piano for the Template before deleting the one from Lightcast, raising an error.

This PR changes the order of the import actions, always running the DeleteTag actions before others to avoid this.

Testing Instructions

Please ensure that the tests cover the expected behavior.


Private-ref:

@openedx-webhooks
Copy link

openedx-webhooks commented Jan 9, 2024

Thanks for the pull request, @rpenido! Please note that it may take us up to several weeks or months to complete a review and merge your PR.

Feel free to add as much of the following information to the ticket as you can:

  • supporting documentation
  • Open edX discussion forum threads
  • timeline information ("this must be merged by XX date", and why that is)
  • partner information ("this is a course on edx.org")
  • any other information that can help Product understand the context for the PR

All technical communication about the code itself will be done via the GitHub pull request interface. As a reminder, our process documentation is here.

Please let us know once your PR is ready for our review and all tests are green.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Jan 9, 2024
@rpenido rpenido marked this pull request as draft January 9, 2024 17:45
@rpenido rpenido force-pushed the rpenido/fal-3603-fix-import-tag-with-same-value branch from af48902 to 2312917 Compare January 9, 2024 17:53
@rpenido rpenido force-pushed the rpenido/fal-3603-fix-import-tag-with-same-value branch from 2312917 to 0b31545 Compare January 9, 2024 18:00
@rpenido rpenido marked this pull request as ready for review January 9, 2024 18:04
Copy link
Contributor

@ChrisChV ChrisChV left a comment

Choose a reason for hiding this comment

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

@rpenido Looks good 👍, only one suggestion

  • I tested this: I tested that the bug is gone
  • I read through the code and considered the security, stability and performance implications of the changes.
  • Includes tests for bugfixes and/or features added.
  • Includes documentation

@@ -139,6 +139,13 @@ def generate_actions(
tag.external_id: tag for tag in self.taxonomy.tag_set.all()
}

for tag in tags:
Copy link
Contributor

Choose a reason for hiding this comment

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

@rpenido Maybe to avoid this loop, you can add the delete actions first using the _build_action() func, adding a new param add_first. What do you think?

Copy link
Contributor Author

@rpenido rpenido Jan 10, 2024

Choose a reason for hiding this comment

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

My first attempt was just to change the self.actions.append(action) to self.actions.insert(0, action) for the DeleteTag here (this was your suggestion?):

# Add action
self.actions.append(action)

This didn't solve the problem because we ran the validation below (inside _build_action() for the CreateTag action) in the plan stage (before actually running the delete action).

# We validate if there are no inconsistencies when executing this action
self.errors.extend(action.validate(self.indexed_actions))

# Validates if exists a tag with the same value on the Taxonomy
taxonomy_tag = self.taxonomy.tag_set.get(value=self.tag.value)
return ImportActionError(
action=self,
message=_(
"Duplicated tag value with tag in database (external_id={external_id})."
).format(external_id=taxonomy_tag.external_id)
)

I need to skip this validation if the tag value has a DeleteTag action, so I need to build all the delete actions (calling _build_delete_actions) before validating the new tags.

I also tried to avoid this loop filtering while we constructed the dict here:

tags_for_delete = {
tag.external_id: tag for tag in self.taxonomy.tag_set.all()
}

But because we have tags (from the DB) and tags (from the importing param), the filtering code became nearly intelligible, so I opted for the loop outside (since it is not a nested loop).

Copy link
Contributor

Choose a reason for hiding this comment

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

My first attempt was just to change the self.actions.append(action) to self.actions.insert(0, action) for the DeleteTag here (this was your suggestion?):

Yes, it is my suggestion.

But because we have tags (from the DB) and tags (from the importing param), the filtering code became nearly intelligible, so I opted for the loop outside (since it is not a nested loop).

I see, in that case the loop is fine. Thanks for the explanation!

Copy link
Contributor

@bradenmacdonald bradenmacdonald left a comment

Choose a reason for hiding this comment

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

I would prefer a few more comments to explain what the code is doing, as it's a little hard to follow in some places, but overall this seems fine to me. Thanks!

Should we merge #135 first?

@rpenido
Copy link
Contributor Author

rpenido commented Jan 10, 2024

Should we merge #135 first?

They are independent. You could merge this first.

We will have a minor conflict anyway in the tests because:

I added a comment in the code that will conflict.

@bradenmacdonald bradenmacdonald merged commit 8e7af54 into openedx:main Jan 10, 2024
7 checks passed
@openedx-webhooks
Copy link

@rpenido 🎉 Your pull request was merged! Please take a moment to answer a two question survey so we can improve your experience in the future.

@bradenmacdonald bradenmacdonald deleted the rpenido/fal-3603-fix-import-tag-with-same-value branch January 10, 2024 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open-source-contribution PR author is not from Axim or 2U
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants