Skip to content

Commit

Permalink
Merge pull request #12186 from kaspernowak/bugfix/fix-export-with-def…
Browse files Browse the repository at this point in the history
…aultSort-on-pgsql

Improve PostgreSQL Ordering Compatibility in PrepareCsvExport Job
  • Loading branch information
danharrin authored Apr 11, 2024
2 parents 52854ae + cf6dd39 commit 08f6d94
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/actions/src/Exports/Jobs/PrepareCsvExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Connection;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
Expand Down Expand Up @@ -63,6 +64,21 @@ public function handle(): void
$keyName = $query->getModel()->getKeyName();
$qualifiedKeyName = $query->getModel()->getQualifiedKeyName();

/** @var Connection $databaseConnection */
$databaseConnection = $query->getConnection();

if ($databaseConnection->getDriverName() === 'pgsql') {
$originalOrders = collect($query->getQuery()->orders)
->reject(fn (array $order): bool => in_array($order['column'] ?? null, [$keyName, $qualifiedKeyName]))
->unique('column');

$query->reorder($qualifiedKeyName);

foreach ($originalOrders as $order) {
$query->orderBy($order['column'], $order['direction']);
}
}

$exportCsvJob = $this->getExportCsvJob();

$totalRows = 0;
Expand Down

0 comments on commit 08f6d94

Please sign in to comment.