Skip to content

Commit

Permalink
command to migrate old tutorials into new ones
Browse files Browse the repository at this point in the history
  • Loading branch information
spitfire305 committed Jul 12, 2024
1 parent a23b827 commit 83e43ac
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions app/Console/Commands/Migrations/MigrateTutorials.php
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;
}
}

0 comments on commit 83e43ac

Please sign in to comment.