简体   繁体   中英

Running ecto migration via release from distillery

I have an elixir application with 3 umbrella projects. I am creating its binary (release) via distillery.

Running this command creates .tar.gz file in _build/prod/rel/se/releases/0.1.0 :

MIX_ENV=prod mix release --env=qa

And I am able to extract and run the application. To run ecto migration I have added this module for release tasks [by following https://hexdocs.pm/distillery/running-migrations.html ]:

defmodule Se.ReleaseTasks do

  @start_apps [
    :postgrex,
    :ecto
  ]

  def myapp, do: Application.get_application(__MODULE__)

  def repos, do: Application.get_env(myapp(), :ecto_repos, [])

  def seed() do
    me = myapp()

    IO.puts "Loading #{me}.."
    # Load the code for myapp, but don't start it
    :ok = Application.load(me)

    IO.puts "Starting dependencies.."
    # Start apps necessary for executing migrations
    Enum.each(@start_apps, &Application.ensure_all_started/1)

    # Start the Repo(s) for myapp
    IO.puts "Starting repos.."
    Enum.each(repos(), &(&1.start_link(pool_size: 1)))

    # Run migrations
    migrate()

    # Run seed script
    Enum.each(repos(), &run_seeds_for/1)

    # Signal shutdown
    IO.puts "Success!"
    :init.stop()
  end

  def migrate, do: Enum.each(repos(), &run_migrations_for/1)

  def priv_dir(app), do: "#{:code.priv_dir(app)}"

  defp run_migrations_for(repo) do
    app = Keyword.get(repo.config, :otp_app)
    IO.puts "Running migrations for #{app}"
    Ecto.Migrator.run(repo, migrations_path(repo), :up, all: true)
  end

  def run_seeds_for(repo) do
    # Run the seed script if it exists
    seed_script = seeds_path(repo)
    if File.exists?(seed_script) do
      IO.puts "Running seed script.."
      Code.eval_file(seed_script)
    end
  end

  def migrations_path(repo), do: priv_path_for(repo, "migrations")

  def seeds_path(repo), do: priv_path_for(repo, "seeds.exs")

  def priv_path_for(repo, filename) do
    app = Keyword.get(repo.config, :otp_app)
    repo_underscore = repo |> Module.split |> List.last |> Macro.underscore
    Path.join([priv_dir(app), repo_underscore, filename])
  end
end

Application is run and compiled with this code located in one of the umbrella project where we require migrations. After compilation and starting server, when I try to run it via:

bin/se_cloud command Elixir.Se.ReleaseTasks seed

I get this error:

Elixir.Se.ReleaseTasks.seed is either not defined or has a non-zero arity

Did anyone else encounter this issue? Or I am mis-configuring something here?

Instead of running the command directly in the terminal put it in a script file in rel/commands/migrate.sh :

#!/bin/sh

$RELEASE_ROOT_DIR/bin/se command Elixir.Se.ReleaseTasks seed

Then register your custom command in the release config:

release :se do
  ...
  set commands: [
    "migrate": "rel/commands/migrate.sh"
  ]
end

You should now be able to run it with:

bin/se migrate

Se.ReleaseTasks module should put under the lib folder that mix can compile it. For umbrella project, you can follow like below code structure:

project
 - apps
   - api-app
     - lib
       - Release.ex
   - ecto-app
 - rel

Here is an example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM