Run seeds after migrations
jarlah opened this issue · comments
Currently we do not run seeds. These are mostly valuable for when running locally and not having to create new users every single time for ex.
As mentioned in your post on Elixir Forums, the seeds file can be evaluated manually:
however, there is an alternative way to solve this though. You can easily start the phoenix application in interactive elixir shell with
iex -S mix phx.server
and runCode.eval_file("priv/repo/seeds.exs")
in the interactive terminal.
(I'm just adding it here since it feels like the right place for it 🙂)
EDIT: This workaround is no longer required.
To workaround this issue, this is what I have come up with so far that allows the seed file to be loaded during application startup. It's not the most elegant, but it works on both initial startup and when restarting the application. It can be tweaked as needed, but does what is required. (Just make sure to test your seeds file manually first...)
Paste this snippet (just the part with the Testcontainers
stuff ;) ) into your project (make sure it comes after starting YourApp.Repo
):
lib/your_app/application.ex
children = [
# Start the Ecto repository
YourApp.Repo,
# If using Testcontainers, import the seeds file
{Task,
fn ->
if Application.get_env(:testcontainers, :enabled) == true &&
File.exists?("priv/repo/seeds.exs") do
try do
Logger.info("Loading seed data into the database...")
Code.eval_file("priv/repo/seeds.exs")
rescue
_ ->
Logger.warning(
"Could not load the seeds file. " <>
"It may have already been loaded during a previous run."
)
end
end
end},
# Start the Telemetry supervisor
YourAppWeb.Telemetry,
# Start the PubSub system
{Phoenix.PubSub, name: YourApp.PubSub},
# ...
]
Now, when you start your application with mix phx.server
, it should either load the seeds file (during the first run after creating the DB), or fail gracefully and show a logger message that suggests that it has already been loaded (during subsequent runs when using a persistent Docker volume).
Again, not perfect, but it's better than running it manually like I was before.
thanks for the snippet @arcanemachine
I will soon start to work on some other parts around testcontainers for elixir, so I might make a stab on it then.
And if someone else takes it up before me that's just 🥳
This is now irrelevant. Use mix testcontainers.test
and update test.exs to use env vars and we are home free
See README