NamesAreAPain

NamesAreAPain

Where to put configuration-based setup code?

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?

Marked As Solved

kynoshi

kynoshi

Hi NamesAreAPain!

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 Seeding Data – Phoenix v1.3.0-rc.1

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: Custom Mix Tasks · Elixir School – 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.

Also Liked

shanesveller

shanesveller

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.

psylone

psylone

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.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

We're in Beta

About us Mission Statement