Hi, I’m pretty new to working with Elixir so please forgive me if this is should have been obvious.
I have an application that manages group permissions by assigning permissions/bundled groups of permissions via a database of relationships. Figuring that many groups would want similar org structures, I implemented a way to copy another organization’s permission’s structure (basically the mapping of roles to permissions). It occurred to me that this would be an easy mechanism to offer preset organization structures, by just having some prototype organizations in the database.
Now, I’m trying to figure out where to put the initialization code for inserting these prototypes into the database. My first thought was to put it directly in a migration, but I would like for these presets to be definable as a configuration option that I could change in the future, and a given migration runs exactly once. So then I figured I could handle the change-detection myself, but that leaves the question of where to put this code. Should I create a process that listens for configuration changes and does the necessary database updates, or is there a better way, maybe by putting some code in the configuration file itself?
You are right: Seeding initial data usually doesn’t belong into migrations. I yet don’t get the exact scenario and parameters of your project, so my anwsers really boil down to the very default options. Based on the scenario, that you or some other technologically educated person will be in charge for setting up the application on their machine.
Basically what be to your interest is the term “seeding data”. You can find a pretty much interesting section on that here https://hexdocs.pm/phoenix/1.3.0-rc.1/seeding_data.html
If this still doesn’t match your scenario’s requirements and your would like to generate some custom seeds, you could have a look into how to write mix tasks in general: https://elixirschool.com/en/lessons/basics/mix-tasks/ – I put mine into
$project/lib/mix/tasks which should be pretty much de default folder as i recall right. In there you can to whatever you want and also access contexts and models from your phoenix project of course.
Where the to be seeded data originates from (local json/xml, hardcoded structs, http remote, database replication, git clone) is completely up to you and your case.
Just my 2 cents.
I didn’t consider a mix task. That would do perfectly, especially if I add a hook to ecto.reset. Thanks a ton.
Note that if these seeds are not for development only, and do things like populate lookup tables that are always necessary, you should tweak the above suggestion a little.
Write the seeding logic as a normal Elixir module and call that from any Mix tasks you might create. I suggest this because the preferred approach to deploying Elixir apps is to use releases, which remove your access to Mix commands.
If the Mix task is very thin and only calls a standard module and its functions, you’ll have less hurdles to continue forward with. When it comes time to deploy the code and run those seeds, you’ll still be able to do so without Mix.
Separately, I’d also suggest either not using Ecto schemas or context modules in your seeds, or making sure to exercise them very regularly. It’s a good candidate for CI if you get that going. Using changesets and schemas in your seeds are more brittle than doing schema-less inserts because the schemas and validations tend to evolve over time, with different requirements, while seeds are not often maintained alongside those changes. You might find a few weeks or months later that your schema logic no longer generates valid rows to insert, and not everyone thinks to wrap them in a database transaction to catch that.
Sorry if this is a distinct question that belongs in its own topic, @shanesveller, but in releases can I still access the Elixir repl like I can in development to execute arbitrary code (in this case, updating the prototypes), or do I need to implement some admin interface that allows me to do so?
Yes, in release you can access to IEx. But if you will delegate this task to not developers people, probably it would be a good idea to give them some kind of admin interface.
This is for a pet project, so it’s just me for the foreseeable future. But thanks, everyone, for the help.