Skip to content

Instantly share code, notes, and snippets.

@mikemartin
Last active April 29, 2022 17:59
Show Gist options
  • Select an option

  • Save mikemartin/808045b6b69a93d5731af69d97b32039 to your computer and use it in GitHub Desktop.

Select an option

Save mikemartin/808045b6b69a93d5731af69d97b32039 to your computer and use it in GitHub Desktop.

Revisions

  1. mikemartin revised this gist Jun 15, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion MigrateUsersToDatabase.php
    Original 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()
    }
    });
    }
    }
    }
  2. mikemartin created this gist Jan 20, 2021.
    81 changes: 81 additions & 0 deletions MigrateUsersToDatabase.php
    Original 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,
    ]);
    }
    }
    });
    }
    }