_toni

_toni

Mix Release. Run seeds.exs file via release binary

Hello,

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

_build/rel/appname/prod/seeds.exs 

Thank you!

Marked As Solved

dbell

dbell

Code.eval_file/1 will execute an EXS file. That is how I am running migrations via a mix release.

Also Liked

webuhu

webuhu

I currently use the following in one of my projects:

defmodule MyApp.Release.Seeder do
  @moduledoc """
  Release tasks for seeds.
  """

  require Logger

  @app :my_app

  @doc """
  Seed seeds file for repo.
  """
  @spec seed(Ecto.Repo.t(), String.t()) :: :ok | {:error, any()}
  def seed(repo, filename) do
    load_app()

    case Ecto.Migrator.with_repo(repo, &eval_seed(&1, filename)) do
      {:ok, {:ok, _fun_return}, _apps} ->
        :ok

      {:ok, {:error, reason}, _apps} ->
        Logger.error(reason)
        {:error, reason}

      {:error, term} ->
        IO.warn(term, [])
        {:error, term}
    end
  end

  @spec eval_seed(Ecto.Repo.t(), String.t()) :: any()
  defp eval_seed(repo, filename) do
    seeds_file = get_path(repo, "seeds", filename)

    if File.regular?(seeds_file) do
      {:ok, Code.eval_file(seeds_file)}
    else
      {:error, "Seeds file not found."}
    end
  end

  @spec get_path(Ecto.Repo.t(), String.t(), String.t()) :: String.t()
  defp get_path(repo, directory, filename) do
    priv_dir = "#{:code.priv_dir(@app)}"

    repo_underscore =
      repo
      |> Module.split()
      |> List.last()
      |> Macro.underscore()

    Path.join([priv_dir, repo_underscore, directory, filename])
  end

  @spec load_app() :: :ok | {:error, term()}
  defp load_app(), do: Application.load(@app)
end

And then for the release:

  • bin/my_app eval 'MyApp.Release.Seeder.seed(Elixir.MyApp.Repo, "seed.exs")

PS: You probably need to add to your config.exs

config :my_app, MyApp.Repo,
  start_apps_before_migration: [:logger]
polypush135

polypush135

/some_release/bin/prod eval "MyApp.Seeder.start_items" :man_shrugging: assuming you are using releases

_toni

_toni

Following Deploying with Releases — Phoenix v1.8.8 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 :slight_smile: 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

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement