9
9
use Magento \Catalog \Model \Product ;
10
10
use Magento \Eav \Setup \EavSetup ;
11
11
use Magento \Eav \Setup \EavSetupFactory ;
12
- use Magento \Framework \Exception \ LocalizedException ;
12
+ use Magento \Framework \DB \ Adapter \ AdapterInterface ;
13
13
use Magento \Framework \Setup \ModuleDataSetupInterface ;
14
14
use Magento \Framework \Setup \Patch \DataPatchInterface ;
15
+ use Magento \Framework \Setup \Patch \NonTransactionableInterface ;
15
16
16
- class UpdateMultiselectAttributesBackendTypes implements DataPatchInterface
17
+ class UpdateMultiselectAttributesBackendTypes implements DataPatchInterface, NonTransactionableInterface
17
18
{
18
19
/**
19
20
* @var ModuleDataSetupInterface
@@ -24,6 +25,11 @@ class UpdateMultiselectAttributesBackendTypes implements DataPatchInterface
24
25
*/
25
26
private $ eavSetupFactory ;
26
27
28
+ /**
29
+ * @var array
30
+ */
31
+ private $ triggersRestoreQueries = [];
32
+
27
33
/**
28
34
* MigrateMultiselectAttributesData constructor.
29
35
* @param ModuleDataSetupInterface $dataSetup
@@ -61,6 +67,7 @@ public function apply()
61
67
$ this ->dataSetup ->startSetup ();
62
68
$ setup = $ this ->dataSetup ;
63
69
$ connection = $ setup ->getConnection ();
70
+ $ this ->triggersRestoreQueries = [];
64
71
65
72
$ attributeTable = $ setup ->getTable ('eav_attribute ' );
66
73
/** @var EavSetup $eavSetup */
@@ -74,26 +81,31 @@ public function apply()
74
81
->where ('backend_type = ? ' , 'varchar ' )
75
82
->where ('frontend_input = ? ' , 'multiselect ' )
76
83
);
84
+ $ attributesToMigrate = array_map ('intval ' , $ attributesToMigrate );
77
85
78
86
$ varcharTable = $ setup ->getTable ('catalog_product_entity_varchar ' );
79
87
$ textTable = $ setup ->getTable ('catalog_product_entity_text ' );
80
- $ varcharTableDataSql = $ connection
81
- ->select ()
82
- ->from ($ varcharTable )
83
- ->where ('attribute_id in (?) ' , $ attributesToMigrate );
84
88
85
89
$ columns = $ connection ->describeTable ($ varcharTable );
86
90
unset($ columns ['value_id ' ]);
87
- $ connection ->query (
88
- $ connection ->insertFromSelect (
89
- $ connection ->select ()
90
- ->from ($ varcharTable , array_keys ($ columns ))
91
- ->where ('attribute_id in (?) ' , $ attributesToMigrate ),
92
- $ textTable ,
93
- array_keys ($ columns )
94
- )
95
- );
96
- $ connection ->query ($ connection ->deleteFromSelect ($ varcharTableDataSql , $ varcharTable ));
91
+ $ this ->dropTriggers ($ textTable );
92
+ $ this ->dropTriggers ($ varcharTable );
93
+ try {
94
+ $ connection ->query (
95
+ $ connection ->insertFromSelect (
96
+ $ connection ->select ()
97
+ ->from ($ varcharTable , array_keys ($ columns ))
98
+ ->where ('attribute_id in (?) ' , $ attributesToMigrate , \Zend_Db::INT_TYPE ),
99
+ $ textTable ,
100
+ array_keys ($ columns ),
101
+ AdapterInterface::INSERT_ON_DUPLICATE
102
+ )
103
+ );
104
+ $ connection ->delete ($ varcharTable , ['attribute_id IN (?) ' => $ attributesToMigrate ]);
105
+ } finally {
106
+ $ this ->restoreTriggers ($ textTable );
107
+ $ this ->restoreTriggers ($ varcharTable );
108
+ }
97
109
98
110
foreach ($ attributesToMigrate as $ attributeId ) {
99
111
$ eavSetup ->updateAttribute ($ entityTypeId , $ attributeId , 'backend_type ' , 'text ' );
@@ -103,4 +115,48 @@ public function apply()
103
115
104
116
return $ this ;
105
117
}
118
+
119
+ /**
120
+ * Drop table triggers
121
+ *
122
+ * @param string $tableName
123
+ * @return void
124
+ * @throws \Zend_Db_Statement_Exception
125
+ */
126
+ private function dropTriggers (string $ tableName ): void
127
+ {
128
+ $ triggers = $ this ->dataSetup ->getConnection ()
129
+ ->query ('SHOW TRIGGERS LIKE \'' . $ tableName . '\'' )
130
+ ->fetchAll ();
131
+
132
+ if (!$ triggers ) {
133
+ return ;
134
+ }
135
+
136
+ foreach ($ triggers as $ trigger ) {
137
+ $ triggerData = $ this ->dataSetup ->getConnection ()
138
+ ->query ('SHOW CREATE TRIGGER ' . $ trigger ['Trigger ' ])
139
+ ->fetch ();
140
+ $ this ->triggersRestoreQueries [$ tableName ][] =
141
+ preg_replace ('/DEFINER=[^\s]*/ ' , '' , $ triggerData ['SQL Original Statement ' ]);
142
+ // phpcs:ignore Magento2.SQL.RawQuery.FoundRawSql
143
+ $ this ->dataSetup ->getConnection ()->query ('DROP TRIGGER IF EXISTS ' . $ trigger ['Trigger ' ]);
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Restore table triggers.
149
+ *
150
+ * @param string $tableName
151
+ * @return void
152
+ * @throws \Zend_Db_Statement_Exception
153
+ */
154
+ private function restoreTriggers (string $ tableName ): void
155
+ {
156
+ if (array_key_exists ($ tableName , $ this ->triggersRestoreQueries )) {
157
+ foreach ($ this ->triggersRestoreQueries [$ tableName ] as $ query ) {
158
+ $ this ->dataSetup ->getConnection ()->multiQuery ($ query );
159
+ }
160
+ }
161
+ }
106
162
}
0 commit comments