Skip to content

Commit

Permalink
Revert Query\Builder::insertOrIgnore
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Apr 17, 2024
1 parent 24705d0 commit 73227b6
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 35 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 1 addition & 22 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 === []) {
Expand All @@ -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);

Expand Down
9 changes: 8 additions & 1 deletion tests/Eloquent/CallBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use MongoDB\Laravel\Tests\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use RuntimeException;

use function assert;

Expand Down Expand Up @@ -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]
Expand All @@ -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,
Expand Down
11 changes: 0 additions & 11 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down

0 comments on commit 73227b6

Please sign in to comment.