Skip to content

Commit 01c5985

Browse files
committed
MAGE-1218 Fix replica sync enablement check
1 parent df8dca1 commit 01c5985

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

Service/Product/ReplicaManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ function($replica) use ($replicas) {
470470
*/
471471
public function isReplicaSyncEnabled(int $storeId): bool
472472
{
473-
return $this->configHelper->isInstantEnabled($storeId);
473+
return $this->configHelper->isInstantEnabled($storeId) && $this->configHelper->isEnabledBackend($storeId);
474474
}
475475

476476
/**

Setup/Patch/Data/RebuildReplicasPatch.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ public function apply(): PatchInterface
6767
// Area code is already set - nothing to do
6868
}
6969

70-
$storeIds = array_keys($this->storeManager->getStores());
71-
// Delete all replicas before resyncing in case of incorrect replica assignments
70+
$storeIds = array_filter(
71+
array_keys($this->storeManager->getStores()),
72+
function (int $storeId) { return $this->replicaManager->isReplicaSyncEnabled($storeId); }
73+
);
7274

7375
try {
76+
// Delete all replicas before resyncing in case of incorrect replica assignments
7477
foreach ($storeIds as $storeId) {
7578
$this->retryDeleteReplica($storeId);
7679
}
@@ -81,7 +84,8 @@ public function apply(): PatchInterface
8184
}
8285
}
8386
catch (AlgoliaException $e) {
84-
$this->logger->error("Could not rebuild replicas - a full reindex will be required.");
87+
// Log the error but do not prevent setup:update
88+
$this->logger->error("Could not rebuild replicas - a full reindex may be required.");
8589
}
8690

8791
$this->moduleDataSetup->getConnection()->endSetup();

Test/Integration/Product/ReplicaIndexingTest.php

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,10 @@ protected function mockSortUpdate(string $sortAttr, string $sortDir, array $attr
172172
*/
173173
public function testReplicaRebuild(): void
174174
{
175-
$primaryIndexName = $this->getIndexName('default');
176-
175+
// Make one replica virtual
177176
$this->mockSortUpdate('price', 'desc', ['virtualReplica' => 1]);
178-
$sorting = $this->objectManager->get(\Algolia\AlgoliaSearch\Service\Product\SortingTransformer::class)->getSortingIndices(1, null, null, true);
179177

180-
$syncCmd = $this->objectManager->get(\Algolia\AlgoliaSearch\Console\Command\ReplicaSyncCommand::class);
181-
$this->mockProperty($syncCmd, 'output', \Symfony\Component\Console\Output\OutputInterface::class);
182-
$syncCmd->syncReplicas();
183-
$this->algoliaHelper->waitLastTask();
178+
$sorting = $this->populateReplicas(1);
184179

185180
$rebuildCmd = $this->objectManager->get(\Algolia\AlgoliaSearch\Console\Command\ReplicaRebuildCommand::class);
186181
$this->invokeMethod(
@@ -193,12 +188,9 @@ public function testReplicaRebuild(): void
193188
);
194189
$this->algoliaHelper->waitLastTask();
195190

196-
$currentSettings = $this->algoliaHelper->getSettings($primaryIndexName);
197-
$this->assertArrayHasKey('replicas', $currentSettings);
198-
$replicas = $currentSettings['replicas'];
191+
$replicas = $this->assertReplicasCreated($sorting);
199192

200-
$this->assertEquals(count($sorting), count($replicas));
201-
$this->assertSortToReplicaConfigParity($primaryIndexName, $sorting, $replicas);
193+
$this->assertSortToReplicaConfigParity($this->indexName, $sorting, $replicas);
202194
}
203195

204196
/**
@@ -209,17 +201,33 @@ public function testReplicaRebuild(): void
209201
*/
210202
public function testReplicaSync(): void
211203
{
212-
$primaryIndexName = $this->indexName;
213-
214204
// Make one replica virtual
215205
$this->mockSortUpdate('created_at', 'desc', ['virtualReplica' => 1]);
216206

217207
$sorting = $this->populateReplicas(1);
218208

219209
$replicas = $this->assertReplicasCreated($sorting);
220210

221-
$this->assertEquals(count($sorting), count($replicas));
222-
$this->assertSortToReplicaConfigParity($primaryIndexName, $sorting, $replicas);
211+
$this->assertSortToReplicaConfigParity($this->indexName, $sorting, $replicas);
212+
}
213+
214+
/**
215+
* @magentoConfigFixture current_store algoliasearch_credentials/credentials/enable_backend 0
216+
* @magentoConfigFixture current_store algoliasearch_instant/instant/is_instant_enabled 1
217+
* @throws AlgoliaException
218+
* @throws ExceededRetriesException
219+
* @throws \ReflectionException
220+
*/
221+
public function testReplicaSyncDisabled(): void
222+
{
223+
$primaryIndexName = $this->indexName;
224+
$settings = $this->algoliaHelper->getSettings($this->indexName);
225+
$this->assertArrayNotHasKey(ReplicaManager::ALGOLIA_SETTINGS_KEY_REPLICAS, $settings);
226+
227+
$this->populateReplicas(1);
228+
229+
$newSettings = $this->algoliaHelper->getSettings($this->indexName);
230+
$this->assertArrayNotHasKey(ReplicaManager::ALGOLIA_SETTINGS_KEY_REPLICAS, $newSettings);
223231
}
224232

225233
/**
@@ -252,14 +260,17 @@ public function testReplicaDeleteUnreliable(): void
252260

253261
/**
254262
* Test the RebuildReplicasPatch with API failures
263+
* @magentoConfigFixture current_store algoliasearch_credentials/credentials/enable_backend 1
255264
* @magentoConfigFixture current_store algoliasearch_instant/instant/is_instant_enabled 1
256265
*/
257266
public function testReplicaRebuildPatch(): void
258267
{
259-
$sorting = $this->populateReplicas(1);
260-
$replicas = $this->assertReplicasCreated($sorting);
261-
268+
$currentStoreId = 1;
262269
$this->assertTrue($this->configHelper->credentialsAreConfigured(), "Credentials not available to apply patch.");
270+
$this->assertTrue($this->replicaManager->isReplicaSyncEnabled($currentStoreId), "Replica sync is not enabled for test store $currentStoreId.");
271+
272+
$sorting = $this->populateReplicas($currentStoreId);
273+
$replicas = $this->assertReplicasCreated($sorting);
263274

264275
$patch = new \Algolia\AlgoliaSearch\Setup\Patch\Data\RebuildReplicasPatch(
265276
$this->objectManager->get(ModuleDataSetupInterface::class),
@@ -275,7 +286,9 @@ public function testReplicaRebuildPatch(): void
275286
$patch->apply();
276287

277288
$this->algoliaHelper->waitLastTask();
278-
$this->assertEquals(count($sorting), count($replicas));
289+
290+
$replicas = $this->assertReplicasCreated($sorting);
291+
279292
$this->assertSortToReplicaConfigParity($this->indexName, $sorting, $replicas);
280293
}
281294

0 commit comments

Comments
 (0)