|
6 | 6 | namespace Magento\Setup\Console\Command;
|
7 | 7 |
|
8 | 8 | use Magento\Deploy\Console\Command\App\ConfigImportCommand;
|
9 |
| -use Magento\Framework\App\State as AppState; |
10 | 9 | use Magento\Framework\App\DeploymentConfig;
|
11 | 10 | use Magento\Framework\App\ObjectManager;
|
| 11 | +use Magento\Framework\App\ResourceConnection; |
| 12 | +use Magento\Framework\App\State as AppState; |
| 13 | +use Magento\Framework\DB\Ddl\Trigger; |
| 14 | +use Magento\Framework\Mview\View\CollectionInterface as ViewCollection; |
| 15 | +use Magento\Framework\Mview\View\StateInterface; |
12 | 16 | use Magento\Framework\Setup\ConsoleLogger;
|
13 | 17 | use Magento\Framework\Setup\Declaration\Schema\DryRunLogger;
|
14 | 18 | use Magento\Framework\Setup\Declaration\Schema\OperationsExecutor;
|
@@ -52,22 +56,30 @@ class UpgradeCommand extends AbstractSetupCommand
|
52 | 56 | */
|
53 | 57 | private $searchConfigFactory;
|
54 | 58 |
|
| 59 | + /* |
| 60 | + * @var ViewCollection |
| 61 | + */ |
| 62 | + private $viewCollection; |
| 63 | + |
55 | 64 | /**
|
56 | 65 | * @param InstallerFactory $installerFactory
|
57 | 66 | * @param SearchConfigFactory $searchConfigFactory
|
58 | 67 | * @param DeploymentConfig $deploymentConfig
|
59 | 68 | * @param AppState|null $appState
|
| 69 | + * @param ViewCollection|null $viewCollection |
60 | 70 | */
|
61 | 71 | public function __construct(
|
62 | 72 | InstallerFactory $installerFactory,
|
63 | 73 | SearchConfigFactory $searchConfigFactory,
|
64 | 74 | DeploymentConfig $deploymentConfig = null,
|
65 |
| - AppState $appState = null |
| 75 | + AppState $appState = null, |
| 76 | + ViewCollection $viewCollection = null |
66 | 77 | ) {
|
67 | 78 | $this->installerFactory = $installerFactory;
|
68 | 79 | $this->searchConfigFactory = $searchConfigFactory;
|
69 | 80 | $this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
|
70 | 81 | $this->appState = $appState ?: ObjectManager::getInstance()->get(AppState::class);
|
| 82 | + $this->viewCollection = $viewCollection ?: ObjectManager::getInstance()->get(ViewCollection::class); |
71 | 83 | parent::__construct();
|
72 | 84 | }
|
73 | 85 |
|
@@ -130,6 +142,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
|
130 | 142 | $installer->updateModulesSequence($keepGenerated);
|
131 | 143 | $searchConfig = $this->searchConfigFactory->create();
|
132 | 144 | $searchConfig->validateSearchEngine();
|
| 145 | + |
| 146 | + $this->removeUnusedTriggers(); |
| 147 | + |
133 | 148 | $installer->installSchema($request);
|
134 | 149 | $installer->installDataFixtures($request);
|
135 | 150 |
|
@@ -157,4 +172,43 @@ protected function execute(InputInterface $input, OutputInterface $output)
|
157 | 172 |
|
158 | 173 | return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
|
159 | 174 | }
|
| 175 | + |
| 176 | + /** |
| 177 | + * Remove unused triggers |
| 178 | + */ |
| 179 | + private function removeUnusedTriggers() |
| 180 | + { |
| 181 | + // unsubscribe mview |
| 182 | + $viewList = $this->viewCollection->getViewsByStateMode(StateInterface::MODE_ENABLED); |
| 183 | + foreach ($viewList as $view) { |
| 184 | + /** @var \Magento\Framework\Mview\ViewInterface $view */ |
| 185 | + $view->unsubscribe(); |
| 186 | + } |
| 187 | + |
| 188 | + // remove extra triggers that have correct naming structure |
| 189 | + /* @var ResourceConnection $resource */ |
| 190 | + $resource = ObjectManager::getInstance()->get(ResourceConnection::class); |
| 191 | + $connection = $resource->getConnection(); |
| 192 | + $triggers = $connection->getTriggers(); |
| 193 | + foreach ($triggers as $trigger) { |
| 194 | + $triggerNames = []; |
| 195 | + foreach (Trigger::getListOfEvents() as $event) { |
| 196 | + $triggerName = $resource->getTriggerName( |
| 197 | + $resource->getTableName($trigger['Table']), |
| 198 | + Trigger::TIME_AFTER, |
| 199 | + $event |
| 200 | + ); |
| 201 | + $triggerNames[] = strtolower($triggerName); |
| 202 | + } |
| 203 | + if (in_array($trigger['Trigger'], $triggerNames)) { |
| 204 | + $connection->dropTrigger($trigger['Trigger']); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + // subscribe mview |
| 209 | + foreach ($viewList as $view) { |
| 210 | + /** @var \Magento\Framework\Mview\ViewInterface $view */ |
| 211 | + $view->subscribe(); |
| 212 | + } |
| 213 | + } |
160 | 214 | }
|
0 commit comments