Skip to content

Commit 3806fe0

Browse files
authored
MCLOUD-7098: Validation which compare service versions and magento version doesn't work on PRO (#6)
1 parent eaea526 commit 3806fe0

File tree

14 files changed

+698
-268
lines changed

14 files changed

+698
-268
lines changed

src/Config/Validator/Deploy/ServiceEol.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
namespace Magento\MagentoCloud\Config\Validator\Deploy;
99

1010
use Magento\MagentoCloud\App\Error;
11+
use Magento\MagentoCloud\App\GenericException;
1112
use Magento\MagentoCloud\Config\Validator;
12-
use Magento\MagentoCloud\Filesystem\FileSystemException;
1313
use Magento\MagentoCloud\Service\EolValidator as EOLValidator;
1414
use Magento\MagentoCloud\Config\ValidatorInterface;
15-
use Magento\MagentoCloud\Service\ServiceMismatchException;
1615

1716
/**
1817
* Class to check if services approaching their EOLs.
@@ -52,8 +51,7 @@ public function __construct(
5251
/**
5352
* Get the defined services and versions and check for their EOLs by error level.
5453
*
55-
* @return Validator\ResultInterface
56-
* @throws FileSystemException
54+
* {@inheritDoc}
5755
*/
5856
public function validate(): Validator\ResultInterface
5957
{
@@ -70,7 +68,7 @@ public function validate(): Validator\ResultInterface
7068
$this->errorLevel == ValidatorInterface::LEVEL_WARNING ? Error::WARN_SERVICE_PASSED_EOL : null
7169
);
7270
}
73-
} catch (ServiceMismatchException $e) {
71+
} catch (GenericException $e) {
7472
return $this->resultFactory->error('Can\'t validate version of some services: ' . $e->getMessage());
7573
}
7674

src/Config/Validator/Deploy/ServiceVersion.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\MagentoCloud\Service\Validator as ServiceVersionValidator;
1515
use Magento\MagentoCloud\Config\Validator;
1616
use Magento\MagentoCloud\Config\ValidatorInterface;
17+
use Psr\Log\LoggerInterface;
1718

1819
/**
1920
* Validates installed service versions according to version mapping.
@@ -36,19 +37,27 @@ class ServiceVersion implements ValidatorInterface
3637
*/
3738
private $serviceFactory;
3839

40+
/**
41+
* @var LoggerInterface
42+
*/
43+
private $logger;
44+
3945
/**
4046
* @param Validator\ResultFactory $resultFactory
4147
* @param ServiceVersionValidator $serviceVersionValidator
4248
* @param ServiceFactory $serviceFactory
49+
* @param LoggerInterface $logger
4350
*/
4451
public function __construct(
4552
Validator\ResultFactory $resultFactory,
4653
ServiceVersionValidator $serviceVersionValidator,
47-
ServiceFactory $serviceFactory
54+
ServiceFactory $serviceFactory,
55+
LoggerInterface $logger
4856
) {
4957
$this->resultFactory = $resultFactory;
5058
$this->serviceVersionValidator = $serviceVersionValidator;
5159
$this->serviceFactory = $serviceFactory;
60+
$this->logger = $logger;
5261
}
5362

