Skip to content

Commit

Permalink
[10.x] Ensuring Primary Reference on Retry in createOrFirst() (#48161)
Browse files Browse the repository at this point in the history
* fix: πŸ› Ensuring Primary Reference on Retry in `createOrFirst()`

* test: πŸ’ Fix tests

* fix: πŸ› Apply `useWritePdo()` to all `createOrFirst` impls
  • Loading branch information
mpyw authored Aug 27, 2023
1 parent 3365194 commit 27958eb
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public function createOrFirst(array $attributes = [], array $values = [])
try {
return $this->withSavepointIfNeeded(fn () => $this->create(array_merge($attributes, $values)));
} catch (UniqueConstraintViolationException $exception) {
return $this->where($attributes)->first();
return $this->useWritePdo()->where($attributes)->first();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ public function createOrFirst(array $attributes = [], array $values = [], array
$this->getQuery()->withSavepointIfNeeded(fn () => $this->attach($instance, $joining, $touch));
});
} catch (UniqueConstraintViolationException $exception) {
return (clone $this)->where($attributes)->first();
return (clone $this)->useWritePdo()->where($attributes)->first();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function createOrFirst(array $attributes = [], array $values = [])
try {
return $this->getQuery()->withSavepointIfNeeded(fn () => $this->create(array_merge($attributes, $values)));
} catch (UniqueConstraintViolationException $exception) {
return $this->where($attributes)->first();
return $this->useWritePdo()->where($attributes)->first();
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public function testCreateOrFirstMethodWithValuesFindsFirstModel()
$relation->getQuery()->shouldReceive('withSavepointIfNeeded')->once()->andReturnUsing(function ($scope) {
return $scope();
});
$relation->getQuery()->shouldReceive('useWritePdo')->once()->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(stdClass::class));

Expand Down
2 changes: 2 additions & 0 deletions tests/Database/DatabaseEloquentMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ public function testCreateOrFirstMethodFindsFirstModel()
$relation->getQuery()->shouldReceive('withSavepointIfNeeded')->once()->andReturnUsing(function ($scope) {
return $scope();
});
$relation->getQuery()->shouldReceive('useWritePdo')->once()->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class));

Expand All @@ -250,6 +251,7 @@ public function testCreateOrFirstMethodWithValuesFindsFirstModel()
$relation->getQuery()->shouldReceive('withSavepointIfNeeded')->once()->andReturnUsing(function ($scope) {
return $scope();
});
$relation->getQuery()->shouldReceive('useWritePdo')->once()->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class));

Expand Down

0 comments on commit 27958eb

Please sign in to comment.