From eb96671109802f745066d171320f05be32b19524 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Thu, 31 Oct 2024 13:22:14 +0000 Subject: [PATCH] fix: Import/export count reliability --- .../actions/src/Exports/Jobs/ExportCsv.php | 34 ++++++++++++------- .../src/Exports/Jobs/PrepareCsvExport.php | 3 +- .../actions/src/Imports/Jobs/ImportCsv.php | 33 +++++++++++------- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/packages/actions/src/Exports/Jobs/ExportCsv.php b/packages/actions/src/Exports/Jobs/ExportCsv.php index c95ace7aa93..19cee64ab77 100644 --- a/packages/actions/src/Exports/Jobs/ExportCsv.php +++ b/packages/actions/src/Exports/Jobs/ExportCsv.php @@ -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; @@ -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); } diff --git a/packages/actions/src/Exports/Jobs/PrepareCsvExport.php b/packages/actions/src/Exports/Jobs/PrepareCsvExport.php index e8a0e2ee4cc..cd6525c928e 100644 --- a/packages/actions/src/Exports/Jobs/PrepareCsvExport.php +++ b/packages/actions/src/Exports/Jobs/PrepareCsvExport.php @@ -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', ); } diff --git a/packages/actions/src/Imports/Jobs/ImportCsv.php b/packages/actions/src/Imports/Jobs/ImportCsv.php index 1ab8ce04df4..00c4b8f4bfa 100644 --- a/packages/actions/src/Imports/Jobs/ImportCsv.php +++ b/packages/actions/src/Imports/Jobs/ImportCsv.php @@ -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,