testcontainers / testcontainers-elixir

Testcontainers is an Elixir library that supports ExUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
MIT License
92 stars 14 forks source link

Run seeds after migrations #63

Open jarlah opened 6 months ago

jarlah commented 6 months ago

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.

arcanemachine commented 3 months ago

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 run Code.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 :slightly_smiling_face:)

arcanemachine commented 3 months ago

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: 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 it's better than running it manually like I was before.

jarlah commented 3 months ago

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 🥳