Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run seeds after migrations #63

Open
jarlah opened this issue Dec 16, 2023 · 3 comments
Open

Run seeds after migrations #63

jarlah opened this issue Dec 16, 2023 · 3 comments
Assignees

Comments

@jarlah
Copy link
Collaborator

jarlah commented Dec 16, 2023

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
Copy link

arcanemachine commented Mar 31, 2024

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 🙂)

@arcanemachine
Copy link

arcanemachine commented Apr 2, 2024

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
Copy link
Collaborator Author

jarlah commented Apr 3, 2024

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 🥳

@jarlah jarlah self-assigned this May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants