diff --git a/vendor/magento/module-staging/Model/Operation/Update/RescheduleUpdate.php b/vendor/magento/module-staging/Model/Operation/Update/RescheduleUpdate.php index 9eae52e5229..268a736d5b2 100644 --- a/vendor/magento/module-staging/Model/Operation/Update/RescheduleUpdate.php +++ b/vendor/magento/module-staging/Model/Operation/Update/RescheduleUpdate.php @@ -11,12 +11,16 @@ use Magento\Framework\EntityManager\EntityMetadataInterface; use Magento\Framework\EntityManager\HydratorPool; use Magento\Framework\EntityManager\MetadataPool; use Magento\Framework\EntityManager\TypeResolver; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Staging\Api\Data\UpdateInterface; use Magento\Staging\Api\UpdateRepositoryInterface; use Magento\Staging\Model\VersionInfo; use Magento\Staging\Model\VersionInfoFactory; use Magento\Staging\Model\VersionManager; +/** + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class RescheduleUpdate { /** @@ -278,6 +282,7 @@ class RescheduleUpdate * @param object $entity * @param UpdateInterface $origin * @return bool + * @throws \Zend_Db_Select_Exception */ private function purgeRollbackEntry($entityType, $entity, UpdateInterface $origin) { @@ -287,23 +292,42 @@ class RescheduleUpdate $identifier = $entityData[$metadata->getIdentifierField()]; $rollbackId = $origin->getRollbackId() ?: $entityData['updated_in']; $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName()); - $connection->update( - $metadata->getEntityTable(), - [ - 'updated_in' => $this->getNextForRollback($metadata, $rollbackId, $identifier), - ], - [ - $metadata->getIdentifierField() . ' = ?' => $identifier, - 'created_in = ?' => $this->getPreviousForRollback($metadata, $rollbackId, $identifier) - ] - ); - $connection->delete( - $metadata->getEntityTable(), - [ - $metadata->getIdentifierField() . ' = ?' => $identifier, - 'created_in = ?' => $rollbackId - ] - ); + try { + $rollbackInfo = $this->updateRepository->get($rollbackId); + } catch (NoSuchEntityException $exception) { + $rollbackInfo = false; + } + $select = $connection->select() + ->from( + ['t' => $metadata->getEntityTable()], + ['updated_in'] + ) + ->where('t.created_in = ?', $rollbackId) + ->where('t.' . $metadata->getIdentifierField() . ' = ?', $identifier) + ->order('t.created_in DESC') + ->limit(1) + ->setPart('disable_staging_preview', true); + $futureUpdate = $connection->fetchOne($select); + if (empty($futureUpdate) || $rollbackInfo === false || $rollbackInfo->getIsRollback()) { + $connection->update( + $metadata->getEntityTable(), + [ + 'updated_in' => $this->getNextForRollback($metadata, $rollbackId, $identifier), + ], + [ + $metadata->getIdentifierField() . ' = ?' => $identifier, + 'created_in = ?' => $this->getPreviousForRollback($metadata, $rollbackId, $identifier) + ] + ); + $connection->delete( + $metadata->getEntityTable(), + [ + $metadata->getIdentifierField() . ' = ?' => $identifier, + 'created_in = ?' => $rollbackId + ] + ); + } + return true; }