Skip to content

[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
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions lib/internal/Magento/Framework/Setup/Patch/PatchApplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -296,4 +292,38 @@ public function revertDataPatches($moduleName = null)
}
}
}

/**
* Checks if patch should be applied by already applied patch alias names
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Checks if patch should be applied by already applied patch alias names
* Check 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;
Copy link
Member

Choose a reason for hiding this comment

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

variable assigning is not necessary, you can use return directly

}
}
}
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Copy link
Member

Choose a reason for hiding this comment

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

this can be removed, or not?

Suggested change
use phpDocumentor\Reflection\Types\True_;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -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(
[
Expand Down Expand Up @@ -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']);
Expand Down