Skip to content

Commit 963c22c

Browse files
Indrani SonawaneIndrani Sonawane
authored andcommitted
Merge remote-tracking branch '30699/indexer-unsubscribe-drop' into community_prs_march
2 parents 6f4bfa1 + 28de31a commit 963c22c

File tree

6 files changed

+45
-13
lines changed

6 files changed

+45
-13
lines changed

app/code/Magento/Indexer/Model/Indexer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ public function setScheduled($scheduled)
326326
$this->getView()->subscribe();
327327
} else {
328328
$this->getView()->unsubscribe();
329+
$this->invalidate();
329330
}
330331
$this->getState()->save();
331332
}

app/code/Magento/Indexer/Setup/Recurring.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,14 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
121121

122122
$indexer = $this->indexerFactory->create()->load($indexerId);
123123
if ($indexer->isScheduled()) {
124-
$indexer->getView()->unsubscribe()->subscribe();
124+
// The purpose of the following two lines is to ensure that any
125+
// database triggers are correctly set up for this indexer. We
126+
// are calling methods on the view directly because we want to
127+
// choose to not drop the changelog tables at this time. This
128+
// also intentionally bypasses the $indexer->invalidate() call
129+
// within $indexer->setScheduled().
130+
$indexer->getView()->unsubscribe(false);
131+
$indexer->getView()->subscribe();
125132
}
126133
}
127134
}

app/code/Magento/Indexer/Test/Unit/Model/IndexerTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,18 @@ public function testIsScheduled()
523523
*/
524524
public function testSetScheduled($scheduled, $method)
525525
{
526-
$stateMock = $this->createPartialMock(State::class, ['load', 'save']);
526+
$stateMock = $this->createPartialMock(State::class, ['load', 'save', 'setStatus']);
527527

528528
$this->stateFactoryMock->expects($this->once())->method('create')->willReturn($stateMock);
529529
$this->viewMock->expects($this->once())->method('load')->willReturnSelf();
530-
$this->viewMock->expects($this->once())->method($method)->willReturn(true);
531-
$stateMock->expects($this->once())->method('save')->willReturnSelf();
530+
$this->viewMock->expects($this->once())->method($method)->willReturnSelf();
531+
$stateMock->expects($this->atLeastOnce())->method('save')->willReturnSelf();
532+
if (!$scheduled) {
533+
$stateMock->expects($this->once())
534+
->method('setStatus')
535+
->with(StateInterface::STATUS_INVALID)
536+
->willReturnSelf();
537+
}
532538
$this->model->setScheduled($scheduled);
533539
}
534540

lib/internal/Magento/Framework/Mview/Test/Unit/ViewSubscribeTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,11 @@ public function testUnsubscribe()
194194
$this->stateMock->expects($this->once())
195195
->method('setMode')
196196
->with(StateInterface::MODE_DISABLED)->willReturnSelf();
197-
$this->changelogMock->expects($this->never())
197+
$this->stateMock->expects($this->once())
198+
->method('setStatus')
199+
->with(StateInterface::STATUS_IDLE)
200+
->willReturnSelf();
201+
$this->changelogMock->expects($this->once())
198202
->method('drop');
199203
$subscriptionMock = $this->createPartialMock(Subscription::class, ['remove']);
200204
$subscriptionMock->expects($this->exactly(1))->method('remove');
@@ -221,6 +225,8 @@ public function testUnsubscribeDisabled()
221225
->method('setVersionId');
222226
$this->stateMock->expects($this->never())
223227
->method('setMode');
228+
$this->stateMock->expects($this->never())
229+
->method('setStatus');
224230
$this->changelogMock->expects($this->never())
225231
->method('drop');
226232
$this->subscriptionFactoryMock->expects($this->never())

lib/internal/Magento/Framework/Mview/View.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,21 +219,32 @@ public function subscribe()
219219
}
220220

221221
/**
222-
* Remove subscriptions
223-
*
224-
* @return ViewInterface
225-
* @throws Exception
222+
* @inheritDoc
226223
*/
227-
public function unsubscribe()
224+
public function unsubscribe(bool $dropTable = true): ViewInterface
228225
{
229226
if ($this->getState()->getMode() != View\StateInterface::MODE_DISABLED) {
230227
// Remove subscriptions
231228
foreach ($this->getSubscriptions() as $subscriptionConfig) {
232229
$this->initSubscriptionInstance($subscriptionConfig)->remove();
233230
}
234231

232+
if ($dropTable) {
233+
try {
234+
$this->getChangelog()->drop();
235+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
236+
} catch (ChangelogTableNotExistsException $e) {
237+
// We want the table to not exist, and it doesn't. All done.
238+
}
239+
240+
$this->getState()->setVersionId(0);
241+
}
242+
235243
// Update view state
236-
$this->getState()->setMode(View\StateInterface::MODE_DISABLED)->save();
244+
$this->getState()
245+
->setMode(View\StateInterface::MODE_DISABLED)
246+
->setStatus(View\StateInterface::STATUS_IDLE)
247+
->save();
237248
}
238249

239250
return $this;

lib/internal/Magento/Framework/Mview/ViewInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ public function load($viewId);
6060
public function subscribe();
6161

6262
/**
63-
* Remove subscriptions
63+
* Remove subscriptions and optionally drop the changelog table
6464
*
65+
* @param bool $dropTable
6566
* @throws \Exception
6667
* @return ViewInterface
6768
*/
68-
public function unsubscribe();
69+
public function unsubscribe(bool $dropTable = true): ViewInterface;
6970

7071
/**
7172
* Materialize view by IDs in changelog

0 commit comments

Comments
 (0)