Skip to content

Commit 56a986d

Browse files
committed
Revert Query\Builder::insertOrIgnore
1 parent 24705d0 commit 56a986d

File tree

3 files changed

+9
-34
lines changed

3 files changed

+9
-34
lines changed

src/Query/Builder.php

+1-22
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use MongoDB\BSON\UTCDateTime;
2424
use MongoDB\Builder\Stage\FluentFactoryTrait;
2525
use MongoDB\Driver\Cursor;
26-
use MongoDB\Driver\Exception\BulkWriteException;
2726
use Override;
2827
use RuntimeException;
2928

@@ -660,26 +659,6 @@ public function whereBetween($column, iterable $values, $boolean = 'and', $not =
660659

661660
/** @inheritdoc */
662661
public function insert(array $values)
663-
{
664-
return $this->performInsert($values);
665-
}
666-
667-
/** @inheritdoc */
668-
public function insertOrIgnore(array $values)
669-
{
670-
try {
671-
return $this->performInsert($values, ['ordered' => false]);
672-
} catch (BulkWriteException $exception) {
673-
// Ignore "duplicate key error"
674-
if ($exception->getCode() !== 11000) {
675-
throw $exception;
676-
}
677-
678-
return $exception->getWriteResult()->isAcknowledged();
679-
}
680-
}
681-
682-
private function performInsert(array $values, array $options = [])
683662
{
684663
// Allow empty insert batch for consistency with Eloquent SQL
685664
if ($values === []) {
@@ -703,7 +682,7 @@ private function performInsert(array $values, array $options = [])
703682
$values = [$values];
704683
}
705684

706-
$options += $this->inheritConnectionOptions();
685+
$options = $this->inheritConnectionOptions();
707686

708687
$result = $this->collection->insertMany($values, $options);
709688

tests/Eloquent/CallBuilderTest.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use MongoDB\Laravel\Tests\TestCase;
1313
use PHPUnit\Framework\Attributes\DataProvider;
1414
use PHPUnit\Framework\Attributes\Test;
15+
use RuntimeException;
1516

1617
use function assert;
1718

@@ -51,7 +52,6 @@ public static function provideFunctionNames(): Generator
5152
yield 'push' => ['push', Builder::class, ['name']];
5253
yield 'raw' => ['raw', Builder::class];
5354
yield 'sum' => ['sum', Builder::class, ['name']];
54-
yield 'insert or ignore' => ['insertOrIgnore', Builder::class, [['user' => 'foo']]];
5555
}
5656

5757
#[Test]
@@ -69,6 +69,13 @@ public function callingUnsupportedMethodThrowsAnException(string $method, string
6969

7070
public static function provideUnsupportedMethods(): Generator
7171
{
72+
yield 'insert or ignore' => [
73+
'insertOrIgnore',
74+
RuntimeException::class,
75+
'This database engine does not support inserting while ignoring errors',
76+
[['name' => 'Jane']],
77+
];
78+
7279
yield 'insert using' => [
7380
'insertUsing',
7481
BadMethodCallException::class,

tests/QueryBuilderTest.php

-11
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,6 @@ public function testBatchInsert()
142142
$this->assertIsArray($users[0]['tags']);
143143
}
144144

145-
public function testInsertOrIgnoreMethod()
146-
{
147-
$builder = DB::collection('users');
148-
// Expect "duplicate key error" on _id field to be ignored
149-
$id = new ObjectId();
150-
$this->assertTrue($builder->insertOrIgnore(['_id' => $id]));
151-
$this->assertTrue($builder->insertOrIgnore(['_id' => $id]));
152-
$this->assertTrue($builder->insertOrIgnore([['_id' => $id], ['foo' => 'bar']]));
153-
$this->assertSame(2, $builder->count());
154-
}
155-
156145
public function testFind()
157146
{
158147
$id = DB::collection('users')->insertGetId(['name' => 'John Doe']);

0 commit comments

Comments
 (0)