-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
command to migrate old tutorials into new ones
- Loading branch information
1 parent
a23b827
commit 83e43ac
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands\Migrations; | ||
|
||
use App\Models\Users\Tutorial; | ||
use App\User; | ||
use Illuminate\Console\Command; | ||
|
||
class MigrateTutorials extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'users:migrate-tutorials'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Migrate user tutorial settings'; | ||
|
||
private $count = 0; | ||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
User::whereNotNull('settings') | ||
->chunk(5000, function ($users): void { | ||
/** @var User $user */ | ||
foreach ($users as $user) { | ||
$this->count++; | ||
$settings = $user->settings; | ||
|
||
foreach ($settings as $key => $setting) { | ||
if (str_starts_with($key, 'tutorial_')) { | ||
$tutorial = new Tutorial(); | ||
$tutorial->user_id = $user->id; | ||
$tutorial->code = substr($key, 9); | ||
$tutorial->save(); | ||
unset($settings[$key]); | ||
} | ||
} | ||
$user->updateQuietly(['settings' => $settings]); | ||
} | ||
}); | ||
|
||
$this->info('Migrated ' . $this->count . ' user tutorials.'); | ||
return Command::SUCCESS; | ||
} | ||
} |