From 77874d0c3782a257101c3fb578144a6950f3dc9c Mon Sep 17 00:00:00 2001 From: bzy107 Date: Thu, 26 Dec 2024 02:04:25 +0900 Subject: [PATCH 1/2] Fix: Add conditions to DISTINCT and modify reorder conditions in PostgreSQL --- .../actions/src/Exports/Jobs/PrepareCsvExport.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/actions/src/Exports/Jobs/PrepareCsvExport.php b/packages/actions/src/Exports/Jobs/PrepareCsvExport.php index cd6525c928e..8c3350e2217 100644 --- a/packages/actions/src/Exports/Jobs/PrepareCsvExport.php +++ b/packages/actions/src/Exports/Jobs/PrepareCsvExport.php @@ -69,13 +69,18 @@ public function handle(): void if ($databaseConnection->getDriverName() === 'pgsql') { $originalOrders = collect($query->getQuery()->orders) - ->reject(fn (array $order): bool => in_array($order['column'] ?? null, [$keyName, $qualifiedKeyName])) + ->reject(fn (array $order): bool => in_array($order['column'] ?? null, [$qualifiedKeyName])) ->unique('column'); /** @var array $originalBindings */ $originalBindings = $query->getRawBindings(); - $query->reorder($qualifiedKeyName); + if (! empty($originalOrders->all())) { + $query->reorder($originalOrders[0]['column'], $originalOrders[0]['direction']); + $originalOrders->forget(0); + } else { + $query->reorder($qualifiedKeyName); + } foreach ($originalOrders as $order) { if (blank($order['column'] ?? null) || blank($order['direction'] ?? null)) { @@ -144,7 +149,10 @@ public function handle(): void $chunkKeySize = $this->chunkSize * 10; $baseQuery = $query->toBase(); - $baseQuery->distinct($qualifiedKeyName); + + if (in_array($query->getQuery()->orders[0]['column'] ?? null, [$keyName, $qualifiedKeyName])) { + $baseQuery->distinct($qualifiedKeyName); + } /** @phpstan-ignore-next-line */ $baseQueryOrders = $baseQuery->orders ?? []; From 3726be63d46acca47628ccd15366fc99e4f310c0 Mon Sep 17 00:00:00 2001 From: bzy107 Date: Thu, 2 Jan 2025 12:07:44 +0900 Subject: [PATCH 2/2] Fix: review comment --- packages/actions/src/Exports/Jobs/PrepareCsvExport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/actions/src/Exports/Jobs/PrepareCsvExport.php b/packages/actions/src/Exports/Jobs/PrepareCsvExport.php index 8c3350e2217..f0bddf3f83f 100644 --- a/packages/actions/src/Exports/Jobs/PrepareCsvExport.php +++ b/packages/actions/src/Exports/Jobs/PrepareCsvExport.php @@ -69,7 +69,7 @@ public function handle(): void if ($databaseConnection->getDriverName() === 'pgsql') { $originalOrders = collect($query->getQuery()->orders) - ->reject(fn (array $order): bool => in_array($order['column'] ?? null, [$qualifiedKeyName])) + ->reject(fn (array $order): bool => ($order['column'] ?? null) === $qualifiedKeyName) ->unique('column'); /** @var array $originalBindings */