5463
/**
@@ -69,6 +78,10 @@ public function validate(): Validator\ResultInterface
6978
foreach ($services as $serviceName) {
7079
$service = $this->serviceFactory->create($serviceName);
7180
$serviceVersion = $service->getVersion();
81+
82+
$logMsq = $serviceVersion ? 'is ' . $serviceVersion : 'is not detected';
83+
$this->logger->info(sprintf('Version of service \'%s\' %s', $serviceName, $logMsq));
84+
7285
if ($serviceVersion !== '0' &&
7386
$error = $this->serviceVersionValidator->validateService($serviceName, $serviceVersion)
7487
) {

src/DB/Data/ConnectionTypes.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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\MagentoCloud\DB\Data;
9+
10+
use Magento\MagentoCloud\Config\Environment;
11+
12+
/**
13+
* Returns database service configurations.
14+
*/
15+
class ConnectionTypes
16+
{
17+
const RELATIONSHIP_KEY = 'database';
18+
const RELATIONSHIP_SLAVE_KEY = 'database-slave';
19+
20+
const RELATIONSHIP_QUOTE_KEY = 'database-quote';
21+
const RELATIONSHIP_QUOTE_SLAVE_KEY = 'database-quote-slave';
22+
23+
const RELATIONSHIP_SALES_KEY = 'database-sales';
24+
const RELATIONSHIP_SALES_SLAVE_KEY = 'database-sales-slave';
25+
26+
/**
27+
* @var Environment
28+
*/
29+
private $environment;
30+
31+
/**
32+
* @param Environment $environment
33+
*/
34+
public function __construct(
35+
Environment $environment
36+
) {
37+
$this->environment = $environment;
38+
}
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
public function getConfiguration(): array
44+
{
45+
return $this->environment->getRelationship(self::RELATIONSHIP_KEY)[0] ?? [];
46+
}
47+
48+
/**
49+
* Returns service configuration for slave.
50+
*
51+
* @return array
52+
*/
53+
public function getSlaveConfiguration(): array
54+
{
55+
return $this->environment->getRelationship(self::RELATIONSHIP_SLAVE_KEY)[0] ?? [];
56+
}
57+
58+
/**
59+
* Returns configuration for quote service.
60+
*/
61+
public function getQuoteConfiguration(): array
62+
{
63+
return $this->environment->getRelationship(self::RELATIONSHIP_QUOTE_KEY)[0] ?? [];
64+
}
65+
66+
/**
67+
* Returns configuration for quote slave service.
68+
*
69+
* @return array
70+
*/
71+
public function getQuoteSlaveConfiguration(): array
72+
{
73+
return $this->environment->getRelationship(self::RELATIONSHIP_QUOTE_SLAVE_KEY)[0] ?? [];
74+
}
75+
76+
/**
77+
* Returns configuration for sales service.
78+
*/
79+
public function getSalesConfiguration(): array
80+
{
81+
return $this->environment->getRelationship(self::RELATIONSHIP_SALES_KEY)[0] ?? [];
82+
}
83+
84+
/**
85+
* Returns configuration for slave sales service.
86+
*
87+
* @return array
88+
*/
89+
public function getSalesSlaveConfiguration(): array
90+
{
91+
return $this->environment->getRelationship(self::RELATIONSHIP_SALES_SLAVE_KEY)[0] ?? [];
92+
}
93+
}

src/DB/Data/RelationshipConnectionFactory.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace Magento\MagentoCloud\DB\Data;
99

10-
use Magento\MagentoCloud\Service\Database;
11-
1210
/**
1311
* Responsible for creating and configuring Magento\MagentoCloud\DB\Data\ConnectionInterface instances.
1412
*/
@@ -24,16 +22,16 @@ class RelationshipConnectionFactory
2422
const CONNECTION_SALES_SLAVE = 'sales-slave';
2523

2624
/**
27-
* @var Database
25+
* @var ConnectionTypes
2826
*/
29-
private $database;
27+
private $connectionType;
3028

3129
/**
32-
* @param Database $database
30+
* @param ConnectionTypes $connectionType
3331
*/
34-
public function __construct(Database $database)
32+
public function __construct(ConnectionTypes $connectionType)
3533
{
36-
$this->database = $database;
34+
$this->connectionType = $connectionType;
3735
}
3836

