Skip to content

Commit 0ff5716

Browse files
Feat/optimize migration perfomance (#26)
* Update MigrateSoftDeletes.php Optimized migration perfomance: update table rows on mysql level instead of update each row on eloquent level. * refactor: Single query execution --------- Co-authored-by: Maksym Bilozub <[email protected]>
1 parent 6faffc2 commit 0ff5716

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

src/Commands/MigrateSoftDeletes.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Webkid\LaravelBooleanSoftdeletes\Commands;
44

5-
use Exception;
65
use Illuminate\Console\Command;
76
use Illuminate\Support\Facades\DB;
87
use Illuminate\Support\Facades\Schema;
@@ -38,18 +37,8 @@ public function handle(): void
3837

3938
$old_field_name = $this->ask('Provide old field name. If deleted_at than leave blank', 'deleted_at');
4039

41-
try {
42-
DB::table($table)->orderBy('id', 'desc')->chunk(100, function ($items) use ($table, $field_name, $old_field_name) {
43-
foreach ($items as $item) {
44-
DB::table($table)
45-
->where('id', $item->id)
46-
->update([$field_name => $item->{$old_field_name} !== null]);
47-
}
48-
});
49-
50-
$this->info('Table has been migrated!');
51-
} catch (Exception $e) {
52-
$this->error($e->getMessage());
53-
}
40+
DB::table($table)->update([
41+
$field_name => DB::raw("IF({$old_field_name} IS NULL, 0, 1)"),
42+
]);
5443
}
5544
}

0 commit comments

Comments
 (0)