Skip to content

Commit d9e3c07

Browse files
committed
Clone and add config to turn off split
1 parent 6f4b10b commit d9e3c07

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

Adapter/Pdo/CloneMysql.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace CodeFarm\SplitDb\Adapter\Pdo;
4+
5+
class CloneMysql extends \Magento\Framework\DB\Adapter\Pdo\Mysql
6+
{
7+
8+
}

Adapter/Pdo/Mysql.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,21 @@ public function __construct(
3333
unset($config['excluded_areas']);
3434
}
3535

36-
if(isset($config['slaves'])){
36+
if(isset($config['slaves']) && isset($config['is_split'])){
3737
// keep the same slave throughout the request
3838
$slaveIndex = rand(0, (count($config['slaves']) - 1));
3939
$slaveConfig = $config['slaves'][$slaveIndex];
4040
unset($config['slaves']);
41-
$slaveConfig = array_merge(
42-
$config,
43-
$slaveConfig
44-
);
45-
$this->readConnection = ObjectManager::getInstance()->create(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, [
41+
if($config['is_split']){
42+
$slaveConfig = array_merge(
43+
$config,
44+
$slaveConfig
45+
);
46+
}else{
47+
$slaveConfig = $config;
48+
}
49+
50+
$this->readConnection = ObjectManager::getInstance()->create(CloneMysql::class, [
4651
'string' => $string,
4752
'dateTime' => $dateTime,
4853
'logger' => $logger,
@@ -52,7 +57,7 @@ public function __construct(
5257
]);
5358
}else{
5459
// create a read connection with the same credentials as the writer
55-
$this->readConnection = ObjectManager::getInstance()->create(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, [
60+
$this->readConnection = ObjectManager::getInstance()->create(CloneMysql::class, [
5661
'string' => $string,
5762
'dateTime' => $dateTime,
5863
'logger' => $logger,
@@ -106,7 +111,7 @@ protected function canUseReader($sql)
106111
'GET_LOCK'
107112
];
108113
foreach($writerSqlIdentifiers as $writerSqlIdentifier){
109-
if(stripos($sql, $writerSqlIdentifier) !== false){
114+
if(stripos(substr($sql, 0 , 20), $writerSqlIdentifier) !== false){
110115
return false;
111116
}
112117
}

Console/Command/ChangeDbMode.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
3636
{
3737
if ($mode = $input->getOption(self::MODE)) {
3838
if($mode == 'default' || $mode == 'split'){
39+
$isActive = ($mode == 'split');
3940
$write = ObjectManager::getInstance()->create(\Magento\Framework\App\DeploymentConfig\Writer::class);
4041
$write->saveConfig([
4142
ConfigFilePool::APP_ENV => [
42-
'DB_MODE' => $mode
43+
'db' => [
44+
'connection' => [
45+
'default' => [
46+
'is_split' => $isActive
47+
]
48+
]
49+
]
4350
]
4451
]);
4552
} else {

0 commit comments

Comments
 (0)