Skip to content

Commit 5c81d5c

Browse files
authored
MCLOUD-7426: New DB connection and resource configuration are broken in 2002.1.4 (#36)
1 parent 1c36aff commit 5c81d5c

File tree

4 files changed

+130
-11
lines changed

4 files changed

+130
-11
lines changed

src/Step/Deploy/InstallUpdate/ConfigUpdate/DbConnection.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ public function __construct(
128128
* In the case when the database was split with the user configuration then sets the flag '.ignore_split_db'
129129
* If the flag '.ignore_split_db' exists, the split process will be ignored
130130
*
131-
* @throws FileSystemException
132-
* @throws ConfigException
133-
* @throws ConfigurationMismatchException
131+
* @inheritDoc
134132
*/
135133
public function execute()
136134
{
@@ -221,7 +219,7 @@ private function updateConnectionsConfig(array $mageSplitConnectionsConfig, bool
221219
}
222220

223221
if ($useSlave && $this->slaveIsAvailable()) {
224-
$slaveConnectionsConfig = $this->getMainConnections($dbConfig[DbConfig::KEY_SLAVE_CONNECTION]);
222+
$slaveConnectionsConfig = $this->filterSplitConnections($dbConfig[DbConfig::KEY_SLAVE_CONNECTION]);
225223
$dbConfig[DbConfig::KEY_SLAVE_CONNECTION] = $slaveConnectionsConfig;
226224
$this->addLoggingAboutSlaveConnection($dbConfig);
227225
} else {
@@ -274,11 +272,11 @@ private function isSameConnection(array $connection1, array $connection2): bool
274272
private function getMainDbConfig(bool $withSlave): array
275273
{
276274
$dbConfig = $this->getDbConfigData();
277-
$dbConfig[DbConfig::KEY_CONNECTION] = $this->getMainConnections($dbConfig[DbConfig::KEY_CONNECTION]);
275+
$dbConfig[DbConfig::KEY_CONNECTION] = $this->filterSplitConnections($dbConfig[DbConfig::KEY_CONNECTION]);
278276

279277
if ($withSlave && $this->slaveIsAvailable()) {
280278
$slaveConnections = $dbConfig[DbConfig::KEY_SLAVE_CONNECTION];
281-
$dbConfig[DbConfig::KEY_SLAVE_CONNECTION] = $this->getMainConnections($slaveConnections);
279+
$dbConfig[DbConfig::KEY_SLAVE_CONNECTION] = $this->filterSplitConnections($slaveConnections);
282280
} else {
283281
unset($dbConfig[DbConfig::KEY_SLAVE_CONNECTION]);
284282
}
@@ -287,24 +285,27 @@ private function getMainDbConfig(bool $withSlave): array
287285
}
288286

289287
/**
290-
* Returns main resource configuration
288+
* Returns resource configuration filtering split resources
291289
*
292290
* @return array
293291
*/
294292
private function getMainResourceConfig(): array
295293
{
296-
return array_intersect_key($this->resourceConfig->get(), array_flip([ResourceConfig::RESOURCE_DEFAULT_SETUP]));
294+
return array_diff_key(
295+
$this->resourceConfig->get(),
296+
array_flip([ResourceConfig::RESOURCE_CHECKOUT, ResourceConfig::RESOURCE_SALES])
297+
);
297298
}
298299

299300
/**
300-
* Returns main connection configurations
301+
* Remove split connections from given connections list
301302
*
302303
* @param array $connections
303304
* @return array
304305
*/
305-
private function getMainConnections(array $connections): array
306+
private function filterSplitConnections(array $connections): array
306307
{
307-
return array_intersect_key($connections, array_flip(DbConfig::MAIN_CONNECTIONS));
308+
return array_diff_key($connections, array_flip(DbConfig::SPLIT_CONNECTIONS));
308309
}
309310

310311
/**

src/Test/Functional/Acceptance/DatabaseConfigurationCest.php

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

88
namespace Magento\MagentoCloud\Test\Functional\Acceptance;
99

10+
use Magento\CloudDocker\Test\Functional\Codeception\Docker;
11+
1012
/**
1113
* This test runs on the latest version of PHP
1214
*
@@ -124,4 +126,37 @@ public function installAndRedeployWithTablePrefix(\CliTester $I)
124126
$I->runDockerComposeCommand('run deploy cloud-post-deploy');
125127
$checkResult($I);
126128
}
129+
130+
/**
131+
* Tests scenario when additional custom db configuration added into
132+
* DATABASE_CONFIGURATION option in .magento.env.yaml
133+
*/
134+
public function customDataBaseConfigurationMagentoEnvYaml(\CliTester $I)
135+
{
136+
$I->copyFileToWorkDir('files/custom_db_configuration/.magento.env.yaml', '.magento.env.yaml');
137+
$I->runEceDockerCommand('build:compose --mode=production');
138+
$I->assertTrue($I->runDockerComposeCommand('run build cloud-build'));
139+
$I->assertTrue($I->startEnvironment());
140+
$I->assertTrue(
141+
$I->runDockerComposeCommand('run deploy cloud-deploy'),
142+
'Installation with additional custom connection failed'
143+
);
144+
$I->runDockerComposeCommand('run deploy cloud-post-deploy');
145+
146+
$config = $this->getConfig($I);
147+
$I->assertArrayHasKey('custom', $config['db']['connection']);
148+
$I->assertArrayHasKey('custom', $config['db']['slave_connection']);
149+
$I->assertArrayHasKey('custom', $config['resource']);
150+
}
151+
152+
/**
153+
* @param \CliTester $I
154+
* @return array
155+
*/
156+
private function getConfig(\CliTester $I): array
157+
{
158+
$destination = sys_get_temp_dir() . '/app/etc/env.php';
159+
$I->assertTrue($I->downloadFromContainer('/app/etc/env.php', $destination, Docker::DEPLOY_CONTAINER));
160+
return require $destination;
161+
}
127162
}

src/Test/Unit/Step/Deploy/InstallUpdate/ConfigUpdate/DbConnectionTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class DbConnectionTest extends TestCase
3535
'username' => 'username',
3636
];
3737

38+
private const CUSTOM_CONNECTION = [
39+
'host' => 'custom.host',
40+
'dbname' => 'custom.dbname',
41+
'password' => 'custom.password',
42+
'username' => 'custom.username',
43+
];
44+
3845
private const CHECKOUT_CONNECTION = [
3946
'host' => 'checkout.host',
4047
'dbname' => 'checkout.dbname',
@@ -73,6 +80,7 @@ class DbConnectionTest extends TestCase
7380
private const RESOURCE_DEFAULT_SETUP = ['connection' => 'default'];
7481
private const RESOURCE_CHECKOUT = ['connection' => 'checkout'];
7582
private const RESOURCE_SALE = ['connection' => 'sales'];
83+
private const RESOURCE_CUSTOM = ['connection' => 'custom'];
7684

7785
/**
7886
* @var DeployInterface|MockObject
@@ -547,4 +555,58 @@ public function testExecuteDisableSlaveConnectionsWhenSplitDbEnabled()
547555

548556
$this->step->execute();
549557
}
558+
559+
/**
560+
* Case when custom connections added in DATABASE_CONFIGURATION.
561+
* Tests that custom resources saves and split db resources are ignored if split db not configured
562+
*/
563+
public function testExecuteSaveCustomConfiguration()
564+
{
565+
$this->dbConfigMock->expects($this->once())
566+
->method('get')
567+
->willReturn([
568+
'connection' => [
569+
'default' => self::DEFAULT_CONNECTION,
570+
'indexer' => self::DEFAULT_CONNECTION,
571+
'custom' => self::CUSTOM_CONNECTION,
572+
'custom2' => self::CUSTOM_CONNECTION,
573+
],
574+
]);
575+
$this->resourceConfigMock->expects($this->once())
576+
->method('get')
577+
->willReturn([
578+
'custom' => self::RESOURCE_CUSTOM,
579+
'custom2' => self::RESOURCE_CUSTOM,
580+
ResourceConfig::RESOURCE_SALES => self::RESOURCE_SALE,
581+
ResourceConfig::RESOURCE_CHECKOUT => self::RESOURCE_CHECKOUT
582+
]);
583+
$this->loggerMock->expects($this->once())
584+
->method('info')
585+
->with('Updating env.php DB connection configuration.');
586+
$this->configReaderMock->expects($this->once())
587+
->method('read')
588+
->willReturn([]);
589+
$this->stageConfigMock->expects($this->exactly(1))
590+
->method('get')
591+
->with(DeployInterface::VAR_MYSQL_USE_SLAVE_CONNECTION)
592+
->willReturn(false);
593+
$this->configWriterMock->expects($this->once())
594+
->method('create')
595+
->with([
596+
'db' => [
597+
'connection' => [
598+
'default' => self::DEFAULT_CONNECTION,
599+
'indexer' => self::DEFAULT_CONNECTION,
600+
'custom' => self::CUSTOM_CONNECTION,
601+
'custom2' => self::CUSTOM_CONNECTION,
602+
]
603+
],
604+
'resource' => [
605+
'custom' => self::RESOURCE_CUSTOM,
606+
'custom2' => self::RESOURCE_CUSTOM,
607+
]
608+
]);
609+
610+
$this->step->execute();
611+
}
550612
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
stage:
2+
deploy:
3+
MYSQL_USE_SLAVE_CONNECTION: true
4+
DATABASE_CONFIGURATION:
5+
_merge: true
6+
connection:
7+
custom:
8+
username: magento2
9+
host: db
10+
dbname: magento2
11+
password: magento2
12+
slave_connection:
13+
custom:
14+
username: magento2
15+
host: db
16+
dbname: magento2slave
17+
password: magento2
18+
RESOURCE_CONFIGURATION:
19+
_merge: true
20+
custom:
21+
connection: custom

0 commit comments

Comments
 (0)