From 88ce9f11cb3ac9bd3d301344ff34d896f28b2892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Akg=C3=BCn?= Date: Tue, 13 Feb 2024 16:11:42 +0100 Subject: [PATCH] Change db transaction place --- app/Jobs/ImportCSV.php | 90 ++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 52 deletions(-) diff --git a/app/Jobs/ImportCSV.php b/app/Jobs/ImportCSV.php index aa0b1d7d..63574e6f 100644 --- a/app/Jobs/ImportCSV.php +++ b/app/Jobs/ImportCSV.php @@ -97,33 +97,34 @@ private function handleNew(CSVImportReader $reader) $mismatch_attrs = (new Mismatch())->getFillable(); $fileLines = []; $whereClause = []; -// - DB::transaction(function () use ($reader, $filepath, $mismatch_attrs, &$fileLines, &$whereClause) { - $mismatches_per_upload_user = DB::table('mismatches') - ->select($mismatch_attrs) - ->join('import_meta', 'mismatches.import_id', '=', 'import_meta.id') - ->where('import_meta.user_id', '=', $this->meta->user->id); - $reader->lines($filepath)->each(function ($mismatchLine) use ( - $mismatch_attrs, - &$fileLines, - $mismatches_per_upload_user, - &$whereClause - ) { - - $new_mismatch = Mismatch::make($mismatchLine); - $fileLines[] = $new_mismatch; - $collection = collect($new_mismatch->getAttributes()); - $collection->forget('review_status'); - $newArray = [['review_status', '!=', 'pending']]; + +// $start = hrtime(true); + + $mismatches_per_upload_user = DB::table('mismatches') + ->select($mismatch_attrs) + ->join('import_meta', 'mismatches.import_id', '=', 'import_meta.id') + ->where('import_meta.user_id', '=', $this->meta->user->id); + $reader->lines($filepath)->each(function ($mismatchLine) use ( + $mismatch_attrs, + &$fileLines, + $mismatches_per_upload_user, + &$whereClause + ) { + + $new_mismatch = Mismatch::make($mismatchLine); + $fileLines[] = $new_mismatch; + $collection = collect($new_mismatch->getAttributes()); + $collection->forget('review_status'); + $newArray = [['review_status', '!=', 'pending']]; // dd($mismatch_attrs, $collection, $new_mismatch->getAttributes()); - $collection->map(function ($item, $key) use (&$newArray) { - if ($key != 'type') { // key can be empty in the file but in the db always has statement by default - $newArray[] = [$key, $item]; - } - }); + $collection->map(function ($item, $key) use (&$newArray) { + if ($key != 'type') { // key can be empty in the file but in the db always has statement by default + $newArray[] = [$key, $item]; + } + }); - $whereClause[] = $newArray; + $whereClause[] = $newArray; // $count = $mismatches_per_upload_user->count(); // $row_in_db = $mismatches_per_upload_user->orWhere(function ($query) use ($newArray) { @@ -139,23 +140,21 @@ private function handleNew(CSVImportReader $reader) // $timespan = (hrtime(true) - $start) / 1000000; // Log::info("DB save timespan:\t {$timespan}ms"); // } - }); - -// dd($whereClause); - - $mismatches_per_upload_user->where(function ($query) use ($whereClause) { - foreach ($whereClause as $where) { - $query->orWhere(function ($query) use ($where) { - $query->where($where); - }); - } - }); + }); - $start = hrtime(true); - $result = $mismatches_per_upload_user->get(); - $timespan = (hrtime(true) - $start) / 1000000; - Log::info("DB check timespan:\t {$timespan}ms"); + $mismatches_per_upload_user->where(function ($query) use ($whereClause) { + foreach ($whereClause as $where) { + $query->orWhere(function ($query) use ($where) { + $query->where($where); + }); + } + }); + $result = $mismatches_per_upload_user->get(); +// +// $timespan = (hrtime(true) - $start) / 1000000; +// Log::info("DB check timespan:\t {$timespan}ms"); + DB::transaction(function () use ($fileLines, $result) { foreach ($fileLines as $fileLine) { if ($result->contains(function ($value, $key) use ($fileLine) { $metaAttrs = $fileLine->getAttributes(); @@ -165,25 +164,12 @@ private function handleNew(CSVImportReader $reader) return false; } } -// dd($value, $key, $fileLine); return true; })) { $this->saveMismatch($fileLine); -// dd($fileLine); } } -// if ($mismatches_per_upload_user->get()) { -// $timespan = (hrtime(true) - $start) / 1000000; -// Log::info("DB check timespan:\t {$timespan}ms"); -// $start = hrtime(true); -//// $this->saveMismatch($new_mismatch); -// $timespan = (hrtime(true) - $start) / 1000000; -// Log::info("DB save timespan:\t {$timespan}ms"); -// } - -// dd($mismatches_per_upload_user->toSql()); - $this->meta->status = 'completed'; $this->meta->save(); });