Skip to content

Commit

Permalink
Merge pull request #15181 from bzy107/fix-order-due-to-using-chunks
Browse files Browse the repository at this point in the history
Fix: Add conditions to DISTINCT and modify reorder conditions in PostgreSQL
  • Loading branch information
danharrin authored Jan 2, 2025
2 parents b880e89 + 3726be6 commit 2bbfeeb
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/actions/src/Exports/Jobs/PrepareCsvExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ($order['column'] ?? null) === $qualifiedKeyName)
->unique('column');

/** @var array<string, mixed> $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)) {
Expand Down Expand Up @@ -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 ?? [];
Expand Down

0 comments on commit 2bbfeeb

Please sign in to comment.