Fix stale EAV attribute code in setup cache after rename (#38636)#40980
Open
lbajsarowicz wants to merge 3 commits into
Open
Fix stale EAV attribute code in setup cache after rename (#38636)#40980lbajsarowicz wants to merge 3 commits into
lbajsarowicz wants to merge 3 commits into
Conversation
EavSetup::getAttribute() caches attribute rows under both attribute_id and attribute_code keys, but updateTableRow() refreshes only the id-keyed entry. After updateAttribute() changed an attribute_code, the stale code-keyed entry survived, so a subsequent addAttribute() with the original code updated the renamed attribute instead of inserting a new one. Invalidate the code-keyed cache entries in _updateAttribute(). Fixes magento#38636
|
Hi @lbajsarowicz. Thank you for your contribution!
Allowed build names are:
You can find more information about the builds here For more details, review the Code Contributions documentation. |
Contributor
Author
|
@magento run all tests |
Contributor
Author
|
@magento run all tests |
Contributor
Author
|
@magento run all tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description (*)
Magento\Eav\Setup\EavSetup::getAttribute()populates the setup cache for eachattribute under two keys — its
attribute_idand itsattribute_code:Magento\Setup\Module\DataSetup::updateTableRow()— used byEavSetup::_updateAttribute()— refreshes only theattribute_id-keyed cacheentry after an
UPDATE. Theattribute_code-keyed entry is left untouched.As a result, when an attribute is renamed and then re-added with the original
code in the same request:
addAttribute('attribute')callsgetAttribute($entityType, 'attribute', 'attribute_id'),which hits the stale code-keyed cache entry still pointing at the original
attribute id.
addAttributetherefore treats it as an existing attribute andruns
updateAttribute()on it (renamingattribute_oldback toattribute)instead of inserting a new attribute — so nothing changes.
Fix
Invalidate the stale setup-cache entries in
_updateAttribute()after the rowis updated. The pre-update
attribute_codeis captured beforeupdateTableRow()(which overwrites the id-keyed cache row), then the id-keyed and code-keyed
entries are removed.
getAttribute()transparently re-reads from the database onthe next lookup.
Related Pull Requests
None
Fixed Issues (*)
Fixes #38636
Manual testing scenarios (*)
Using a single setup/data-patch instance:
addAttribute($entityType, 'attribute', [...]).updateAttribute($entityType, 'attribute', ['attribute_code' => 'attribute_old']).addAttribute($entityType, 'attribute', [...]).attribute—
attribute_oldno longer exists and no new attribute is created.attribute, andattribute_oldretains its renamed code — both attributes exist as distinct records.
Contribution checklist (*)
added in
EavSetupTest)