Skip to content

Commit 32ce065

Browse files
committed
MC-33026: Upgrade magento with MySQL search engine removed
1 parent cccb714 commit 32ce065

File tree

15 files changed

+197
-55
lines changed

15 files changed

+197
-55
lines changed

app/code/Magento/Elasticsearch/Setup/InstallConfig.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Magento\Framework\App\Config\ReinitableConfigInterface;
1111
use Magento\Framework\App\Config\Storage\WriterInterface;
12-
use Magento\Framework\Exception\InputException;
1312
use Magento\Setup\Model\SearchConfigOptionsList;
1413
use Magento\Search\Setup\InstallConfigInterface;
1514

@@ -27,11 +26,6 @@ class InstallConfig implements InstallConfigInterface
2726
SearchConfigOptionsList::INPUT_KEY_SEARCH_ENGINE => 'engine'
2827
];
2928

30-
/**
31-
* @var ConnectionValidator
32-
*/
33-
private $validator;
34-
3529
/**
3630
* @var WriterInterface
3731
*/
@@ -44,18 +38,15 @@ class InstallConfig implements InstallConfigInterface
4438

4539
/**
4640
* @param WriterInterface $configWriter
47-
* @param ConnectionValidator $validator
4841
* @param ReinitableConfigInterface $reinitableConfig
4942
* @param array $searchConfigMapping
5043
*/
5144
public function __construct(
5245
WriterInterface $configWriter,
53-
ConnectionValidator $validator,
5446
ReinitableConfigInterface $reinitableConfig,
5547
array $searchConfigMapping = []
5648
) {
5749
$this->configWriter = $configWriter;
58-
$this->validator = $validator;
5950
$this->reinitableConfig = $reinitableConfig;
6051
$this->searchConfigMapping = array_merge($this->searchConfigMapping, $searchConfigMapping);
6152
}
@@ -73,14 +64,5 @@ public function configure(array $inputOptions)
7364
$this->configWriter->save(self::CATALOG_SEARCH . $configKey, $inputValue);
7465
}
7566
$this->reinitableConfig->reinit();
76-
$searchEngine = $inputOptions[SearchConfigOptionsList::INPUT_KEY_SEARCH_ENGINE] ?? null;
77-
if (!$this->validator->validate($searchEngine)) {
78-
throw new InputException(
79-
__(
80-
'Connection to Elasticsearch cannot be established. '
81-
. 'Please check the configuration and try again.'
82-
)
83-
);
84-
}
8567
}
8668
}

app/code/Magento/Elasticsearch/Setup/ConnectionValidator.php renamed to app/code/Magento/Elasticsearch/Setup/Validator.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
namespace Magento\Elasticsearch\Setup;
99

1010
use Magento\AdvancedSearch\Model\Client\ClientResolver;
11+
use Magento\Search\Model\SearchEngine\ValidatorInterface;
1112

1213
/**
1314
* Validate Elasticsearch connection
1415
*/
15-
class ConnectionValidator
16+
class Validator implements ValidatorInterface
1617
{
1718
/**
1819
* @var ClientResolver
@@ -30,16 +31,19 @@ public function __construct(ClientResolver $clientResolver)
3031
/**
3132
* Checks Elasticsearch Connection
3233
*
33-
* @param string|null $searchEngine if empty, uses the currently configured engine
34-
* @return bool true if the connection succeeded, false otherwise
34+
* @param array $searchConfig
35+
* @return array
3536
*/
36-
public function validate(string $searchEngine = null): bool
37+
public function validate(array $searchConfig = []): array
3738
{
39+
$errors = [];
40+
$searchEngine = $searchConfig['engine'] ?? null;
3841
try {
3942
$client = $this->clientResolver->create($searchEngine);
40-
return $client->testConnection();
43+
$client->testConnection();
4144
} catch (\Exception $e) {
42-
return false;
45+
$errors[] = 'Elasticsearch connection validation failed: ' . $e->getMessage();
4346
}
47+
return $errors;
4448
}
4549
}

app/code/Magento/Elasticsearch/Test/Unit/Setup/ConnectionValidatorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Magento\Elasticsearch\Test\Unit\Setup;
99

