From 5bf8e844a44daa6de88663431b8dafff8ab380de Mon Sep 17 00:00:00 2001 From: mpyw Date: Thu, 24 Aug 2023 15:33:29 +0900 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Ensuring=20Primary=20?= =?UTF-8?q?Reference=20on=20Retry=20in=20`createOrFirst()`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index 109cb017dc95..c4b52db7ff3e 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(); } } From fdf6dfeac2c86f5485fbe5f605dd72b5e40645fe Mon Sep 17 00:00:00 2001 From: mpyw Date: Fri, 25 Aug 2023 20:07:32 +0900 Subject: [PATCH 2/3] =?UTF-8?q?test:=20=F0=9F=92=8D=20Fix=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Database/DatabaseEloquentHasManyTest.php | 1 + tests/Database/DatabaseEloquentMorphTest.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/tests/Database/DatabaseEloquentHasManyTest.php b/tests/Database/DatabaseEloquentHasManyTest.php index a10e884cd6e9..0bec03bc97fa 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 ed17b3a092a5..924cd17fc97b 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)); From 70fde6f16b7ed7e2f8d680f660c2d9fe8343e7c6 Mon Sep 17 00:00:00 2001 From: mpyw Date: Sat, 26 Aug 2023 00:33:34 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Apply=20`useWritePdo(?= =?UTF-8?q?)`=20to=20all=20`createOrFirst`=20impls?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Illuminate/Database/Eloquent/Builder.php | 2 +- src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 2e17922c5353..7e4b2725bd2b 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 4a443ddece10..2e1078a609aa 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(); } }