Repository navigation
Conversation
…l_id Closes openedx#858. Alternative to openedx#867 with a much smaller footprint. UpdateParentTag.applies_for compared the row's parent_id with the parent's pre-import external_id, so a row that kept the old id was "no change" and _validate_parent never ran for it. When the same file renamed that parent away (tag_1 -> tag_50), the stale reference was accepted silently. Now such a row applies as a parent change whenever its parent is renamed away (`_vacated_pks`), and _validate_parent rejects it with the existing "renamed to X; use X" error. _validate_parent also rejects a row whose parent_id is its own parent's pre-import id when another row takes that id (a swap): that reference is ambiguous, while a child naming the end-state id of a swapped parent is still accepted. A case-only rename no longer counts as vacating the id, since external_id is case-insensitive. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…trees Covers the remaining bugs openedx#867 found around parent_id handling, with one mechanism instead of a dry-run simulator and a conditional rebuild: - The plan computes the tree as it will be after the import (_end_state), rejects a parent cycle or a tree deeper than the limit there (Tag.save only validates the saved tag's depth, and silently corrupts lineage on a cycle), and records which existing tags change parent. - Rows are built after the row that creates or renames-in their parent, so a child may precede its new parent in the file. _validate_parent recognises a renamed-in parent from _vacated_pks, whatever the row order. - execute() detaches every moved tag before running the actions. After that each tag is at or above its final depth and none is an ancestor of its new parent, so no action can overflow or form a cycle part-way. - Kept children of deleted tags are promoted before the first delete, so the cascade cannot remove a kept grandchild. - A child whose deleted parent's id is reclaimed by a rename follows the renamed tag instead of being left at the root. Co-Authored-By: Claude Fable 5.1 <[email protected]>
The detach of every tag whose parent changes used to run as a hidden phase at the start of execute(). It is now a synthesized DetachTag action per tag, like StageTagExternalIdForSwap, built after staging and before the row actions, so the plan lists everything that runs. A root tag, a kept child of a deleted tag (already promoted) and a row with an unknown parent get no detach action. The tag is looked up by pk, since staging may have moved it onto a placeholder id. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
I had Opus do an analysis of this PR to compare it with #867 and it claims that it does cover the same ground with a small caveat and less testing. Claude drafted the following comment to share its analysis: To compare the two PRs, I ran #867's tests against this branch's code, because #867's tests map one-to-one to the acceptance criteria in #858, which Seva updated on 10-14 to cover all the extra fixes. I ran them on SQLite only. Coverage. Behaviorally, this PR covers nearly all of #858. Out of 199 tests, 156 pass, including the swap, row-order, delete-cascade, depth-limit, intermediate-step and case-variant scenarios. Most of the 43 failures are only wording differences: this PR keeps the existing "Parent tag X is renamed to Y in this file" message, and its depth error names the tag rather than using the ticket's quoted text. Both of those are fine with me. One mixed cycle (a rename row plus a new tag) gets the cycle error here instead of "Unknown parent tag", which #858 doesn't specify either way. Two gaps remain:
Whether the gaps matter. I don't think either one is a correctness problem. The row number is a small usability fix that I'd like to see in whichever PR we go with. The extra move-to-root lines are the price of the always-detach approach, and they're what lets this PR drop the in-memory simulation and the second plan build. I think that's a reasonable trade, and that the two ACs it breaks describe #867's mechanism more than an outcome an admin needs. If we go this way, I'd rather amend those two ACs in #858 than add the simulation back. The bigger difference between the PRs is test depth, not behavior. This PR adds about 360 lines of tests against about 1,900 in #867. Whichever implementation we keep, I'd suggest bringing over #867's |
| found = (self.tag.parent_id or "").casefold() in { | ||
| renamed_to.casefold() for renamed_to in vacated_pks.values() | ||
| } or self._search_action(indexed_actions, CreateTag.name, "id", self.tag.parent_id, casefold=True) | ||
| if found and not (parent_tag is not None and self._is_current_parent(parent_tag)): |
There was a problem hiding this comment.
Suggestion: these four lines took me a few reads. As far as I can tell, found means "some row of this file gives the id parent_id to a tag, by a rename onto it or by a create action built earlier", and line 131 means "accept that, unless the id currently belongs to this row's own parent (the ambiguous swap)". Naming the pieces would make both lines say that directly, something like:
renamed_onto_id = (self.tag.parent_id or "").casefold() in {
renamed_to.casefold() for renamed_to in vacated_pks.values()
}
created_with_id = self._search_action(indexed_actions, CreateTag.name, "id", self.tag.parent_id, casefold=True)
names_own_parents_old_id = parent_tag is not None and self._is_current_parent(parent_tag)
if (renamed_onto_id or created_with_id) and not names_own_parents_old_id:
return None| ).format(parent_id=self.tag.parent_id), | ||
| ) | ||
|
|
||
| def _is_current_parent(self, parent_tag: Tag) -> bool: |
There was a problem hiding this comment.
Nit: I found the name and docstring of _is_current_parent hard to follow, mostly because "this row's tag" could be a tag that doesn't exist yet. If I read it right, it answers "before the import, was parent_tag the parent of the tag this row describes?", looking that tag up by previous_id when the row renames it and by id otherwise. Something like _was_parent_before_import, with that lookup rule in the docstring, would be clearer to me.
| """ | ||
| This action applies whenever there is a change on the parent. | ||
|
|
||
| A row that names its parent's current id is still a parent change when |
There was a problem hiding this comment.
Nit: I had trouble telling whether "current id" meant the id before or after the import. In this PR "current" always means before the import, but the new docstrings and comments use several terms for the same two ideas: "current id" here, "currently holding" (line 110), "own current parent" (line 115) and "pre-import id" (line 116) for the id before the import, and "final id" and "end state" in import_plan.py for the id after it. Could we settle on "pre-import id" and "final id" throughout?
| Such a row used to count as "no change", so a stale id got through | ||
| `_validate_parent` and a reclaimed id lost the child to the delete | ||
| phase's promotion. |
There was a problem hiding this comment.
Nit: these lines describe how the code used to behave, which belongs in the commit history rather than the docstring. I'd delete them, or keep only the reason in present tense, for example "Otherwise a stale id would skip parent validation, and a child of a reclaimed id would be lost to the delete phase's promotion."
| taxonomy_tag.parent is not None | ||
| and taxonomy_tag.parent.external_id != tag.parent_id | ||
| ) or (taxonomy_tag.parent is None and tag.parent_id is not None) | ||
| parent = taxonomy_tag.parent |
There was a problem hiding this comment.
Nit: old_parent would make it clearer that this is the parent before the import, and it matches the old_parent label __str__ already uses on line 342.
| action = self._action() | ||
| tag = self.taxonomy.tag_set.get(external_id='tag_2') | ||
| self.assertIsNotNone(tag.parent) | ||
| self.taxonomy.tag_set.filter(pk=tag.pk).update(external_id="oel-import-staging:x") |
There was a problem hiding this comment.
Nit: on first read I thought this test claimed that changing the external_id removes the parent. I think the update() simulates a staging action that already moved the tag onto a placeholder id, to show that DetachTag finds the tag by pk. A comment like "Simulate a staging action that already ran" would make that clear.
| """ | ||
| tag_1 and tag_3 swap ids, and each of their children names the id its | ||
| parent will hold after the import: tag_2 (under tag_1, which becomes | ||
| tag_3) says parent_id=tag_3, and tag_4 (under tag_3, which becomes tag_1) | ||
| says parent_id=tag_1. The full-replace import succeeds, whether the | ||
| children's rows come before or after the renames, and each child stays on | ||
| the same physical parent. See test_import_plan.TestTagImportPlan | ||
| .test_generate_actions_swap_child_naming_own_parent_stale_id_rejected | ||
| for the children naming the pre-import ids instead. | ||
| """ |
There was a problem hiding this comment.
Nit: the test body already lays out the rows and the expected result, so I don't think this docstring adds much, and the dotted reference to the other test would go stale silently if that test were renamed. I'd delete the docstring, or cut it to one line.
| """ | ||
| tag_2 is already under tag_1. The file renames tag_1 to tag_50 while the | ||
| tag_2 row still says parent_id=tag_1. That row matches the parent's | ||
| pre-import id, so it used to count as "no change" and slipped past | ||
| _validate_parent, although tag_1 names nothing after the import. It must | ||
| be rejected, and the error must name the parent's new id. | ||
| """ |
There was a problem hiding this comment.
Nit: this docstring describes how the code used to behave ("it used to count as 'no change'"), and the test is clear without it. I'd delete it.
| """ | ||
| tag_2 is under tag_1, and the file swaps the ids of tag_1 and tag_3. After | ||
| the import "tag_1" names the old tag_3, so a tag_2 row that still says | ||
| parent_id=tag_1 could mean "stay with my parent" or "move under the tag | ||
| now called tag_1". It is rejected, once, and told to use tag_3. |
There was a problem hiding this comment.
Nit: the test is self-explanatory, so I don't think the docstring is needed.
| self.assertLess( | ||
| max(index for index, name in enumerate(names) if name == 'detach'), | ||
| min(index for index, name in enumerate(names) if name != 'detach'), | ||
| ) |
There was a problem hiding this comment.
Nit: it took me a moment to see that this asserts every detach action comes before every other action in the plan. A one-line comment would help, for example "All detaches run before any row action." This only holds here because these cases have no renames or deletes, which are built before the detaches, so the comment could say that too.
|
Closing in favor of #877 |
Preamble
@ufedaseyeuconsultant, @mgwozdz-unicon: I did this as an experiment, to see if a more capable model (Fable 5.1) could make something equivalent to #867 without as much code and complexity. I was particularly worried by
_ParentForestand whether we really needed that many tests to run through our edge cases. I have not actually looked through this code in detail—what follows is entirely Claude-generated. I mostly wanted to see how well it could do for a first pass. Please take a look at this and see if it seems like a reasonable approach to you. Thank you.Description
Alternative to #867 with the same fixes in about a third of the code. Closes #858.
Problem
UpdateParentTag.applies_forcompared a row'sparent_idwith the parent's pre-importexternal_id. A row that kept the old id was "no change", so_validate_parentnever ran for it, and a file that renamedtag_1totag_50while a child row still saidparent_id: tag_1was accepted silently (#858).Fixing that exposed the neighbouring problems #867 found: parent references only worked when the parent's row came first; a kept grandchild of a deleted chain was removed by the delete cascade; parent cycles and imports deeper than the limit passed the Plan step (
Tag.savevalidates only the saved tag's depth, cascades descendants unchecked, and silently corrupts lineage on a cycle); and some valid restructures failed part-way through because an intermediate step was too deep or cyclic.Approach
#867 searches for an action order that is valid at every intermediate step, which needs a dependency sort with subtree walks, an in-memory forest simulator, and a second plan build when the simulation fails. This PR always detaches first instead, so no simulation is needed:
TagImportPlan._end_statecomputes the tree as it will be after the import from one query._validate_hierarchyrejects a parent cycle (once, on its first row) and a tree deeper than the limit there.DetachTagaction (likeStageTagExternalIdForSwap) moves every existing tag whose parent changes to the root, after staging and before the row actions. After that every tag is at or above its final depth and none is an ancestor of its new parent, so the row actions can run in any order without exceeding the depth limit or building a cycle.For #858 itself,
UpdateParentTag.applies_fornow treats "same id as my current parent" as a parent change when this import gives that id to another tag, and_validate_parentrejects a row that names its own parent's pre-import id while a swap row takes that id, since it could mean "stay with my parent" or "move under the tag now called that". A child naming the end-state id of a swapped parent is accepted._validate_parentrecognises a renamed-in parent from_vacated_pks, so that check no longer depends on row order either.What changed
All in
src/openedx_tagging/import_export/:actions.py:_validate_parentrewritten without exception-as-control-flow, plus the own-parent check;UpdateParentTag.applies_foras above, and its description cached at plan time so the execution log doesn't read "from parent None" after the detach; newDetachTagaction; "Unknown parent" message no longer tells the admin to reorder the file.import_plan.py:_existing_tags,_end_state,_validate_hierarchy,_build_detach_actions,_order_rows_parent_first; kept children are promoted before the first delete; a case-only rename no longer counts as vacating its id.exceptions.py:ParentCycleError,TagTooDeepError.User-visible changes
Row N's parent_id (X) creates a parent cycle.andThis import would leave tag X more than 6 levels deep."Row N" is the N-th data row, as in the existingDuplicateFinalIdError.Parent tag tag_1 is renamed to tag_50 in this file; use tag_50 as the parent_id.Unknown parent tag (X). Add a row for the parent to your file.replaces the old "add parent before the child" wording.Move tag X to the root first, so that the parent changes in this file can run in any order.before the row actions. Withreplace=True, promotions of kept children now precede the deletes.Supporting information
Testing instructions
Automated:
pytest tests/openedx_tagging/import_export/ -o addopts=""(214 tests) andpytest tests/openedx_tagging -o addopts="". Quality:tox -e quality.Manual, through the taxonomy import endpoint or the Studio import wizard, on a taxonomy with
tag_1(root),tag_2undertag_1,tag_3(root),tag_4undertag_3:tagsrows)tag_50(previous_idtag_1),tag_2(parent_idtag_1),tag_3,tag_4(parent_idtag_3)Parent tag tag_1 is renamed to tag_50 in this file; use tag_50 as the parent_id.tag_3(previous_idtag_1),tag_1(previous_idtag_3),tag_2(parent_idtag_3),tag_4(parent_idtag_1), in any row ordertag_2undertag_3andtag_4undertag_1.tag_1(parent_idtag_2), plus the other three rows unchangedRow 1's parent_id (tag_2) creates a parent cycle.Other information
openedx-platformneeds to change.replace=Trueplan expectations (promotion before deletes, order only); two plans that move an existing tag gain the detach line; the "Unknown parent" wording; one_validate_parentunit test now sets_vacated_pksinstead of a built rename action.claude-fable-5-1).🤖 Generated with Claude Code