summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Molakvoæ <skjnldsv@users.noreply.github.com>2024-06-07 11:05:11 +0200
committerGitHub <noreply@github.com>2024-06-07 11:05:11 +0200
commit5707accfb2be56ca43f4fc8f5e20dd3d1c8ae109 (patch)
tree0824ee2e21532d708cd77f716f94b56edcf53f28
parentfe3103321b28e3c20b3164d998df8a5fc05f28ec (diff)
parent2530337d36ad9c9ffe35bd0878dd19a01095b723 (diff)
downloadnextcloud-server-5707accfb2be56ca43f4fc8f5e20dd3d1c8ae109.tar.gz
nextcloud-server-5707accfb2be56ca43f4fc8f5e20dd3d1c8ae109.zip
Merge pull request #44957 from nextcloud/backport/44884/stable27
-rw-r--r--config/config.sample.php7
-rw-r--r--lib/private/DB/Connection.php29
2 files changed, 28 insertions, 8 deletions
diff --git a/config/config.sample.php b/config/config.sample.php
index 4560dfa762f..cf4cc85ea5c 100644
--- a/config/config.sample.php
+++ b/config/config.sample.php
@@ -152,6 +152,13 @@ $CONFIG = [
'dbpersistent' => '',
/**
+ * Add request id to the database query in a comment.
+ *
+ * This can be enabled to assist in mapping database logs to Nextcloud logs.
+ */
+'db.log_request_id' => false,
+
+/**
* Indicates whether the Nextcloud instance was installed successfully; ``true``
* indicates a successful installation, and ``false`` indicates an unsuccessful
* installation.
diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php
index 85c6a72dfdb..6b19863b56a 100644
--- a/lib/private/DB/Connection.php
+++ b/lib/private/DB/Connection.php
@@ -54,6 +54,7 @@ use OCP\PreConditionNotMetException;
use OCP\Profiler\IProfiler;
use OC\DB\QueryBuilder\QueryBuilder;
use OC\SystemConfig;
+use OCP\Server;
use Psr\Log\LoggerInterface;
class Connection extends \Doctrine\DBAL\Connection {
@@ -79,6 +80,9 @@ class Connection extends \Doctrine\DBAL\Connection {
/** @var DbDataCollector|null */
protected $dbDataCollector = null;
+ protected bool $logRequestId;
+ protected string $requestId;
+
/**
* Initializes a new instance of the Connection class.
*
@@ -106,6 +110,9 @@ class Connection extends \Doctrine\DBAL\Connection {
$this->systemConfig = \OC::$server->getSystemConfig();
$this->logger = \OC::$server->get(LoggerInterface::class);
+ $this->logRequestId = $this->systemConfig->getValue('db.log_request_id', false);
+ $this->requestId = Server::get(IRequestId::class)->getId();
+
/** @var \OCP\Profiler\IProfiler */
$profiler = \OC::$server->get(IProfiler::class);
if ($profiler->isEnabled()) {
@@ -233,8 +240,7 @@ class Connection extends \Doctrine\DBAL\Connection {
$platform = $this->getDatabasePlatform();
$statement = $platform->modifyLimitQuery($statement, $limit, $offset);
}
- $statement = $this->replaceTablePrefix($statement);
- $statement = $this->adapter->fixupStatement($statement);
+ $statement = $this->finishQuery($statement);
return parent::prepare($statement);
}
@@ -255,8 +261,7 @@ class Connection extends \Doctrine\DBAL\Connection {
* @throws \Doctrine\DBAL\Exception
*/
public function executeQuery(string $sql, array $params = [], $types = [], QueryCacheProfile $qcp = null): Result {
- $sql = $this->replaceTablePrefix($sql);
- $sql = $this->adapter->fixupStatement($sql);
+ $sql = $this->finishQuery($sql);
$this->queriesExecuted++;
$this->logQueryToFile($sql);
return parent::executeQuery($sql, $params, $types, $qcp);
@@ -266,8 +271,7 @@ class Connection extends \Doctrine\DBAL\Connection {
* @throws Exception
*/
public function executeUpdate(string $sql, array $params = [], array $types = []): int {
- $sql = $this->replaceTablePrefix($sql);
- $sql = $this->adapter->fixupStatement($sql);
+ $sql = $this->finishQuery($sql);
$this->queriesExecuted++;
$this->logQueryToFile($sql);
return parent::executeUpdate($sql, $params, $types);
@@ -288,8 +292,7 @@ class Connection extends \Doctrine\DBAL\Connection {
* @throws \Doctrine\DBAL\Exception
*/
public function executeStatement($sql, array $params = [], array $types = []): int {
- $sql = $this->replaceTablePrefix($sql);
- $sql = $this->adapter->fixupStatement($sql);
+ $sql = $this->finishQuery($sql);
$this->queriesExecuted++;
$this->logQueryToFile($sql);
return (int)parent::executeStatement($sql, $params, $types);
@@ -516,6 +519,16 @@ class Connection extends \Doctrine\DBAL\Connection {
return $schema->tablesExist([$table]);
}
+ protected function finishQuery(string $statement): string {
+ $statement = $this->replaceTablePrefix($statement);
+ $statement = $this->adapter->fixupStatement($statement);
+ if ($this->logRequestId) {
+ return $statement . " /* reqid: " . $this->requestId . " */";
+ } else {
+ return $statement;
+ }
+ }
+
// internal use
/**
* @param string $statement