From 486ec44d7a796c4333155bda46ea55d451a0e260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 14 Apr 2025 14:42:15 +0200 Subject: [PATCH 1/2] PHPORM-319 Make branch 4.x compatible with driver v2 (#3347) --- .github/workflows/build-ci.yml | 21 +++++++++++++++++++-- composer.json | 4 ++-- src/Connection.php | 4 ++-- src/Eloquent/Builder.php | 4 ++-- src/MongoDBServiceProvider.php | 2 +- src/Schema/Builder.php | 6 +++--- tests/QueryBuilderTest.php | 6 +++--- 7 files changed, 32 insertions(+), 15 deletions(-) diff --git a/.github/workflows/build-ci.yml b/.github/workflows/build-ci.yml index 45833d579..faed625d0 100644 --- a/.github/workflows/build-ci.yml +++ b/.github/workflows/build-ci.yml @@ -4,11 +4,15 @@ on: push: pull_request: +env: + MONGODB_EXT_V1: mongodb-1.21.0 + MONGODB_EXT_V2: mongodb + jobs: build: runs-on: "${{ matrix.os }}" - name: "PHP ${{ matrix.php }} Laravel ${{ matrix.laravel }} MongoDB ${{ matrix.mongodb }} ${{ matrix.mode }}" + name: "PHP/${{ matrix.php }} Laravel/${{ matrix.laravel }} Driver/${{ matrix.driver }} Server/${{ matrix.mongodb }} ${{ matrix.mode }}" strategy: matrix: @@ -19,13 +23,18 @@ jobs: - "5.0" - "6.0" - "7.0" + - "8.0" php: - "8.1" - "8.2" - "8.3" + - "8.4" laravel: - "10.*" - "11.*" + driver: + - 1 + - 2 include: - php: "8.1" laravel: "10.*" @@ -59,11 +68,19 @@ jobs: if [ "${{ matrix.mongodb }}" = "4.4" ]; then MONGOSH_BIN="mongo"; else MONGOSH_BIN="mongosh"; fi docker exec --tty mongodb $MONGOSH_BIN 127.0.0.1:27017 --eval "db.runCommand({ serverStatus: 1 })" + - name: Setup cache environment + id: extcache + uses: shivammathur/cache-extensions@v1 + with: + php-version: ${{ matrix.php }} + extensions: ${{ matrix.driver == 1 && env.MONGODB_EXT_V1 || env.MONGODB_EXT_V2 }} + key: "extcache-v${{ matrix.driver }}" + - name: "Installing php" uses: "shivammathur/setup-php@v2" with: php-version: ${{ matrix.php }} - extensions: "curl,mbstring,xdebug" + extensions: "curl,mbstring,xdebug,${{ matrix.driver == 1 && env.MONGODB_EXT_V1 || env.MONGODB_EXT_V2 }}" coverage: "xdebug" tools: "composer" diff --git a/composer.json b/composer.json index af060bb3c..b6c79b3f9 100644 --- a/composer.json +++ b/composer.json @@ -23,14 +23,14 @@ "license": "MIT", "require": { "php": "^8.1", - "ext-mongodb": "^1.15", + "ext-mongodb": "^1.21|^2", "composer-runtime-api": "^2.0.0", "illuminate/cache": "^10.36|^11", "illuminate/container": "^10.0|^11", "illuminate/database": "^10.30|^11", "illuminate/events": "^10.0|^11", "illuminate/support": "^10.0|^11", - "mongodb/mongodb": "^1.15" + "mongodb/mongodb": "^1.21|^2" }, "require-dev": { "mongodb/builder": "^0.2", diff --git a/src/Connection.php b/src/Connection.php index a0affa56a..35a27378a 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -66,7 +66,7 @@ public function __construct(array $config) $this->connection = $this->createConnection($dsn, $config, $options); // Select database - $this->db = $this->connection->selectDatabase($this->getDefaultDatabaseName($dsn, $config)); + $this->db = $this->connection->getDatabase($this->getDefaultDatabaseName($dsn, $config)); $this->tablePrefix = $config['prefix'] ?? ''; @@ -117,7 +117,7 @@ public function table($table, $as = null) */ public function getCollection($name) { - return new Collection($this, $this->db->selectCollection($this->tablePrefix . $name)); + return new Collection($this, $this->db->getCollection($this->tablePrefix . $name)); } /** @inheritdoc */ diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index da96b64f1..c36562425 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use MongoDB\Driver\Cursor; -use MongoDB\Driver\Exception\WriteException; +use MongoDB\Driver\Exception\BulkWriteException; use MongoDB\Laravel\Connection; use MongoDB\Laravel\Helpers\QueriesRelationships; use MongoDB\Laravel\Query\AggregationBuilder; @@ -222,7 +222,7 @@ public function createOrFirst(array $attributes = [], array $values = []) try { return $this->create(array_merge($attributes, $values)); - } catch (WriteException $e) { + } catch (BulkWriteException $e) { if ($e->getCode() === self::DUPLICATE_KEY_ERROR) { return $this->where($attributes)->first() ?? throw $e; } diff --git a/src/MongoDBServiceProvider.php b/src/MongoDBServiceProvider.php index 0932048c9..c385c5dd4 100644 --- a/src/MongoDBServiceProvider.php +++ b/src/MongoDBServiceProvider.php @@ -108,7 +108,7 @@ private function registerFlysystemAdapter(): void } $bucket = $connection->getMongoClient() - ->selectDatabase($config['database'] ?? $connection->getDatabaseName()) + ->getDatabase($config['database'] ?? $connection->getDatabaseName()) ->selectGridFSBucket(['bucketName' => $config['bucket'] ?? 'fs', 'disableMD5' => true]); } diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index e31a1efe1..30127d339 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -143,7 +143,7 @@ public function getTables() $collections = []; foreach ($db->listCollectionNames() as $collectionName) { - $stats = $db->selectCollection($collectionName)->aggregate([ + $stats = $db->getCollection($collectionName)->aggregate([ ['$collStats' => ['storageStats' => ['scale' => 1]]], ['$project' => ['storageStats.totalSize' => 1]], ])->toArray(); @@ -176,7 +176,7 @@ public function getTableListing() public function getColumns($table) { - $stats = $this->connection->getMongoDB()->selectCollection($table)->aggregate([ + $stats = $this->connection->getMongoDB()->getCollection($table)->aggregate([ // Sample 1,000 documents to get a representative sample of the collection ['$sample' => ['size' => 1_000]], // Convert each document to an array of fields @@ -224,7 +224,7 @@ public function getColumns($table) public function getIndexes($table) { - $indexes = $this->connection->getMongoDB()->selectCollection($table)->listIndexes(); + $indexes = $this->connection->getMongoDB()->getCollection($table)->listIndexes(); $indexList = []; foreach ($indexes as $index) { diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 1a87ee430..7975518bc 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -156,7 +156,7 @@ public function testFindWithTimeout() $id = DB::table('users')->insertGetId(['name' => 'John Doe']); $subscriber = new class implements CommandSubscriber { - public function commandStarted(CommandStartedEvent $event) + public function commandStarted(CommandStartedEvent $event): void { if ($event->getCommandName() !== 'find') { return; @@ -166,11 +166,11 @@ public function commandStarted(CommandStartedEvent $event) Assert::assertSame(1000, $event->getCommand()->maxTimeMS); } - public function commandFailed(CommandFailedEvent $event) + public function commandFailed(CommandFailedEvent $event): void { } - public function commandSucceeded(CommandSucceededEvent $event) + public function commandSucceeded(CommandSucceededEvent $event): void { } }; From 731f858e0944370cc97fc8d2aa67413c7634d5f3 Mon Sep 17 00:00:00 2001 From: Stephanie <52582720+stephmarie17@users.noreply.github.com> Date: Wed, 16 Apr 2025 10:02:51 -0700 Subject: [PATCH 2/2] DOCSP-49294 Add MDB Extension section to compat page (#3357) --- docs/compatibility.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/compatibility.txt b/docs/compatibility.txt index e02bda581..79cf7b88a 100644 --- a/docs/compatibility.txt +++ b/docs/compatibility.txt @@ -28,3 +28,15 @@ the {+odm-short+} that you can use together. To find compatibility information for unmaintained versions of the {+odm-short+}, see `Laravel Version Compatibility <{+mongodb-laravel-gh+}/blob/3.9/README.md#installation>`__ on GitHub. + +PHP Driver Compatibility +------------------------ + +To use {+odm-long+} v4.9 or later, you must install v1.21 of the +{+php-library+} and {+php-extension+}. + +.. important:: {+php-extension+} v2.0 Compatibility + + If you upgrade the {+php-extension+} to v2.0, you must also upgrade + {+odm-long+} to v4.9.0. {+odm-long+} v4.9.0 still supports v1.x + versions of the extension. \ No newline at end of file