Skip to content

Commit c3f500c

Browse files
committed
PHPORM-319 Make branch 4.x compatible with driver v2
1 parent 453139a commit c3f500c

File tree

7 files changed

+32
-15
lines changed

7 files changed

+32
-15
lines changed

.github/workflows/build-ci.yml

+19-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ on:
44
push:
55
pull_request:
66

7+
env:
8+
MONGODB_EXT_V1: mongodb-1.21.0
9+
MONGODB_EXT_V2: stable
10+
711
jobs:
812
build:
913
runs-on: "${{ matrix.os }}"
1014

11-
name: "PHP ${{ matrix.php }} Laravel ${{ matrix.laravel }} MongoDB ${{ matrix.mongodb }} ${{ matrix.mode }}"
15+
name: "PHP/${{ matrix.php }} Laravel/${{ matrix.laravel }} Driver/${{ matrix.driver }} Server/${{ matrix.mongodb }} ${{ matrix.mode }}"
1216

1317
strategy:
1418
matrix:
@@ -19,13 +23,18 @@ jobs:
1923
- "5.0"
2024
- "6.0"
2125
- "7.0"
26+
- "8.0"
2227
php:
2328
- "8.1"
2429
- "8.2"
2530
- "8.3"
31+
- "8.4"
2632
laravel:
2733
- "10.*"
2834
- "11.*"
35+
driver:
36+
- 1
37+
- 2
2938
include:
3039
- php: "8.1"
3140
laravel: "10.*"
@@ -59,11 +68,19 @@ jobs:
5968
if [ "${{ matrix.mongodb }}" = "4.4" ]; then MONGOSH_BIN="mongo"; else MONGOSH_BIN="mongosh"; fi
6069
docker exec --tty mongodb $MONGOSH_BIN 127.0.0.1:27017 --eval "db.runCommand({ serverStatus: 1 })"
6170
71+
- name: Setup cache environment
72+
id: extcache
73+
uses: shivammathur/cache-extensions@v1
74+
with:
75+
php-version: ${{ matrix.php }}
76+
extensions: ${{ matrix.driver == 1 && env.MONGODB_EXT_V1 || env.MONGODB_EXT_V2 }}
77+
key: "extcache-v${{ matrix.driver }}"
78+
6279
- name: "Installing php"
6380
uses: "shivammathur/setup-php@v2"
6481
with:
6582
php-version: ${{ matrix.php }}
66-
extensions: "curl,mbstring,xdebug"
83+
extensions: "curl,mbstring,xdebug,${{ matrix.driver == 1 && env.MONGODB_EXT_V1 || env.MONGODB_EXT_V2 }}"
6784
coverage: "xdebug"
6885
tools: "composer"
6986

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
"license": "MIT",
2424
"require": {
2525
"php": "^8.1",
26-
"ext-mongodb": "^1.15",
26+
"ext-mongodb": "^1.21|^2",
2727
"composer-runtime-api": "^2.0.0",
2828
"illuminate/cache": "^10.36|^11",
2929
"illuminate/container": "^10.0|^11",
3030
"illuminate/database": "^10.30|^11",
3131
"illuminate/events": "^10.0|^11",
3232
"illuminate/support": "^10.0|^11",
33-
"mongodb/mongodb": "^1.15"
33+
"mongodb/mongodb": "^1.21|^2"
3434
},
3535
"require-dev": {
3636
"mongodb/builder": "^0.2",

src/Connection.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __construct(array $config)
6666
$this->connection = $this->createConnection($dsn, $config, $options);
6767

6868
// Select database
69-
$this->db = $this->connection->selectDatabase($this->getDefaultDatabaseName($dsn, $config));
69+
$this->db = $this->connection->getDatabase($this->getDefaultDatabaseName($dsn, $config));
7070

7171
$this->tablePrefix = $config['prefix'] ?? '';
7272

@@ -117,7 +117,7 @@ public function table($table, $as = null)
117117
*/
118118
public function getCollection($name)
119119
{
120-
return new Collection($this, $this->db->selectCollection($this->tablePrefix . $name));
120+
return new Collection($this, $this->db->getCollection($this->tablePrefix . $name));
121121
}
122122

123123
/** @inheritdoc */

src/Eloquent/Builder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
88
use MongoDB\Driver\Cursor;
9-
use MongoDB\Driver\Exception\WriteException;
9+
use MongoDB\Driver\Exception\BulkWriteException;
1010
use MongoDB\Laravel\Connection;
1111
use MongoDB\Laravel\Helpers\QueriesRelationships;
1212
use MongoDB\Laravel\Query\AggregationBuilder;
@@ -222,7 +222,7 @@ public function createOrFirst(array $attributes = [], array $values = [])
222222

223223
try {
224224
return $this->create(array_merge($attributes, $values));
225-
} catch (WriteException $e) {
225+
} catch (BulkWriteException $e) {
226226
if ($e->getCode() === self::DUPLICATE_KEY_ERROR) {
227227
return $this->where($attributes)->first() ?? throw $e;
228228
}

src/MongoDBServiceProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private function registerFlysystemAdapter(): void
108108
}
109109

110110
$bucket = $connection->getMongoClient()
111-
->selectDatabase($config['database'] ?? $connection->getDatabaseName())
111+
->getDatabase($config['database'] ?? $connection->getDatabaseName())
112112
->selectGridFSBucket(['bucketName' => $config['bucket'] ?? 'fs', 'disableMD5' => true]);
113113
}
114114

