dgigafox

dgigafox

Plan to create a seeder library

Hi guys. Would like to ask for some help cause I am new to creating libraries.
So I am currently creating an elixir library for seeding that would solve this problem:

Seeding a database is simple using seeds.exs but fairly messy and complicated when the app is huge and has a bunch of tables. For example, the project that I am working on currently is taking like 30 to 40 minutes to seed which does not count DB timeouts which you need to re-run again if it happens.

Now the interface of my library is simple. Seeders have 2 callbacks:

@callback deps() :: [atom()]
@callback run(Context.t()) :: struct() | [struct()]

deps are list of seeders that should be run before the current seeder while run returns a struct/structs. It will look something like this

defmodule UserSeed do
  use ThisLibrary, key: :user

  @impl true
  def deps, do: []

  @impl true
  def run(%ThisLibrary.Context{}) do
    {:ok, users} = ...some_insert_operation()
    users
  end
end

After this seed runs it will be put to Context.seeds with the key which can be pattern matched by other seeders next in line. Something like:

defmodule PostSeed do
  use ThisLibrary, key: :post

  @impl true
  def deps, do: [UserSeed]

  @impl true
  def run(%ThisLibrary.Context{seeds: %{users: users}}) do
    {:ok, posts} = ...some_insert_operation(users)
    posts
  end
end

Now, I would like my implementation to be using GenServers. Each seeder library is a GenServer periodically checking if all deps already ran. If all deps run then that’s the time it will call run/0 and do the DB operation. The library runs a dynamic supervisor that starts each seeder as a child. So in case of error it can save the state in whatever format (maybe database, json file, etc…), re-running the seeder library will not start from the beginning but will just run from where it stopped. Also would like to run seeders asynchronously as long as its deps are complete for faster seeding.

Question is. Is this a good implementation? Am I overcomplicating things, or am I missing something?

Most Liked

al2o3cr

al2o3cr

Roughly how many rows are involved with this seed? 30-40 minutes seems like it should be tens of millions, which seems excessive.

Hitting DB timeouts suggests two things could be happening:

  • inserting too many rows in a single transaction
  • if there aren’t many rows involved, inserting things in ways that cause lock contention (unique indexes, etc)

It’s also worth investigating the database you’re seeding into - is it hitting a performance bottleneck? If so, dividing work up among GenServers is just going to mean more processes waiting for an overloaded DB.

al2o3cr

al2o3cr

30-40 minutes to insert 40k rows seems remarkably slow; I’d recommend you start by figuring out where all that time is going. For instance, changing up your seeding workflow won’t help much if (for instance) you’re using bcrypt_elixir and inserting a lot of users with log_rounds set too high.

If you need updatable seeds, a package like phil_columns could be what you’re looking for.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement