aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/DB/Connection.php
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2023-12-03 09:14:55 +0100
committerJulius Härtl <jus@bitgrid.net>2023-12-16 20:27:37 +0100
commit79c4986354da2c7dd102de21174114e7687daf98 (patch)
tree1f8ace9f815c53e5b79f42aafd963e635d746b33 /lib/private/DB/Connection.php
parent9c4467461c2bc08e621c1e8a7d88389c387815c5 (diff)
downloadnextcloud-server-79c4986354da2c7dd102de21174114e7687daf98.tar.gz
nextcloud-server-79c4986354da2c7dd102de21174114e7687daf98.zip
enh: Implement PrimaryReadReplicaConnection
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'lib/private/DB/Connection.php')
-rw-r--r--lib/private/DB/Connection.php19
1 files changed, 17 insertions, 2 deletions
diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php
index 6e2724ca5ab..e3fda3e464f 100644
--- a/lib/private/DB/Connection.php
+++ b/lib/private/DB/Connection.php
@@ -38,6 +38,7 @@ namespace OC\DB;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Configuration;
+use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\MySQLPlatform;
@@ -55,7 +56,7 @@ use OCP\PreConditionNotMetException;
use OCP\Profiler\IProfiler;
use Psr\Log\LoggerInterface;
-class Connection extends \Doctrine\DBAL\Connection {
+class Connection extends PrimaryReadReplicaConnection {
/** @var string */
protected $tablePrefix;
@@ -119,7 +120,7 @@ class Connection extends \Doctrine\DBAL\Connection {
/**
* @throws Exception
*/
- public function connect() {
+ public function connect($connectionName = null) {
try {
if ($this->_conn) {
/** @psalm-suppress InternalMethod */
@@ -302,6 +303,10 @@ class Connection extends \Doctrine\DBAL\Connection {
$prefix .= \OC::$server->get(IRequestId::class)->getId() . "\t";
}
+ // FIXME: Improve to log the actual target db host
+ $isPrimary = $this->connections['primary'] === $this->_conn;
+ $prefix .= ' ' . ($isPrimary === true ? 'primary' : 'replica') . ' ';
+
file_put_contents(
$this->systemConfig->getValue('query_log_file', ''),
$prefix . $sql . "\n",
@@ -603,4 +608,14 @@ class Connection extends \Doctrine\DBAL\Connection {
return new Migrator($this, $config, $dispatcher);
}
}
+
+ protected function performConnect(?string $connectionName = null): bool {
+ $before = $this->isConnectedToPrimary();
+ $result = parent::performConnect($connectionName);
+ $after = $this->isConnectedToPrimary();
+ if (!$before && $after) {
+ $this->logger->debug('Switched to primary database', ['exception' => new \Exception()]);
+ }
+ return $result;
+ }
}