6
6
declare (strict_types=1 );
7
7
namespace Magento \Wishlist \Setup \Patch \Data ;
8
8
9
+ use Magento \Framework \DB \Select \QueryModifierFactory ;
9
10
use Magento \Framework \Serialize \Serializer \Json ;
10
11
use Magento \Framework \Setup \ModuleDataSetupInterface ;
11
12
use Magento \Framework \Setup \Patch \DataPatchInterface ;
13
+ use Magento \Framework \App \ObjectManager ;
14
+ use Magento \Framework \DB \Query \Generator ;
12
15
13
16
/**
14
17
* Class Clean Up Data Removes unused data
15
18
*/
16
19
class CleanUpData implements DataPatchInterface
17
20
{
21
+ /**
22
+ * Batch size for query
23
+ */
24
+ private const BATCH_SIZE = 1000 ;
25
+
18
26
/**
19
27
* @var ModuleDataSetupInterface
20
28
*/
21
29
private $ moduleDataSetup ;
22
30
31
+ /**
32
+ * @var Generator
33
+ */
34
+ private $ queryGenerator ;
35
+
23
36
/**
24
37
* @var Json
25
38
*/
26
39
private $ json ;
40
+ /**
41
+ * @var QueryModifierFactory
42
+ */
43
+ private $ queryModifierFactory ;
27
44
28
45
/**
29
46
* RemoveData constructor.
30
47
* @param ModuleDataSetupInterface $moduleDataSetup
31
- * @param Json $json
48
+ * @param Json|null $json
49
+ * @param Generator|null $queryGenerator
50
+ * @param QueryModifierFactory|null $queryModifierFactory
32
51
*/
33
52
public function __construct (
34
53
ModuleDataSetupInterface $ moduleDataSetup ,
35
- Json $ json
54
+ Json $ json = null ,
55
+ Generator $ queryGenerator = null ,
56
+ QueryModifierFactory $ queryModifierFactory = null
36
57
) {
37
58
$ 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);
39
62
}
40
63
41
64
/**
@@ -45,16 +68,25 @@ public function apply()
45
68
{
46
69
$ this ->moduleDataSetup ->getConnection ()->startSetup ();
47
70
$ 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 (
54
74
$ wishListItemOptionTable ,
55
- ['value ' => $ rowValue ],
56
- ['option_id = ? ' => $ optionRow ['option_id ' ]]
75
+ ['option_id ' , 'value ' ]
57
76
);
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
+ }
58
90
}
59
91
$ this ->moduleDataSetup ->getConnection ()->endSetup ();
60
92
0 commit comments