Skip to content

Commit cd1397d

Browse files
MC-32014: Remove google-shopping-ads module from core in 2.4.1
1 parent 45bd9f6 commit cd1397d

File tree

3 files changed

+133
-95
lines changed

3 files changed

+133
-95
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Mview;
7+
8+
use Magento\Framework\App\ResourceConnection;
9+
use Magento\Framework\Mview\View\CollectionFactory;
10+
use Magento\Framework\Mview\View\StateInterface;
11+
12+
/**
13+
* Class for removing old triggers that were created by mview
14+
*/
15+
class OldViews
16+
{
17+
/**
18+
* @var CollectionFactory
19+
*/
20+
private $viewCollectionFactory;
21+
22+
/**
23+
* @var ResourceConnection
24+
*/
25+
private $resource;
26+
27+
/**
28+
* @var ViewFactory
29+
*/
30+
private $viewFactory;
31+
32+
/**
33+
* @param CollectionFactory $viewCollectionFactory
34+
* @param ResourceConnection $resource
35+
* @param ViewFactory $viewFactory
36+
*/
37+
public function __construct(
38+
CollectionFactory $viewCollectionFactory,
39+
ResourceConnection $resource,
40+
ViewFactory $viewFactory
41+
) {
42+
$this->viewCollectionFactory = $viewCollectionFactory;
43+
$this->resource = $resource;
44+
$this->viewFactory = $viewFactory;
45+
}
46+
47+
/**
48+
* Remove unused triggers
49+
*/
50+
public function unsubscribe()
51+
{
52+
$viewCollection = $this->viewCollectionFactory->create();
53+
$viewList = $viewCollection->getViewsByStateMode(StateInterface::MODE_ENABLED);
54+
55+
// Unsubscribe mviews
56+
foreach ($viewList as $view) {
57+
/** @var ViewInterface $view */
58+
$view->unsubscribe();
59+
}
60+
61+
// Unsubscribe old views that still have triggers in db
62+
$triggerTableNames = $this->getTriggerTableNames();
63+
foreach ($triggerTableNames as $tableName) {
64+
$this->createViewByTableName($tableName)->unsubscribe();
65+
}
66+
67+
// Re-subscribe mviews
68+
foreach ($viewList as $view) {
69+
/** @var ViewInterface $view */
70+
$view->subscribe();
71+
}
72+
}
73+
74+
/**
75+
* Retrieve trigger table name list
76+
*
77+
* @return array
78+
*/
79+
private function getTriggerTableNames(): array
80+
{
81+
$connection = $this->resource->getConnection();
82+
$dbName = $this->resource->getSchemaName(ResourceConnection::DEFAULT_CONNECTION);
83+
$sql = $connection->select()
84+
->from(
85+
['information_schema.TRIGGERS'],
86+
['EVENT_OBJECT_TABLE']
87+
)
88+
->distinct(true)
89+
->where('TRIGGER_SCHEMA = ?', $dbName);
90+
return $connection->fetchCol($sql);
91+
}
92+
93+
/**
94+
* Create view by db table name
95+
*
96+
* @param string $tableName
97+
* @return ViewInterface
98+
*/
99+
private function createViewByTableName(string $tableName): ViewInterface
100+
{
101+
$subscription[$tableName] = [
102+
'name' => $tableName,
103+
'column' => '',
104+
'subscription_model' => null
105+
];
106+
$data['data'] = [
107+
'id' => '0',
108+
'subscriptions' => $subscription,
109+
];
110+
111+
$view = $this->viewFactory->create($data);
112+
$view->getState()->setMode(StateInterface::MODE_ENABLED);
113+
114+
return $view;
115+
}
116+
}

setup/src/Magento/Setup/Console/Command/UpgradeCommand.php

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@
88
use Magento\Deploy\Console\Command\App\ConfigImportCommand;
99
use Magento\Framework\App\DeploymentConfig;
1010
use Magento\Framework\App\ObjectManager;
11-
use Magento\Framework\App\ResourceConnection;
1211
use Magento\Framework\App\State as AppState;
1312
use Magento\Framework\Console\Cli;
1413
use Magento\Framework\Exception\RuntimeException;
15-
use Magento\Framework\Mview\View\CollectionFactory as ViewCollectionFactory;
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;
2014
use Magento\Framework\Setup\ConsoleLogger;
2115
use Magento\Framework\Setup\Declaration\Schema\DryRunLogger;
2216
use Magento\Framework\Setup\Declaration\Schema\OperationsExecutor;
@@ -60,48 +54,22 @@ class UpgradeCommand extends AbstractSetupCommand
6054
*/
6155
private $searchConfigFactory;
6256

