Ok, so you're using Statamic v3. It's going well. You've got loads of users and now you need more power. You need to put your users into a database!
There are loads of good reasons why you should do this, but there isn't an awful lot of advice out there on how to do it. So here's some. (Whether it can be called 'good' advice is left as an exercise for the reader.)
So you're looking at the docs and you're following all the steps... super easy - edit this line, comment that out - simples. But then you get to Step 8:
Run a command to migrate your file based users into the database.
Oh sweet! It's that easy!? I love you Statamic!!! ... wait, where's the command? It's here! 🙃
So, in true Blue Peter fashion, here's one I made earlier:
php artisan users:migrate [--force]
Just pop MigrateUsers.php
into your app/Console/Commands
folder (create that if it doesn't exist).
NB: You'll need to revert the changes in Step 3 of the Statamic docs' instructions - disabling the Users Stache store - as this is going to need access to that. (You can disable it again once you're done.)
This command is just a stub to get you going, feel free to copy and modify it till your heart's content.
The main thing you'll probably want to modify is the migrate
method - this is where you'll map your
file-based user's attributes to the your database model's fields.
You can modify the namespace of the model easily at the top, just replace App\Models\User
with the
appropriate Eloquent model import - but be sure to leave the UserModel
alias so things don't break.
By default, this command will reference users by their email
, both in the database ($searchColumn
)
and user YAML
files ($searchField
). Feel free to change one or both of these values depending on your
configuration, e.g. if in your database you use a UUID, change the value of the $searchColumn
property
to the name of that column, and change the value of the $searchField
property to the relevant field
in your users' YAML
files.
Yeh, I didn't get to that. This took me a few hours to write and then write this up, so you know... take what you can get 🙃
Update: Oh alright, I added Roles support.
The output is very minimal by default. If you want more detail, you can use verbosity flags -v|vv
, but
if you have lots of user files (e.g. thousands) then it's going to be quite long and might exceed your
terminal's buffer. You may want to pipe it to a file. (Note that errors will get logged too.)
The command is safe and will not overwrite user records in your database by default. If you wish to force
it to do so, use the --force
or -f
flag.
This means you should be able to keep on running the command until you get everything just right without creating loads of duplicates.
This script is simple so you shouldn't bump into any errors using it as-is, but depending on how old some
of those YAML
files are, you might bump into issues if you modify the migrate
command to start
normalising your data.
There's not much this command can do to help in those scenarios; you're on your own! I can only recommend
a bevy of if-else
and try-catch
blocks to catch all of those little data oddities.
However, when you do encounter an error, you'll likely want to capture that and move on instead of stopping
the process. In that case, you can use the helpful logError()
method, which will keep a track of all the
errors you report for a given user and log/output a useful summary at the end.
MIT.
Please leave the credits at the top wherever you use it.
See: https://gist.github.com/simonhamp/a2b9113c100e5194db53298162f1dde0?permalink_comment_id=4728402#gistcomment-4728402