|
7 | 7 |
|
8 | 8 | namespace Magento\MagentoCloud\Service; |
9 | 9 |
|
10 | | -use Magento\MagentoCloud\Config\Environment; |
| 10 | +use Magento\MagentoCloud\DB\ConnectionInterface; |
| 11 | +use Magento\MagentoCloud\DB\Data\ConnectionTypes; |
11 | 12 |
|
12 | 13 | /** |
13 | | - * Returns database service configurations. |
| 14 | + * Returns main database service configurations. |
14 | 15 | */ |
15 | 16 | class Database implements ServiceInterface |
16 | 17 | { |
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; |
25 | 22 |
|
26 | 23 | /** |
27 | | - * @var Environment |
| 24 | + * @var ConnectionInterface |
28 | 25 | */ |
29 | | - private $environment; |
| 26 | + private $connection; |
30 | 27 |
|
31 | 28 | /** |
32 | 29 | * @var string |
33 | 30 | */ |
34 | 31 | private $version; |
35 | 32 |
|
36 | 33 | /** |
37 | | - * @param Environment $environment |
| 34 | + * @param ConnectionTypes $connectionType |
| 35 | + * @param ConnectionInterface $connection |
38 | 36 | */ |
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; |
42 | 43 | } |
43 | 44 |
|
44 | 45 | /** |
45 | 46 | * @inheritdoc |
46 | 47 | */ |
47 | 48 | public function getConfiguration(): array |
48 | 49 | { |
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(); |
78 | 51 | } |
79 | 52 |
|
80 | 53 | /** |
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) |
100 | 56 | * |
101 | | - * @return string |
| 57 | + * {@inheritDoc} |
102 | 58 | */ |
103 | 59 | public function getVersion(): string |
104 | 60 | { |
105 | 61 | if ($this->version === null) { |
106 | 62 | $this->version = '0'; |
107 | 63 |
|
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); |
109 | 72 |
|
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()); |
112 | 77 | } |
113 | 78 | } |
114 | 79 |
|
|
0 commit comments