63-
/**
64-
* @var ViewCollectionFactory
65-
*/
66-
private $viewCollectionFactory;
67-
68-
/**
69-
* @var SubscriptionFactory
70-
*/
71-
private $subscriptionFactory;
72-
73-
/**
74-
* @var ResourceConnection
75-
*/
76-
private $resource;
77-
7857
/**
7958
* @param InstallerFactory $installerFactory
8059
* @param SearchConfigFactory $searchConfigFactory
8160
* @param DeploymentConfig $deploymentConfig
8261
* @param AppState|null $appState
83-
* @param ViewCollectionFactory|null $viewCollectionFactory
84-
* @param SubscriptionFactory|null $subscriptionFactory
85-
* @param ResourceConnection|null $resource
8662
*/
8763
public function __construct(
8864
InstallerFactory $installerFactory,
8965
SearchConfigFactory $searchConfigFactory,
9066
DeploymentConfig $deploymentConfig = null,
91-
AppState $appState = null,
92-
ViewCollectionFactory $viewCollectionFactory = null,
93-
SubscriptionFactory $subscriptionFactory = null,
94-
ResourceConnection $resource = null
67+
AppState $appState = null
9568
) {
9669
$this->installerFactory = $installerFactory;
9770
$this->searchConfigFactory = $searchConfigFactory;
9871
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
9972
$this->appState = $appState ?: ObjectManager::getInstance()->get(AppState::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);
10573
parent::__construct();
10674
}
10775

@@ -164,7 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
164132
$installer->updateModulesSequence($keepGenerated);
165133
$searchConfig = $this->searchConfigFactory->create();
166134
$searchConfig->validateSearchEngine();
167-
$this->removeUnusedTriggers();
135+
$installer->removeUnusedTriggers();
168136
$installer->installSchema($request);
169137
$installer->installDataFixtures($request);
170138

@@ -192,65 +160,4 @@ protected function execute(InputInterface $input, OutputInterface $output)
192160

193161
return Cli::RETURN_SUCCESS;
194162
}
195-
196-
/**
197-
* Remove unused triggers
198-
*/
199-
private function removeUnusedTriggers()
200-
{
201-
$viewCollection = $this->viewCollectionFactory->create();
202-
$viewList = $viewCollection->getViewsByStateMode(StateInterface::MODE_ENABLED);
203-
204-
// Unsubscribe mviews
205-
foreach ($viewList as $view) {
206-
/** @var ViewInterface $view */
207-
$view->unsubscribe();
208-
}
209-
210-
// Remove extra triggers that have correct naming structure
211-
$triggers = $this->getTriggers();
212-
foreach ($triggers as $trigger) {
213-
$this->initSubscriptionInstance($trigger['Table'])->remove();
214-
}
215-
216-
// Subscribe mviews
217-
foreach ($viewList as $view) {
218-
/** @var ViewInterface $view */
219-
$view->subscribe();
220-
}
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-
}
256163
}

setup/src/Magento/Setup/Model/Installer.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Magento\Framework\Model\ResourceModel\Db\Context;
2626
use Magento\Framework\Module\ModuleList\Loader as ModuleLoader;
2727
use Magento\Framework\Module\ModuleListInterface;
28+
use Magento\Framework\Mview\OldViews;
2829
use Magento\Framework\Setup\Declaration\Schema\DryRunLogger;
2930
use Magento\Framework\Setup\FilePermissions;
3031
use Magento\Framework\Setup\InstallDataInterface;
@@ -247,6 +248,11 @@ class Installer
247248
*/
248249
private $patchApplierFactory;
249250

251+
/**
252+
* @var OldViews
253+
*/
254+
private $oldViews;
255+
250256
/**
251257
* Constructor
252258
*
@@ -320,6 +326,7 @@ public function __construct(
320326
$this->componentRegistrar = $componentRegistrar;
321327
$this->phpReadinessCheck = $phpReadinessCheck;
322328
$this->schemaPersistor = $this->objectManagerProvider->get()->get(SchemaPersistor::class);
329+
$this->oldViews = $this->objectManagerProvider->get()->get(OldViews::class);
323330
}
324331

325332
/**
@@ -1646,4 +1653,12 @@ private function updateColumnType(
16461653
);
16471654
}
16481655
}
1656+
1657+
/**
1658+
* Remove unused triggers from db
1659+
*/
1660+
public function removeUnusedTriggers(): void
1661+
{
1662+
$this->oldViews->unsubscribe();
1663+
}
16491664
}

0 commit comments

Comments
 (0)