Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command to migrate old tutorials into new ones #917

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = mb_substr($key, 9);
$tutorial->save();
unset($settings[$key]);
}
}
$user->updateQuietly(['settings' => $settings]);
}
});

$this->info('Migrated ' . $this->count . ' user tutorials.');
return Command::SUCCESS;
}
}