Created
August 6, 2021 06:13
-
-
Save aerni/c27cec34878c3aeafe8c2b8e87d78e4d to your computer and use it in GitHub Desktop.
Migrate User To Database
This file contains hidden or 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
<?php | |
namespace App\Console\Commands; | |
use SplFileInfo; | |
use App\Models\User; | |
use Statamic\Facades\YAML; | |
use Statamic\Facades\Entry; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\File; | |
class MigrateUsersToDatabase extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'migrate:users'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Migrate flat-file users to Eloquent.'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
$users = collect(File::allFiles(base_path('users'))) | |
->filter(function (SplFileInfo $userFile) { | |
return $userFile->getExtension() === 'yaml'; | |
}) | |
->map(function (SplFileInfo $userFile) { | |
return [ | |
'email' => str_replace('.yaml', '', $userFile->getFilename()), | |
'data' => YAML::parse(file_get_contents($userFile->getRealPath())), | |
]; | |
}) | |
->each(function ($fileData) { | |
$email = $fileData['email']; | |
$user = $fileData['data']; | |
$this->info("Migrating {$email}"); | |
// TODO: Could make it possible to not just 'updateOrCreate' a user. | |
$eloquentUser = User::firstOrCreate( | |
['email' => $email], | |
[ | |
'name' => isset($user['name']) ? $user['name'] : 'Name', | |
'email' => $email, | |
'password' => isset($user['password_hash']) ? $user['password_hash'] : null, | |
'super' => isset($user['super']) ? $user['super'] : false, | |
'preferences' => isset($user['preferences']) ? $user['preferences'] : null, | |
'pledges' => isset($user['pledges']) ? $user['pledges'] : null, | |
'customer_id' => isset($user['customer_id']) ? $user['customer_id'] : null, | |
] | |
); | |
$this->searchAndReplaceUserId($user['id'], $eloquentUser->id); | |
// TODO: If I use 'updateOrCreate' a user, I will also have to delete the | |
// column in this table, if the Statamic user yaml doesn't link to this group anymore. | |
if (isset($user['groups'])) { | |
foreach ($user['groups'] as $group) { | |
DB::table('group_user')->updateOrInsert( | |
[ | |
'user_id' => $eloquentUser->id, | |
'group_id' => $group, | |
], | |
[ | |
'user_id' => $eloquentUser->id, | |
'group_id' => $group, | |
] | |
); | |
} | |
} | |
// TODO: If I use 'updateOrCreate' a user, I will also have to delete the | |
// column in this table, if the Statamic user yaml doesn't link to this role anymore. | |
if (isset($user['roles'])) { | |
foreach ($user['roles'] as $role) { | |
DB::table('role_user')->insertOrIgnore( | |
[ | |
'user_id' => $eloquentUser->id, | |
'role_id' => $role, | |
], | |
[ | |
'user_id' => $eloquentUser->id, | |
'role_id' => $role, | |
] | |
); | |
} | |
} | |
}); | |
$this->info("Successfully migrated {$users->count()} users."); | |
} | |
// TODO: Could abstract to go through all the user relationship fields, not just the one defined here. | |
private function searchAndReplaceUserId(string $statamicId, int $eloquentId): void | |
{ | |
Entry::query() | |
->where('backer', $statamicId) | |
->get() | |
->each(function ($entry) use ($eloquentId) { | |
$entry->set('backer', $eloquentId)->save(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment