refactor: Simplify user migration command by removing unnecessary variable assignments and streamlining role assignment logic.

This commit is contained in:
Yoga Pangestu 2026-03-16 13:45:48 +07:00
parent 8e68c0b225
commit 99e326ad51

View File

@ -7,7 +7,6 @@
use App\Models\User; use App\Models\User;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class MigrateUserCommand extends Command class MigrateUserCommand extends Command
@ -33,8 +32,7 @@ public function handle()
{ {
$this->info('Starting user migration...'); $this->info('Starting user migration...');
$legacyConn = DB::connection('mysql_second'); $legacyUsers = DB::connection('mysql_second')->table('users')->get();
$legacyUsers = $legacyConn->table('users')->get();
if ($legacyUsers->isEmpty()) { if ($legacyUsers->isEmpty()) {
$this->warn('No users found in legacy database.'); $this->warn('No users found in legacy database.');
@ -42,36 +40,35 @@ public function handle()
return; return;
} }
$this->withProgressBar($legacyUsers, function ($legacy) { foreach ($legacyUsers as $legacy) {
// Skip if user already exists with same email, but ensure role is synced if needed
$user = User::where('email', $legacy->email)->first(); $user = User::where('email', $legacy->email)->first();
$roleName = $this->mapRole($legacy->role);
if (! $user) { if (! $user) {
$username = $this->generateUniqueUsername($legacy->name, $legacy->email); $username = $this->generateUniqueUsername($legacy->name, $legacy->email);
// Using DB facade directly to insert ensures the legacy password hash
// is NOT double-hashed by the model's "hashed" cast.
$userId = DB::table('users')->insertGetId([ $userId = DB::table('users')->insertGetId([
'id' => $legacy->id, 'id' => $legacy->id,
'name' => Str::limit($legacy->name, 100), 'name' => Str::limit($legacy->name, 100),
'email' => $legacy->email, 'email' => $legacy->email,
'username' => $username, 'username' => $username,
'password' => $legacy->password, // Preserving legacy hash 'password' => $legacy->password,
'is_active' => $legacy->status == '1' ? IsActive::ACTIVE->value : IsActive::INACTIVE->value, 'is_active' => $legacy->status == '1'
? IsActive::ACTIVE->value
: IsActive::INACTIVE->value,
'email_verified_at' => now(), 'email_verified_at' => now(),
'created_at' => $legacy->created_at ?? now(), 'created_at' => $legacy->created_at ?? now(),
'updated_at' => $legacy->updated_at ?? now(), 'updated_at' => $legacy->updated_at ?? now(),
]); ]);
$user = User::find($userId); $user = User::find($userId);
}
// Map and Assign Role if ($roleName) {
$roleName = $this->mapRole($legacy->role); $user->syncRoles([$roleName]);
if ($roleName && $user) { }
$user->syncRoles([$roleName]);
} }
}); }
$this->newLine(); $this->newLine();
$this->info('User migration completed successfully!'); $this->info('User migration completed successfully!');