Testcontainers - A Testcontainers implementation for Elixir

Version 1.7.0 released

Kafka improvements and dependency version updates.

And its still alive of course :wink:

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 :wink: ) 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: ContactService.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 better than doing it manually IMO.

2 Likes