Skip to content

Commit e58fe94

Browse files
author
Hwashiang Yu
committed
MC-32145: Wishlist upgrade db update
- Added batching to patch
1 parent 5347b08 commit e58fe94

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

app/code/Magento/Wishlist/Setup/Patch/Data/CleanUpData.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,59 @@
66
declare(strict_types=1);
77
namespace Magento\Wishlist\Setup\Patch\Data;
88

9+
use Magento\Framework\DB\Select\QueryModifierFactory;
910
use Magento\Framework\Serialize\Serializer\Json;
1011
use Magento\Framework\Setup\ModuleDataSetupInterface;
1112
use Magento\Framework\Setup\Patch\DataPatchInterface;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Framework\DB\Query\Generator;
1215

1316
/**
1417
* Class Clean Up Data Removes unused data
1518
*/
1619
class CleanUpData implements DataPatchInterface
1720
{
21+
/**
22+
* Batch size for query
23+
*/
24+
private const BATCH_SIZE = 1000;
25+
1826
/**
1927
* @var ModuleDataSetupInterface
2028
*/
2129
private $moduleDataSetup;
2230

31+
/**
32+
* @var Generator
33+
*/
34+
private $queryGenerator;
35+
2336
/**
2437
* @var Json
2538
*/
2639
private $json;
40+
/**
41+
* @var QueryModifierFactory
42+
*/
43+
private $queryModifierFactory;
2744

2845
/**
2946
* RemoveData constructor.
3047
* @param ModuleDataSetupInterface $moduleDataSetup
31-
* @param Json $json
48+
* @param Json|null $json
49+
* @param Generator|null $queryGenerator
50+
* @param QueryModifierFactory|null $queryModifierFactory
3251
*/
3352
public function __construct(
3453
ModuleDataSetupInterface $moduleDataSetup,
35-
Json $json
54+
Json $json = null,
55+
Generator $queryGenerator = null,
56+
QueryModifierFactory $queryModifierFactory = null
3657
) {
3758
$this->moduleDataSetup = $moduleDataSetup;
38-
$this->json = $json;
59+
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
60+
$this->queryGenerator = $queryGenerator ?: ObjectManager::getInstance()->get(Generator::class);
61+
$this->queryModifierFactory = $queryModifierFactory ?: ObjectManager::getInstance()->get(QueryModifierFactory::class);
3962
}
4063

4164
/**
@@ -45,16 +68,25 @@ public function apply()
4568
{
4669
$this->moduleDataSetup->getConnection()->startSetup();
4770
$wishListItemOptionTable = $this->moduleDataSetup->getTable('wishlist_item_option');
48-
$select = $this->moduleDataSetup->getConnection()->select()->from($wishListItemOptionTable);
49-
foreach ($this->moduleDataSetup->getConnection()->fetchAll($select) as $optionRow) {
50-
$rowValue = $this->json->unserialize($optionRow['value']);
51-
unset($rowValue['login']);
52-
$rowValue = $this->json->serialize($rowValue);
53-
$this->moduleDataSetup->getConnection()->update(
71+
$select = $this->moduleDataSetup->getConnection()
72+
->select()
73+
->from(
5474
$wishListItemOptionTable,
55-
['value' => $rowValue],
56-
['option_id = ?' => $optionRow['option_id']]
75+
['option_id', 'value']
5776
);
77+
$iterator = $this->queryGenerator->generate('option_id', $select, self::BATCH_SIZE);
78+
foreach ($iterator as $key=>$selectByRange) {
79+
$optionRows = $this->moduleDataSetup->getConnection()->fetchAll($selectByRange);
80+
foreach ($optionRows as $optionRow) {
81+
$rowValue = $this->json->unserialize($optionRow['value']);
82+
unset($rowValue['login']);
83+
$rowValue = $this->json->serialize($rowValue);
84+
$this->moduleDataSetup->getConnection()->update(
85+
$wishListItemOptionTable,
86+
['value' => $rowValue],
87+
['option_id = ?' => $optionRow['option_id']]
88+
);
89+
}
5890
}
5991
$this->moduleDataSetup->getConnection()->endSetup();
6092

0 commit comments

Comments
 (0)