NamesAreAPain

NamesAreAPain

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?

Showing Posts 1 to 6

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.

NamesAreAPain

NamesAreAPain OP

I didn’t consider a mix task. That would do perfectly, especially if I add a hook to ecto.reset. Thanks a ton.

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.

NamesAreAPain

NamesAreAPain OP

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?

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.

NamesAreAPain

NamesAreAPain OP

This is for a pet project, so it’s just me for the foreseeable future. But thanks, everyone, for the help.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews