From 27958eb4e32da06b6f329ecf4a47c5e74784fe3c Mon Sep 17 00:00:00 2001 From: mpyw Date: Sun, 27 Aug 2023 23:52:53 +0900 Subject: [PATCH] [10.x] Ensuring Primary Reference on Retry in `createOrFirst()` (#48161) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 🐛 Ensuring Primary Reference on Retry in `createOrFirst()` * test: 💍 Fix tests * fix: 🐛 Apply `useWritePdo()` to all `createOrFirst` impls --- src/Illuminate/Database/Eloquent/Builder.php | 2 +- src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php | 2 +- src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php | 2 +- tests/Database/DatabaseEloquentHasManyTest.php | 1 + tests/Database/DatabaseEloquentMorphTest.php | 2 ++ 5 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 2e17922c535..7e4b2725bd2 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -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(); } } diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 4a443ddece1..2e1078a609a 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -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(); } } diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index 109cb017dc9..c4b52db7ff3 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -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(); } } diff --git a/tests/Database/DatabaseEloquentHasManyTest.php b/tests/Database/DatabaseEloquentHasManyTest.php index a10e884cd6e..0bec03bc97f 100755 --- a/tests/Database/DatabaseEloquentHasManyTest.php +++ b/tests/Database/DatabaseEloquentHasManyTest.php @@ -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)); diff --git a/tests/Database/DatabaseEloquentMorphTest.php b/tests/Database/DatabaseEloquentMorphTest.php index ed17b3a092a..924cd17fc97 100755 --- a/tests/Database/DatabaseEloquentMorphTest.php +++ b/tests/Database/DatabaseEloquentMorphTest.php @@ -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)); @@ -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));