diff --git a/CHANGELOG.md b/CHANGELOG.md index ecb58bfb1..f653604ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,6 @@ All notable changes to this project will be documented in this file. * New aggregation pipeline builder by @GromNaN in [#2738](https://github.com/mongodb/laravel-mongodb/pull/2738) * Drop support for Composer 1.x by @GromNaN in [#2784](https://github.com/mongodb/laravel-mongodb/pull/2784) * Fix `artisan query:retry` command by @GromNaN in [#2838](https://github.com/mongodb/laravel-mongodb/pull/2838) -* Implement `MongoDB\Laravel\Query\Builder::insertOrIgnore()` to ignore duplicate values by @GromNaN in [#2877](https://github.com/mongodb/laravel-mongodb/pull/2877) * Add `mongodb` cache and lock drivers by @GromNaN in [#2877](https://github.com/mongodb/laravel-mongodb/pull/2877) ## [4.2.0] - 2024-03-14 diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 5d2a7e2a6..89faa4b17 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -23,7 +23,6 @@ use MongoDB\BSON\UTCDateTime; use MongoDB\Builder\Stage\FluentFactoryTrait; use MongoDB\Driver\Cursor; -use MongoDB\Driver\Exception\BulkWriteException; use Override; use RuntimeException; @@ -660,26 +659,6 @@ public function whereBetween($column, iterable $values, $boolean = 'and', $not = /** @inheritdoc */ public function insert(array $values) - { - return $this->performInsert($values); - } - - /** @inheritdoc */ - public function insertOrIgnore(array $values) - { - try { - return $this->performInsert($values, ['ordered' => false]); - } catch (BulkWriteException $exception) { - // Ignore "duplicate key error" - if ($exception->getCode() !== 11000) { - throw $exception; - } - - return $exception->getWriteResult()->isAcknowledged(); - } - } - - private function performInsert(array $values, array $options = []) { // Allow empty insert batch for consistency with Eloquent SQL if ($values === []) { @@ -703,7 +682,7 @@ private function performInsert(array $values, array $options = []) $values = [$values]; } - $options += $this->inheritConnectionOptions(); + $options = $this->inheritConnectionOptions(); $result = $this->collection->insertMany($values, $options); diff --git a/tests/Eloquent/CallBuilderTest.php b/tests/Eloquent/CallBuilderTest.php index 0d0c01582..fa4cb4580 100644 --- a/tests/Eloquent/CallBuilderTest.php +++ b/tests/Eloquent/CallBuilderTest.php @@ -12,6 +12,7 @@ use MongoDB\Laravel\Tests\TestCase; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; +use RuntimeException; use function assert; @@ -51,7 +52,6 @@ public static function provideFunctionNames(): Generator yield 'push' => ['push', Builder::class, ['name']]; yield 'raw' => ['raw', Builder::class]; yield 'sum' => ['sum', Builder::class, ['name']]; - yield 'insert or ignore' => ['insertOrIgnore', Builder::class, [['user' => 'foo']]]; } #[Test] @@ -69,6 +69,13 @@ public function callingUnsupportedMethodThrowsAnException(string $method, string public static function provideUnsupportedMethods(): Generator { + yield 'insert or ignore' => [ + 'insertOrIgnore', + RuntimeException::class, + 'This database engine does not support inserting while ignoring errors', + [['name' => 'Jane']], + ]; + yield 'insert using' => [ 'insertUsing', BadMethodCallException::class, diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 0f0d3cf4f..4320e6a54 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -142,17 +142,6 @@ public function testBatchInsert() $this->assertIsArray($users[0]['tags']); } - public function testInsertOrIgnoreMethod() - { - $builder = DB::collection('users'); - // Expect "duplicate key error" on _id field to be ignored - $id = new ObjectId(); - $this->assertTrue($builder->insertOrIgnore(['_id' => $id])); - $this->assertTrue($builder->insertOrIgnore(['_id' => $id])); - $this->assertTrue($builder->insertOrIgnore([['_id' => $id], ['foo' => 'bar']])); - $this->assertSame(2, $builder->count()); - } - public function testFind() { $id = DB::collection('users')->insertGetId(['name' => 'John Doe']);