src/Schema/Builder.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function getTables()
143143
$collections = [];
144144

145145
foreach ($db->listCollectionNames() as $collectionName) {
146-
$stats = $db->selectCollection($collectionName)->aggregate([
146+
$stats = $db->getCollection($collectionName)->aggregate([
147147
['$collStats' => ['storageStats' => ['scale' => 1]]],
148148
['$project' => ['storageStats.totalSize' => 1]],
149149
])->toArray();
@@ -176,7 +176,7 @@ public function getTableListing()
176176

177177
public function getColumns($table)
178178
{
179-
$stats = $this->connection->getMongoDB()->selectCollection($table)->aggregate([
179+
$stats = $this->connection->getMongoDB()->getCollection($table)->aggregate([
180180
// Sample 1,000 documents to get a representative sample of the collection
181181
['$sample' => ['size' => 1_000]],
182182
// Convert each document to an array of fields
@@ -224,7 +224,7 @@ public function getColumns($table)
224224

225225
public function getIndexes($table)
226226
{
227-
$indexes = $this->connection->getMongoDB()->selectCollection($table)->listIndexes();
227+
$indexes = $this->connection->getMongoDB()->getCollection($table)->listIndexes();
228228

229229
$indexList = [];
230230
foreach ($indexes as $index) {

tests/QueryBuilderTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function testFindWithTimeout()
156156
$id = DB::table('users')->insertGetId(['name' => 'John Doe']);
157157

158158
$subscriber = new class implements CommandSubscriber {
159-
public function commandStarted(CommandStartedEvent $event)
159+
public function commandStarted(CommandStartedEvent $event): void
160160
{
161161
if ($event->getCommandName() !== 'find') {
162162
return;
@@ -166,11 +166,11 @@ public function commandStarted(CommandStartedEvent $event)
166166
Assert::assertSame(1000, $event->getCommand()->maxTimeMS);
167167
}
168168

169-
public function commandFailed(CommandFailedEvent $event)
169+
public function commandFailed(CommandFailedEvent $event): void
170170
{
171171
}
172172

173-
public function commandSucceeded(CommandSucceededEvent $event)
173+
public function commandSucceeded(CommandSucceededEvent $event): void
174174
{
175175
}
176176
};

0 commit comments

Comments
 (0)