Skip to content

Commit

Permalink
Fix: Add conditions to DISTINCT and modify reorder conditions in Post…
Browse files Browse the repository at this point in the history
…greSQL
  • Loading branch information
bzy107 committed Dec 25, 2024
1 parent 753ceea commit 77874d0
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 => in_array($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

1 comment on commit 77874d0

@Lq9000
Copy link

@Lq9000 Lq9000 commented on 77874d0 Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

172.17.221.128/24

Please sign in to comment.