-
Notifications
You must be signed in to change notification settings - Fork 9.4k
[Fixed: #31396] Data/Schema Patches getAliases() not working as expected #37682
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
Closed
Closed
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f8bed9b
Fixed issue with Data/Schema patch applying
Usik2203 015ac91
minor fix
Usik2203 05aba68
Merge branch '2.4-develop' into data-patch-fix
Usik2203 fbca506
Merge branch '2.4-develop' into data-patch-fix
Usik2203 d88bd43
Fixed failed tests
Usik2203 13e4a2d
Fixed failed tests
Usik2203 391d60e
Merge branch '2.4-develop' into data-patch-fix
Usik2203 3cf9514
Merge branch 'data-patch-fix' of github.com:Usik2203/magento2 into da…
MatthijsBreed 31f3498
Update to Usik2203's PR branch, account for applied patch aliases, up…
MatthijsBreed 6881afe
Fix testcases
MatthijsBreed 9a69f52
Merge branch 'magento:2.4-develop' into data-patch-fix
MatthijsBreed 8ddedc8
Applied feedback, fixed unit tests
MatthijsBreed 76e06f1
Fix 1 more test
MatthijsBreed d8441d4
Merge branch '2.4-develop' into data-patch-fix
engcom-Hotel 022190b
[FEEDBACK] Early return to prevent unneeded iterations
MatthijsBreed 3f43bcf
Merge branch '2.4-develop' into data-patch-fix
engcom-Hotel a13676e
Merge branch '2.4-develop' into data-patch-fix
engcom-Lima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,13 +159,11 @@ public function applyDataPatch($moduleName = null) | |
} else { | ||
try { | ||
$this->moduleDataSetup->getConnection()->beginTransaction(); | ||
$dataPatch->apply(); | ||
$this->patchHistory->fixPatch(get_class($dataPatch)); | ||
foreach ($dataPatch->getAliases() as $patchAlias) { | ||
if (!$this->patchHistory->isApplied($patchAlias)) { | ||
$this->patchHistory->fixPatch($patchAlias); | ||
} | ||
if ($this->shouldApply($dataPatch)) { | ||
$dataPatch->apply(); | ||
$this->patchHistory->fixPatch(get_class($dataPatch)); | ||
} | ||
$this->fixAliases($dataPatch); | ||
$this->moduleDataSetup->getConnection()->commit(); | ||
} catch (\Exception $e) { | ||
$this->moduleDataSetup->getConnection()->rollBack(); | ||
|
@@ -240,13 +238,11 @@ public function applySchemaPatch($moduleName = null) | |
* @var SchemaPatchInterface $schemaPatch | ||
*/ | ||
$schemaPatch = $this->patchFactory->create($schemaPatch, ['schemaSetup' => $this->schemaSetup]); | ||
$schemaPatch->apply(); | ||
$this->patchHistory->fixPatch(get_class($schemaPatch)); | ||
foreach ($schemaPatch->getAliases() as $patchAlias) { | ||
if (!$this->patchHistory->isApplied($patchAlias)) { | ||
$this->patchHistory->fixPatch($patchAlias); | ||
} | ||
if ($this->shouldApply($schemaPatch)) { | ||
$schemaPatch->apply(); | ||
$this->patchHistory->fixPatch(get_class($schemaPatch)); | ||
} | ||
$this->fixAliases($schemaPatch); | ||
} catch (\Exception $e) { | ||
throw new SetupException( | ||
new Phrase( | ||
|
@@ -296,4 +292,38 @@ public function revertDataPatches($moduleName = null) | |
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks if patch should be applied by already applied patch alias names | ||
* | ||
* @param DataPatchInterface|SchemaPatchInterface $patch | ||
* @return bool | ||
*/ | ||
private function shouldApply(DataPatchInterface|SchemaPatchInterface$patch): bool | ||
{ | ||
$shouldApply = true; | ||
if ($patch = $patch->getAliases()) { | ||
foreach ($patch as $patchAlias) { | ||
if ($this->patchHistory->isApplied($patchAlias)) { | ||
$shouldApply = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. variable assigning is not necessary, you can use |
||
} | ||
} | ||
} | ||
return $shouldApply; | ||
} | ||
|
||
/** | ||
* Save all patch aliases | ||
* | ||
* @param DataPatchInterface|SchemaPatchInterface $patch | ||
* @return void | ||
*/ | ||
private function fixAliases(DataPatchInterface|SchemaPatchInterface $patch): void | ||
{ | ||
foreach ($patch->getAliases() as $patchAlias) { | ||
if (!$this->patchHistory->isApplied($patchAlias)) { | ||
$this->patchHistory->fixPatch($patchAlias); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -24,6 +24,7 @@ | |||
use Magento\Framework\Setup\SchemaSetupInterface; | ||||
use Magento\Framework\Setup\SetupInterface; | ||||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||||
use phpDocumentor\Reflection\Types\True_; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be removed, or not?
Suggested change
|
||||
use PHPUnit\Framework\MockObject\MockObject; | ||||
use PHPUnit\Framework\TestCase; | ||||
|
||||
|
@@ -179,7 +180,7 @@ public function testApplyDataPatchForNewlyInstalledModule($moduleName, $dataPatc | |||
// phpstan:ignore "Class OtherDataPatch not found." | ||||
$patch2 = $this->createMock(\OtherDataPatch::class); | ||||
$patch2->expects($this->once())->method('apply'); | ||||
$patch2->expects($this->once())->method('getAliases')->willReturn([]); | ||||
$patch2->expects($this->exactly(2))->method('getAliases')->willReturn([]); | ||||
|
||||
$this->objectManagerMock->expects($this->any())->method('create')->willReturnMap( | ||||
[ | ||||
|
@@ -222,6 +223,7 @@ public function testApplyDataPatchForAlias($moduleName, $dataPatches, $moduleVer | |||
|
||||
$patch1 = $this->getMockForAbstractClass(DataPatchInterface::class); | ||||
$patch1->expects($this->once())->method('getAliases')->willReturn(['PatchAlias']); | ||||
$patch1->expects($this->never())->method('apply'); | ||||
$patchClass = get_class($patch1); | ||||
|
||||
$patchRegistryMock = $this->createAggregateIteratorMock(PatchRegistry::class, [$patchClass], ['registerPatch']); | ||||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.