1010
use Magento\AdvancedSearch\Model\Client\ClientResolver;
11-
use Magento\Elasticsearch\Setup\ConnectionValidator;
11+
use Magento\Elasticsearch\Setup\Validator;
1212
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1313
use Magento\Elasticsearch\Elasticsearch5\Model\Client\Elasticsearch;
1414
use PHPUnit\Framework\MockObject\MockObject;
@@ -17,7 +17,7 @@
1717
class ConnectionValidatorTest extends TestCase
1818
{
1919
/**
20-
* @var ConnectionValidator
20+
* @var Validator
2121
*/
2222
private $connectionValidator;
2323

@@ -42,7 +42,7 @@ protected function setUp()
4242

4343
$objectManager = new ObjectManager($this);
4444
$this->connectionValidator = $objectManager->getObject(
45-
ConnectionValidator::class,
45+
Validator::class,
4646
[
4747
'clientResolver' => $this->clientResolverMock
4848
]

app/code/Magento/Elasticsearch/Test/Unit/Setup/InstallConfigTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Magento\Elasticsearch\Test\Unit\Setup;
99

10-
use Magento\Elasticsearch\Setup\ConnectionValidator;
10+
use Magento\Elasticsearch\Setup\Validator;
1111
use Magento\Elasticsearch\Setup\InstallConfig;
1212
use Magento\Framework\App\Config\Storage\WriterInterface;
1313
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -22,7 +22,7 @@ class InstallConfigTest extends TestCase
2222
private $installConfig;
2323

2424
/**
25-
* @var ConnectionValidator|MockObject
25+
* @var Validator|MockObject
2626
*/
2727
private $validatorMock;
2828

@@ -33,14 +33,14 @@ class InstallConfigTest extends TestCase
3333

3434
protected function setup()
3535
{
36-
$this->validatorMock = $this->getMockBuilder(ConnectionValidator::class)
36+
$this->validatorMock = $this->getMockBuilder(Validator::class)
3737
->disableOriginalConstructor()
3838
->getMock();
3939
$this->configWriterMock = $this->getMockBuilder(WriterInterface::class)->getMockForAbstractClass();
4040

4141
$objectManager = new ObjectManager($this);
4242
$this->installConfig = $objectManager->getObject(
43-
InstallConfig::class,
43+
InstallConfigInterface::class,
4444
[
4545
'configWriter' => $this->configWriterMock,
4646
'validator' => $this->validatorMock,

app/code/Magento/Elasticsearch/etc/di.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,4 +533,14 @@
533533
</argument>
534534
</arguments>
535535
</type>
536+
<type name="Magento\Search\Model\SearchEngine\Validator">
537+
<arguments>
538+
<argument name="engineBlacklist" xsi:type="array">
539+
<item name="elasticsearch2" xsi:type="string">elasticsearch2</item>
540+
</argument>
541+
<argument name="engineValidators" xsi:type="array">
542+
<item name="elasticsearch5" xsi:type="object">Magento\Elasticsearch\Setup\Validator</item>
543+
</argument>
544+
</arguments>
545+
</type>
536546
</config>

app/code/Magento/Elasticsearch6/etc/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,11 @@
240240
</argument>
241241
</arguments>
242242
</type>
243+
<type name="Magento\Search\Model\SearchEngine\Validator">
244+
<arguments>
245+
<argument name="engineValidators" xsi:type="array">
246+
<item name="elasticsearch6" xsi:type="object">Magento\Elasticsearch\Setup\Validator</item>
247+
</argument>
248+
</arguments>
249+
</type>
243250
</config>

app/code/Magento/Elasticsearch7/etc/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,11 @@
243243
</argument>
244244
</arguments>
245245
</type>
246+
<type name="Magento\Search\Model\SearchEngine\Validator">
247+
<arguments>
248+
<argument name="engineValidators" xsi:type="array">
249+
<item name="elasticsearch7" xsi:type="object">Magento\Elasticsearch\Setup\Validator</item>
250+
</argument>
251+
</arguments>
252+
</type>
246253
</config>

app/code/Magento/Elasticsearch7/etc/module.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<module name="Magento_Elasticsearch7">
1010
<sequence>
1111
<module name="Magento_Elasticsearch"/>
12+
<module name="Magento_Elasticsearch6"/>
1213
</sequence>
1314
</module>
1415
</config>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Search\Model\SearchEngine;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
12+
/**
13+
* Validate search engine configuration
14+
*/
15+
class Validator implements ValidatorInterface
16+
{
17+
/**
18+
* @var ScopeConfigInterface
19+
*/
20+
private $scopeConfig;
21+
22+
/**
23+
* @var array
24+
*/
25+
private $engineBlacklist = ['mysql'];
26+
27+
/**
28+
* @var ValidatorInterface[]
29+
*/
30+
private $engineValidators;
31+
32+
/**
33+
* @param ScopeConfigInterface $scopeConfig
34+
* @param array $engineValidators
35+
* @param array $engineBlacklist
36+
*/
37+
public function __construct(
38+
ScopeConfigInterface $scopeConfig,
39+
array $engineValidators = [],
40+
array $engineBlacklist = []
41+
) {
42+
$this->scopeConfig = $scopeConfig;
43+
$this->engineValidators = $engineValidators;
44+
$this->engineBlacklist = array_merge($this->engineBlacklist, $engineBlacklist);
45+
}
46+
47+
48+
/**
49+
* @inheritDoc
50+
*/
51+
public function validate(array $searchConfig = []): array
52+
{
53+
$errors = [];
54+
55+
$currentEngine = isset($searchConfig['engine'])
56+
? $searchConfig['engine']
57+
: $this->scopeConfig->getValue('catalog/search/engine');
58+
59+
if (in_array($currentEngine, $this->engineBlacklist)) {
60+
$errors[] = "Search engine '{$currentEngine}' is not supported. Fix search configuration and try again.";
61+
}
62+
63+
if (isset($this->engineValidators[$currentEngine])) {
64+
$validator = $this->engineValidators[$currentEngine];
65+
$validationErrors = $validator->validate($searchConfig);
66+
$errors = array_merge($errors, $validationErrors);
67+
}
68+
return $errors;
69+
}
70+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Search\Model\SearchEngine;
9+
10+
/**
11+
* Validate search engine configuration
12+
*/
13+
interface ValidatorInterface
14+
{
15+
/**
16+
* Validate search engine
17+
*
18+
* @param array $searchConfig
19+
* @return string[] array of errors, empty array if validation passed
20+
*/
21+
public function validate(array $searchConfig = []): array;
22+
}

0 commit comments

Comments
 (0)