When one is within the Mix context it’s possible to run
$ mix run priv/repo/seeds.exs
Which will evaluate code like the following into the DB:
#/priv/repo/seeds.exs ---> copied to _build/rel/appname/prod/seeds.exs once release is built
defmodule Seeder do
def start_items do
%Item{}
|> Item.changeset(%{name: Faker.Commerce.product_name()})
|> Repo.insert!()
do
end
Seeder.start_items()
I would like to also run the same script in production from a mix release binary à la
./bin/appname eval "Appname.Release.migrate"
What is the recommended way? I already copied the seeds.exs to overlays so they are located in here
Oh, so you suggest to have the Seeder module out of rel/overlays/seeds.exs and instead make it into a module inside /lib/appname/seeder.ex, that sounds like a different approach but a good idea nevertheless
Following Deploying with Releases — Phoenix v1.7.10 for migrations I got stuck when it comes to the seeds due to the fact that they by consensus live in a script .exs which I assumed requires the mix tool to the mix runex.
Ah cool. Even better Can you briefly annotate how that invoking would look like, I did find its full path via priv_dir(:appname) function but didn’t figure out how to run the script itself.
defmodule Appname.Release do
def run_seeds do
# Load seeds.exs script here
end
end
To seed one might pack seeds into a migration and run a migration. There is no mix by default on target release. Of course, another way [not recommended] would be to include mix app in the release.