Skip to content

Commit

Permalink
ENGCOM-8825: Restore UNIQUE_CHECKS mysql variable to its original val…
Browse files Browse the repository at this point in the history
…ue when done … #32286
  • Loading branch information
gabrieldagama committed Aug 11, 2021
2 parents ff42eed + 0931e3a commit f3a8391
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<description value="Print Order from Guest on Frontend"/>
<severity value="BLOCKER"/>
<testCaseId value="MC-38689"/>
<skip>
<issueId value="MQE-2834" />
</skip>
<group value="remote_storage_aws_s3"/>
</annotations>
<before>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<description value="To be sure that other elements of Success page are shown for placed order as registered Customer."/>
<severity value="CRITICAL"/>
<testCaseId value="MC-16488"/>
<skip>
<issueId value="MQE-2834" />
</skip>
<group value="checkout"/>
</annotations>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\Message\MessageInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;

Expand Down Expand Up @@ -53,7 +52,6 @@ public function execute(Quote $cart, array $cartItems): void
foreach ($cartItems as $cartItemData) {
$this->addProductToCart->execute($cart, $cartItemData);
}

$this->cartRepository->save($cart);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\QuoteGraphQl\Model\Cart\AddProductsToCart;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Framework\Lock\LockManagerInterface;

/**
* Add simple products to cart GraphQl resolver
Expand All @@ -30,16 +31,24 @@ class AddSimpleProductsToCart implements ResolverInterface
*/
private $addProductsToCart;

/**
* @var LockManagerInterface
*/
private $lockManager;

/**
* @param GetCartForUser $getCartForUser
* @param AddProductsToCart $addProductsToCart
* @param LockManagerInterface $lockManager
*/
public function __construct(
GetCartForUser $getCartForUser,
AddProductsToCart $addProductsToCart
AddProductsToCart $addProductsToCart,
LockManagerInterface $lockManager
) {
$this->getCartForUser = $getCartForUser;
$this->addProductsToCart = $addProductsToCart;
$this->lockManager = $lockManager;
}

/**
Expand All @@ -58,12 +67,20 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
throw new GraphQlInputException(__('Required parameter "cart_items" is missing'));
}
$cartItems = $args['input']['cart_items'];

$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();

$lockName = 'cart_processing_lock_' . $maskedCartId;
while ($this->lockManager->isLocked($lockName)) {
// wait till other process working with the same cart complete
usleep(rand(100, 600));
}
$this->lockManager->lock($lockName, 1);

$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
$this->addProductsToCart->execute($cart, $cartItems);

$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);

$this->lockManager->unlock($lockName);
return [
'cart' => [
'model' => $cart,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<description value="Print Order from Guest on Frontend"/>
<severity value="CRITICAL"/>
<testCaseId value="MC-28494"/>
<skip>
<issueId value="MQE-2834" />
</skip>
<group value="sales"/>
<group value="mtf_migrated"/>
</annotations>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<description value="Check while order printing URL with an id of not relevant order redirects to order history"/>
<severity value="MAJOR"/>
<testCaseId value="MC-28543"/>
<skip>
<issueId value="MQE-2834" />
</skip>
<group value="sales"/>
</annotations>
<before>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ public function getDestructiveOperations()
private function startSetupForAllConnections()
{
foreach ($this->sharding->getResources() as $resource) {
$this->resourceConnection->getConnection($resource)
->startSetup();
$this->resourceConnection->getConnection($resource)
->query('SET UNIQUE_CHECKS=0');
$connection = $this->resourceConnection->getConnection($resource);

$connection->startSetup();
$connection->query('SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0');
}
}

Expand All @@ -148,8 +148,10 @@ private function startSetupForAllConnections()
private function endSetupForAllConnections()
{
foreach ($this->sharding->getResources() as $resource) {
$this->resourceConnection->getConnection($resource)
->endSetup();
$connection = $this->resourceConnection->getConnection($resource);

$connection->query('SET UNIQUE_CHECKS=IF(@OLD_UNIQUE_CHECKS=0, 0, 1)');
$connection->endSetup();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function testExecute()
$connectionMock = $this->getMockBuilder(Mysql::class)
->disableOriginalConstructor()
->getMock();
$this->resourceConnectionMock->expects(self::exactly(3))
$this->resourceConnectionMock->expects(self::exactly(2))
->method('getConnection')
->with('default')
->willReturn($connectionMock);
Expand Down

0 comments on commit f3a8391

Please sign in to comment.