Skip to content

PHPORM-319 Make branch 4.x compatible with driver v2 #3347

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions .github/workflows/build-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.*"
Expand Down Expand Up @@ -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"

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ?? '';

Expand Down Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDBServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
}
};
Expand Down