From 3d1030a4fa73658236dd9d90f38cee21484fc25e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 15 Jul 2024 12:39:30 +0200 Subject: [PATCH 1/2] Implement Connection::getServerVersion --- CHANGELOG.md | 4 ++++ src/Connection.php | 5 +++++ tests/ConnectionTest.php | 6 ++++++ 3 files changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f2a2f9e5..532d81a81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All notable changes to this project will be documented in this file. +## [4.9.0] - coming soon + +* Add `Connection::getServerVersion()` by @GromNaN in [#3043](https://github.com/mongodb/laravel-mongodb/pull/3043) + ## [4.6.0] - 2024-07-09 * Add `DocumentModel` trait to use any 3rd party model with MongoDB @GromNaN in [#2580](https://github.com/mongodb/laravel-mongodb/pull/2580) diff --git a/src/Connection.php b/src/Connection.php index 2ce5324ee..343c0ca21 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -327,6 +327,11 @@ public function __call($method, $parameters) return $this->db->$method(...$parameters); } + public function getServerVersion(): string + { + return $this->db->command(['buildInfo' => 1])->toArray()[0]['version']; + } + private static function getVersion(): string { return self::$version ?? self::lookupVersion(); diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 586452109..ef0b746c3 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -299,4 +299,10 @@ public function testPingMethod() $instance = new Connection($config); $instance->ping(); } + + public function testServerVersion() + { + $version = DB::connection('mongodb')->getServerVersion(); + $this->assertIsString($version); + } } From c79c077e25e6c5b370a9d86047863f0dd8b9b153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 15 Jul 2024 13:48:39 +0200 Subject: [PATCH 2/2] Add comment --- src/Connection.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Connection.php b/src/Connection.php index 343c0ca21..685e509b6 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -327,6 +327,12 @@ public function __call($method, $parameters) return $this->db->$method(...$parameters); } + /** + * Return the server version of one of the MongoDB servers: primary for + * replica sets and standalone, and the selected server for sharded clusters. + * + * @internal + */ public function getServerVersion(): string { return $this->db->command(['buildInfo' => 1])->toArray()[0]['version'];