Skip to content

Commit 23648e4

Browse files
authored
Fix issue magento#31396
1 parent 50c513f commit 23648e4

File tree

1 file changed

+67
-45
lines changed

1 file changed

+67
-45
lines changed

lib/internal/Magento/Framework/Setup/Patch/PatchApplier.php

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,15 @@ public function applyDataPatch($moduleName = null)
153153
new Phrase("Patch %1 should implement DataPatchInterface", [get_class($dataPatch)])
154154
);
155155
}
156+
if ($this->isApplied($dataPatch)) {
157+
continue;
158+
}
156159
if ($dataPatch instanceof NonTransactionableInterface) {
157-
$dataPatch->apply();
158-
$this->patchHistory->fixPatch(get_class($dataPatch));
160+
$this->applyPatch($dataPatch);
159161
} else {
160162
try {
161163
$this->moduleDataSetup->getConnection()->beginTransaction();
162-
$dataPatch->apply();
163-
$this->patchHistory->fixPatch(get_class($dataPatch));
164-
foreach ($dataPatch->getAliases() as $patchAlias) {
165-
if (!$this->patchHistory->isApplied($patchAlias)) {
166-
$this->patchHistory->fixPatch($patchAlias);
167-
}
168-
}
164+
$this->applyPatch($dataPatch);
169165
$this->moduleDataSetup->getConnection()->commit();
170166
} catch (\Exception $e) {
171167
$this->moduleDataSetup->getConnection()->rollBack();
@@ -187,35 +183,6 @@ public function applyDataPatch($moduleName = null)
187183
}
188184
}
189185

190-
/**
191-
* Register all patches in registry in order to manipulate chains and dependencies of patches of patches
192-
*
193-
* @param string $moduleName
194-
* @param string $patchType
195-
* @return PatchRegistry
196-
*/
197-
private function prepareRegistry($moduleName, $patchType)
198-
{
199-
$reader = $patchType === self::DATA_PATCH ? $this->dataPatchReader : $this->schemaPatchReader;
200-
$registry = $this->patchRegistryFactory->create();
201-
202-
//Prepare modules to read
203-
if ($moduleName === null) {
204-
$patchNames = [];
205-
foreach ($this->moduleList->getNames() as $moduleName) {
206-
$patchNames += $reader->read($moduleName);
207-
}
208-
} else {
209-
$patchNames = $reader->read($moduleName);
210-
}
211-
212-
foreach ($patchNames as $patchName) {
213-
$registry->registerPatch($patchName);
214-
}
215-
216-
return $registry;
217-
}
218-
219186
/**
220187
* Apply all patches for one module
221188
*
@@ -240,12 +207,8 @@ public function applySchemaPatch($moduleName = null)
240207
* @var SchemaPatchInterface $schemaPatch
241208
*/
242209
$schemaPatch = $this->patchFactory->create($schemaPatch, ['schemaSetup' => $this->schemaSetup]);
243-
$schemaPatch->apply();
244-
$this->patchHistory->fixPatch(get_class($schemaPatch));
245-
foreach ($schemaPatch->getAliases() as $patchAlias) {
246-
if (!$this->patchHistory->isApplied($patchAlias)) {
247-
$this->patchHistory->fixPatch($patchAlias);
248-
}
210+
if (!$this->isApplied($schemaPatch)) {
211+
$this->applyPatch($schemaPatch);
249212
}
250213
} catch (\Exception $e) {
251214
$schemaPatchClass = is_object($schemaPatch) ? get_class($schemaPatch) : $schemaPatch;
@@ -281,7 +244,7 @@ public function revertDataPatches($moduleName = null)
281244
'\\' . $dataPatch,
282245
['moduleDataSetup' => $this->moduleDataSetup]
283246
);
284-
if ($dataPatch instanceof PatchRevertableInterface) {
247+
if ($dataPatch instanceof PatchRevertableInterface && $this->isApplied($dataPatch)) {
285248
try {
286249
$adapter->beginTransaction();
287250
/** @var PatchRevertableInterface|DataPatchInterface $dataPatch */
@@ -297,4 +260,63 @@ public function revertDataPatches($moduleName = null)
297260
}
298261
}
299262
}
263+
264+
/**
265+
* Register all patches in registry in order to manipulate chains and dependencies of patches of patches
266+
*
267+
* @param string $moduleName
268+
* @param string $patchType
269+
* @return PatchRegistry
270+
*/
271+
private function prepareRegistry($moduleName, $patchType)
272+
{
273+
$reader = $patchType === self::DATA_PATCH ? $this->dataPatchReader : $this->schemaPatchReader;
274+
$registry = $this->patchRegistryFactory->create();
275+
276+
//Prepare modules to read
277+
if ($moduleName === null) {
278+
$patchNames = [];
279+
foreach ($this->moduleList->getNames() as $moduleName) {
280+
$patchNames += $reader->read($moduleName);
281+
}
282+
} else {
283+
$patchNames = $reader->read($moduleName);
284+
}
285+
286+
foreach ($patchNames as $patchName) {
287+
$registry->registerPatch($patchName);
288+
}
289+
290+
return $registry;
291+
}
292+
293+
/**
294+
* Apply the given patch. The patch is and its aliases are added to the history.
295+
*/
296+
private function applyPatch((PatchInterface $patch): void
297+
{
298+
$patch->apply();
299+
$this->patchHistory->fixPatch(get_class($schemaPatch));
300+
foreach ($schemaPatch->getAliases() as $patchAlias) {
301+
if (!$this->patchHistory->isApplied($patchAlias)) {
302+
$this->patchHistory->fixPatch($patchAlias);
303+
}
304+
}
305+
}
306+
307+
/**
308+
* Check wether the given patch or any of its aliases are already applied or not.
309+
*/
310+
private function isApplied(PatchInterface $patch): bool
311+
{
312+
if (!$this->patchHistory->isApplied(get_class($patch))) {
313+
foreach ($patch->getAliases() as $alias) {
314+
if ($this->patchHistory->isApplied($alias)) {
315+
return true;
316+
}
317+
}
318+
}
319+
320+
return false;
321+
}
300322
}

0 commit comments

Comments
 (0)