Skip to content

Commit

Permalink
Merge pull request #14673 from filamentphp/improve-import-export-coun…
Browse files Browse the repository at this point in the history
…ting-reliability

fix: Import/export count reliability
  • Loading branch information
danharrin authored Oct 31, 2024
2 parents c2e378d + eb96671 commit 858d416
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
34 changes: 21 additions & 13 deletions packages/actions/src/Exports/Jobs/ExportCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use League\Csv\Writer;
use SplTempFileObject;
use Throwable;
Expand Down Expand Up @@ -100,19 +101,26 @@ public function handle(): void
$filePath = $this->export->getFileDirectory() . DIRECTORY_SEPARATOR . str_pad(strval($this->page), 16, '0', STR_PAD_LEFT) . '.csv';
$this->export->getFileDisk()->put($filePath, $csv->toString(), Filesystem::VISIBILITY_PRIVATE);

$this->export->refresh();

$exportProcessedRows = $this->export->processed_rows + $processedRows;
$this->export->processed_rows = ($exportProcessedRows < $this->export->total_rows) ?
$exportProcessedRows :
$this->export->total_rows;

$exportSuccessfulRows = $this->export->successful_rows + $successfulRows;
$this->export->successful_rows = ($exportSuccessfulRows < $this->export->total_rows) ?
$exportSuccessfulRows :
$this->export->total_rows;

$this->export->save();
$this->export::query()
->whereKey($this->export->getKey())
->update([
'processed_rows' => DB::raw('processed_rows + ' . $processedRows),
'successful_rows' => DB::raw('successful_rows + ' . $successfulRows),
]);

$this->export::query()
->whereKey($this->export->getKey())
->whereColumn('processed_rows', '>', 'total_rows')
->update([
'processed_rows' => DB::raw('total_rows'),
]);

$this->export::query()
->whereKey($this->export->getKey())
->whereColumn('successful_rows', '>', 'total_rows')
->update([
'successful_rows' => DB::raw('total_rows'),
]);

$this->handleExceptions($exceptions);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/actions/src/Exports/Jobs/PrepareCsvExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ public function handle(): void
fn (Collection $records) => $dispatchRecords(
Arr::pluck($records->all(), $keyName),
),
column: $keyName,
column: $qualifiedKeyName,
alias: $keyName,
descending: ($baseQueryOrders[0]['direction'] ?? 'asc') === 'desc',
);
}
Expand Down
33 changes: 21 additions & 12 deletions packages/actions/src/Imports/Jobs/ImportCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,28 @@ public function handle(): void
$processedRows++;
}

$this->import->refresh();

$importProcessedRows = $this->import->processed_rows + $processedRows;
$this->import->processed_rows = ($importProcessedRows < $this->import->total_rows) ?
$importProcessedRows :
$this->import->total_rows;
$this->import::query()
->whereKey($this->import)
->update([
'processed_rows' => DB::raw('processed_rows + ' . $processedRows),
'successful_rows' => DB::raw('successful_rows + ' . $successfulRows),
]);

$this->import::query()
->whereKey($this->import)
->whereColumn('processed_rows', '>', 'total_rows')
->update([
'processed_rows' => DB::raw('total_rows'),
]);

$this->import::query()
->whereKey($this->import)
->whereColumn('successful_rows', '>', 'total_rows')
->update([
'successful_rows' => DB::raw('total_rows'),
]);

$importSuccessfulRows = $this->import->successful_rows + $successfulRows;
$this->import->successful_rows = ($importSuccessfulRows < $this->import->total_rows) ?
$importSuccessfulRows :
$this->import->total_rows;

$this->import->save();
$this->import->refresh();

event(new ImportChunkProcessed(
$this->import,
Expand Down

0 comments on commit 858d416

Please sign in to comment.