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?