10
10
use Magento \Framework \App \ObjectManager ;
11
11
use Magento \Framework \App \ResourceConnection ;
12
12
use Magento \Framework \App \State as AppState ;
13
- use Magento \Framework \DB \Ddl \Trigger ;
13
+ use Magento \Framework \Console \Cli ;
14
+ use Magento \Framework \Exception \RuntimeException ;
14
15
use Magento \Framework \Mview \View \CollectionFactory as ViewCollectionFactory ;
15
16
use Magento \Framework \Mview \View \StateInterface ;
17
+ use Magento \Framework \Mview \View \SubscriptionFactory ;
18
+ use Magento \Framework \Mview \View \SubscriptionInterface ;
19
+ use Magento \Framework \Mview \ViewInterface ;
16
20
use Magento \Framework \Setup \ConsoleLogger ;
17
21
use Magento \Framework \Setup \Declaration \Schema \DryRunLogger ;
18
22
use Magento \Framework \Setup \Declaration \Schema \OperationsExecutor ;
@@ -56,30 +60,48 @@ class UpgradeCommand extends AbstractSetupCommand
56
60
*/
57
61
private $ searchConfigFactory ;
58
62
59
- /*
63
+ /**
60
64
* @var ViewCollectionFactory
61
65
*/
62
66
private $ viewCollectionFactory ;
63
67
68
+ /**
69
+ * @var SubscriptionFactory
70
+ */
71
+ private $ subscriptionFactory ;
72
+
73
+ /**
74
+ * @var ResourceConnection
75
+ */
76
+ private $ resource ;
77
+
64
78
/**
65
79
* @param InstallerFactory $installerFactory
66
80
* @param SearchConfigFactory $searchConfigFactory
67
81
* @param DeploymentConfig $deploymentConfig
68
82
* @param AppState|null $appState
69
83
* @param ViewCollectionFactory|null $viewCollectionFactory
84
+ * @param SubscriptionFactory|null $subscriptionFactory
85
+ * @param ResourceConnection|null $resource
70
86
*/
71
87
public function __construct (
72
88
InstallerFactory $ installerFactory ,
73
89
SearchConfigFactory $ searchConfigFactory ,
74
90
DeploymentConfig $ deploymentConfig = null ,
75
91
AppState $ appState = null ,
76
- ViewCollectionFactory $ viewCollectionFactory = null
92
+ ViewCollectionFactory $ viewCollectionFactory = null ,
93
+ SubscriptionFactory $ subscriptionFactory = null ,
94
+ ResourceConnection $ resource = null
77
95
) {
78
96
$ this ->installerFactory = $ installerFactory ;
79
97
$ this ->searchConfigFactory = $ searchConfigFactory ;
80
98
$ this ->deploymentConfig = $ deploymentConfig ?: ObjectManager::getInstance ()->get (DeploymentConfig::class);
81
99
$ this ->appState = $ appState ?: ObjectManager::getInstance ()->get (AppState::class);
82
- $ this ->viewCollectionFactory = $ viewCollectionFactory ?: ObjectManager::getInstance ()->get (ViewCollectionFactory::class);
100
+ $ this ->viewCollectionFactory = $ viewCollectionFactory
101
+ ?: ObjectManager::getInstance ()->get (ViewCollectionFactory::class);
102
+ $ this ->subscriptionFactory = $ subscriptionFactory
103
+ ?: ObjectManager::getInstance ()->get (SubscriptionFactory::class);
104
+ $ this ->resource = $ resource ?: ObjectManager::getInstance ()->get (ResourceConnection::class);
83
105
parent ::__construct ();
84
106
}
85
107
@@ -142,9 +164,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
142
164
$ installer ->updateModulesSequence ($ keepGenerated );
143
165
$ searchConfig = $ this ->searchConfigFactory ->create ();
144
166
$ searchConfig ->validateSearchEngine ();
145
-
146
167
$ this ->removeUnusedTriggers ();
147
-
148
168
$ installer ->installSchema ($ request );
149
169
$ installer ->installDataFixtures ($ request );
150
170
@@ -153,8 +173,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
153
173
$ arrayInput = new ArrayInput ([]);
154
174
$ arrayInput ->setInteractive ($ input ->isInteractive ());
155
175
$ result = $ importConfigCommand ->run ($ arrayInput , $ output );
156
- if ($ result === \ Magento \ Framework \ Console \ Cli::RETURN_FAILURE ) {
157
- throw new \ Magento \ Framework \ Exception \ RuntimeException (
176
+ if ($ result === Cli::RETURN_FAILURE ) {
177
+ throw new RuntimeException (
158
178
__ ('%1 failed. See previous output. ' , ConfigImportCommand::COMMAND_NAME )
159
179
);
160
180
}
@@ -167,49 +187,70 @@ protected function execute(InputInterface $input, OutputInterface $output)
167
187
}
168
188
} catch (\Exception $ e ) {
169
189
$ output ->writeln ('<error> ' . $ e ->getMessage () . '</error> ' );
170
- return \ Magento \ Framework \ Console \ Cli::RETURN_FAILURE ;
190
+ return Cli::RETURN_FAILURE ;
171
191
}
172
192
173
- return \ Magento \ Framework \ Console \ Cli::RETURN_SUCCESS ;
193
+ return Cli::RETURN_SUCCESS ;
174
194
}
175
195
176
196
/**
177
197
* Remove unused triggers
178
198
*/
179
199
private function removeUnusedTriggers ()
180
200
{
181
- // unsubscribe mview
182
201
$ viewCollection = $ this ->viewCollectionFactory ->create ();
183
202
$ viewList = $ viewCollection ->getViewsByStateMode (StateInterface::MODE_ENABLED );
203
+
204
+ // Unsubscribe mviews
184
205
foreach ($ viewList as $ view ) {
185
- /** @var \Magento\Framework\Mview\ ViewInterface $view */
206
+ /** @var ViewInterface $view */
186
207
$ view ->unsubscribe ();
187
208
}
188
209
189
- // remove extra triggers that have correct naming structure
190
- /** @var ResourceConnection $resource */
191
- $ resource = ObjectManager::getInstance ()->get (ResourceConnection::class);
192
- $ connection = $ resource ->getConnection ();
193
- $ triggers = $ connection ->getTriggers ();
210
+ // Remove extra triggers that have correct naming structure
211
+ $ triggers = $ this ->getTriggers ();
194
212
foreach ($ triggers as $ trigger ) {
195
- $ triggerNames = [];
196
- foreach (Trigger::getListOfEvents () as $ event ) {
197
- $ triggerName = $ resource ->getTriggerName (
198
- $ resource ->getTableName ($ trigger ['Table ' ]),
199
- Trigger::TIME_AFTER ,
200
- $ event
201
- );
202
- $ triggerNames [] = strtolower ($ triggerName );
203
- }
204
- if (in_array ($ trigger ['Trigger ' ], $ triggerNames )) {
205
- $ connection ->dropTrigger ($ trigger ['Trigger ' ]);
206
- }
213
+ $ this ->initSubscriptionInstance ($ trigger ['Table ' ])->remove ();
207
214
}
208
215
209
- // subscribe mview
216
+ // Subscribe mviews
210
217
foreach ($ viewList as $ view ) {
211
- /** @var \Magento\Framework\Mview\ ViewInterface $view */
218
+ /** @var ViewInterface $view */
212
219
$ view ->subscribe ();
213
220
}
214
221
}
222
+
223
+ /**
224
+ * Retrieve triggers list
225
+ *
226
+ * @return array
227
+ */
228
+ private function getTriggers (): array
229
+ {
230
+ $ connection = $ this ->resource ->getConnection ();
231
+ $ result = $ connection ->query ('SHOW TRIGGERS ' );
232
+ return $ result ->fetchAll ();
233
+ }
234
+
235
+ /**
236
+ * Initializes subscription instance
237
+ *
238
+ * @param string $tablename
239
+ * @return SubscriptionInterface
240
+ */
241
+ private function initSubscriptionInstance (string $ tablename ): SubscriptionInterface
242
+ {
243
+ /** @var ViewInterface $view */
244
+ $ view = ObjectManager::getInstance ()->create (ViewInterface::class);
245
+ $ view ->setId ('0 ' );
246
+
247
+ return $ this ->subscriptionFactory ->create (
248
+ [
249
+ 'view ' => $ view ,
250
+ 'tableName ' => $ tablename ,
251
+ 'columnName ' => '' ,
252
+ 'subscriptionModel ' => SubscriptionFactory::INSTANCE_NAME ,
253
+ ]
254
+ );
255
+ }
215
256
}
0 commit comments