3937
/**
@@ -47,22 +45,22 @@ public function create(string $connectionType): ConnectionInterface
4745
{
4846
switch ($connectionType) {
4947
case self::CONNECTION_MAIN:
50-
$configuration = $this->database->getConfiguration();
48+
$configuration = $this->connectionType->getConfiguration();
5149
break;
5250
case self::CONNECTION_SLAVE:
53-
$configuration = $this->database->getSlaveConfiguration();
51+
$configuration = $this->connectionType->getSlaveConfiguration();
5452
break;
5553
case self::CONNECTION_QUOTE_MAIN:
56-
$configuration = $this->database->getQuoteConfiguration();
54+
$configuration = $this->connectionType->getQuoteConfiguration();
5755
break;
5856
case self::CONNECTION_QUOTE_SLAVE:
59-
$configuration = $this->database->getQuoteSlaveConfiguration();
57+
$configuration = $this->connectionType->getQuoteSlaveConfiguration();
6058
break;
6159
case self::CONNECTION_SALES_MAIN:
62-
$configuration = $this->database->getSalesConfiguration();
60+
$configuration = $this->connectionType->getSalesConfiguration();
6361
break;
6462
case self::CONNECTION_SALES_SLAVE:
65-
$configuration = $this->database->getSalesSlaveConfiguration();
63+
$configuration = $this->connectionType->getSalesSlaveConfiguration();
6664
break;
6765
default:
6866
throw new \RuntimeException(

src/Service/Database.php

Lines changed: 33 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,108 +7,73 @@
77

88
namespace Magento\MagentoCloud\Service;
99

10-
use Magento\MagentoCloud\Config\Environment;
10+
use Magento\MagentoCloud\DB\ConnectionInterface;
11+
use Magento\MagentoCloud\DB\Data\ConnectionTypes;
1112

1213
/**
13-
* Returns database service configurations.
14+
* Returns main database service configurations.
1415
*/
1516
class Database implements ServiceInterface
1617
{
17-
const RELATIONSHIP_KEY = 'database';
18-
const RELATIONSHIP_SLAVE_KEY = 'database-slave';
19-
20-
const RELATIONSHIP_QUOTE_KEY = 'database-quote';
21-
const RELATIONSHIP_QUOTE_SLAVE_KEY = 'database-quote-slave';
22-
23-
const RELATIONSHIP_SALES_KEY = 'database-sales';
24-
const RELATIONSHIP_SALES_SLAVE_KEY = 'database-sales-slave';
18+
/**
19+
* @var ConnectionTypes
20+
*/
21+
private $connectionType;
2522

2623
/**
27-
* @var Environment
24+
* @var ConnectionInterface
2825
*/
29-
private $environment;
26+
private $connection;
3027

3128
/**
3229
* @var string
3330
*/
3431
private $version;
3532

3633
/**
37-
* @param Environment $environment
34+
* @param ConnectionTypes $connectionType
35+
* @param ConnectionInterface $connection
3836
*/
39-
public function __construct(Environment $environment)
40-
{
41-
$this->environment = $environment;
37+
public function __construct(
38+
ConnectionTypes $connectionType,
39+
ConnectionInterface $connection
40+
) {
41+
$this->connectionType = $connectionType;
42+
$this->connection = $connection;
4243
}
4344

4445
/**
4546
* @inheritdoc
4647
*/
4748
public function getConfiguration(): array
4849
{
49-
return $this->environment->getRelationship(self::RELATIONSHIP_KEY)[0] ?? [];
50-
}
51-
52-
/**
53-
* Returns service configuration for slave.
54-
*
55-
* @return array
56-
*/
57-
public function getSlaveConfiguration(): array
58-
{
59-
return $this->environment->getRelationship(self::RELATIONSHIP_SLAVE_KEY)[0] ?? [];
60-
}
61-
62-
/**
63-
* Returns configuration for quote service.
64-
*/
65-
public function getQuoteConfiguration(): array
66-
{
67-
return $this->environment->getRelationship(self::RELATIONSHIP_QUOTE_KEY)[0] ?? [];
68-
}
69-
70-
/**
71-
* Returns configuration for quote slave service.
72-
*
73-
* @return array
74-
*/
75-
public function getQuoteSlaveConfiguration(): array
76-
{
77-
return $this->environment->getRelationship(self::RELATIONSHIP_QUOTE_SLAVE_KEY)[0] ?? [];
50+
return $this->connectionType->getConfiguration();
7851
}
7952

8053
/**
81-
* Returns configuration for sales service.
82-
*/
83-
public function getSalesConfiguration(): array
84-
{
85-
return $this->environment->getRelationship(self::RELATIONSHIP_SALES_KEY)[0] ?? [];
86-
}
87-
88-
/**
89-
* Returns configuration for slave sales service.
90-
*
91-
* @return array
92-
*/
93-
public function getSalesSlaveConfiguration(): array
94-
{
95-
return $this->environment->getRelationship(self::RELATIONSHIP_SALES_SLAVE_KEY)[0] ?? [];
96-
}
97-
98-
/**
99-
* Returns version of the service.
54+
* Retrieves MySQL service version whether from relationship configuration
55+
* or using SQL query (for PRO environments)
10056
*
101-
* @return string
57+
* {@inheritDoc}
10258
*/
10359
public function getVersion(): string
10460
{
10561
if ($this->version === null) {
10662
$this->version = '0';
10763

108-
$databaseConfig = $this->getConfiguration();
64+
try {
65+
$databaseConfig = $this->getConfiguration();
66+
67+
if (isset($databaseConfig['type']) && strpos($databaseConfig['type'], ':') !== false) {
68+
$this->version = explode(':', $databaseConfig['type'])[1];
69+
} elseif (!empty($databaseConfig['host'])) {
70+
$rawVersion = $this->connection->selectOne('SELECT VERSION() as version');
71+
preg_match('/^\d+\.\d+/', $rawVersion['version'] ?? '', $matches);
10972

110-
if (isset($databaseConfig['type']) && strpos($databaseConfig['type'], ':') !== false) {
111-
$this->version = explode(':', $databaseConfig['type'])[1];
73+
$this->version = $matches[0] ?? '0';
74+
}
75+
} catch (\Exception $e) {
76+
throw new ServiceException($e->getMessage());
11277
}
11378
}
11479

0 commit comments

Comments
 (0)