Skip to content

Commit

Permalink
Move select to db transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanakg committed Feb 13, 2024
1 parent 88ce9f1 commit bb01bbd
Showing 1 changed file with 39 additions and 41 deletions.
80 changes: 39 additions & 41 deletions app/Jobs/ImportCSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,37 +94,39 @@ private function handleNew(CSVImportReader $reader)
$filepath = Storage::disk('local')
->path('mismatch-files/' . $this->meta->filename);

$mismatch_attrs = (new Mismatch())->getFillable();
$fileLines = [];
$whereClause = [];

// $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']];


DB::transaction(function () use ($filepath, $reader) {
$mismatch_attrs = (new Mismatch())->getFillable();

$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']];
// 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) {
Expand All @@ -140,21 +142,17 @@ private function handleNew(CSVImportReader $reader)
// $timespan = (hrtime(true) - $start) / 1000000;
// Log::info("DB save 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");
$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();

DB::transaction(function () use ($fileLines, $result) {
foreach ($fileLines as $fileLine) {
if ($result->contains(function ($value, $key) use ($fileLine) {
$metaAttrs = $fileLine->getAttributes();
Expand Down

0 comments on commit bb01bbd

Please sign in to comment.