Last active
April 29, 2022 17:59
-
-
Save mikemartin/808045b6b69a93d5731af69d97b32039 to your computer and use it in GitHub Desktop.
Revisions
-
mikemartin revised this gist
Jun 15, 2021 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -64,6 +64,7 @@ public function handle() 'password' => isset($user['password_hash']) ? $user['password_hash'] : null, 'super' => isset($user['super']) ? $user['super'] : false, 'preferences' => isset($user['preferences']) ? $user['preferences'] : null, 'products' => isset($user['products']) ? $user['products'] : null, 'created_at' => isset($user['created_at']) ? $user['created_at'] : now(), ]); @@ -78,4 +79,4 @@ public function handle() } }); } } -
mikemartin created this gist
Jan 20, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,81 @@ <?php namespace App\Console\Commands; use App\User; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\File; use SplFileInfo; use Statamic\Facades\YAML; 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() { 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}"); $eloquentUser = User::create([ '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, 'created_at' => isset($user['created_at']) ? $user['created_at'] : now(), ]); if (isset($user['groups'])) { foreach ($user['groups'] as $group) { DB::table('group_user') ->insert([ 'user_id' => $eloquentUser->id, 'group_id' => $group, ]); } } }); } }