MyApp.Repo configuration not found in an exrm release migration task

I’m following the approach from this post to make my release_tasks module which is as follows:

defmodule :release_tasks do
  def migrate do
    {:ok, _} = Application.load(:myapp)
    Enum.map(Application.spec(:myapp, :applications), &Application.load(&1))
    Enum.map(Application.spec(:myapp, :applications), &Application.ensure_all_started(&1))

    path = Application.app_dir(:myapp, "priv/repo/migrations")

    Ecto.Migrator.run(MyApp.Repo, path, :up, all: true)

    :init.stop()
  end
end

When I try to run the task I get configuration for MyApp.Repo not specified in :myapp environment error which I’m having trouble to explain:

 {"init terminating in do_boot",{#{'__exception__'=>true,'__struct__'=>'Elixir.ArgumentError',message=><<"configuration for MyApp.Repo not specified in :myapp environment">>},[{'Elixir.Ecto.Repo.Supervisor',config,3,[{file,"lib/ecto/repo/supervisor.ex"},{line,23}]},{'Elixir.Ecto.Migration.SchemaMigration',get_source,1,[]},{'Elixir.Ecto.Migration.SchemaMigration',create_migrations_table,3,[{file,"lib/ecto/migration/schema_migration.ex"},{line,38}]},{'Elixir.Ecto.Migrator',migrated_versions,2,[{file,"lib/ecto/migrator.ex"},{line,43}]},{'Elixir.Ecto.Migrator',run,4,[{file,"lib/ecto/migrator.ex"},{line,142}]},{release_tasks,migrate,0,[{file,"lib/release_tasks.ex"},{line,9}]},{init,start_it,1,[]},{init,start_em,1,[]}]}}

In release sys.config Elixir.MyApp.Repo is present under myapp.

What’s happening?

Any ideas appreciated!

1 Like

Why aren’t you using {:ok, _} = Application.ensure_all_started(:my_app) like in the example you linked?

2 Likes

Because that would start my app and fail since the DB is not ready yet. It’s discussed in the github issue linked from that post.

1 Like

the post you mentioned is 100% working. First I create the table, eg. postgres would be createdb myapp_db. After that just type the command migrate under your release directory: /myapp.
If it doesn’t works for you, maybe you are missing some configs
Phoenix- 1.2.0, Elixir-1.3.1, Erlang-19

1 Like

Did you make any progress on this? Having a similar issue so was curious if you came to anything on this.

1 Like

@ASCrookes I ended up w/ the following task code:

defmodule Release.Tasks do
  def migrate do
    :ok = Application.load(:myapp)

    Enum.each [:ecto, :postgrex, :logger], fn(app) ->
      {:ok, _} = Application.ensure_all_started(app)
    end

    MyApp.Repo.start_link

    path = Application.app_dir(:myapp, "priv/repo/migrations")

    Ecto.Migrator.run(MyApp.Repo, path, :up, all: true)

    :init.stop()
  end
end

It works. Not sure if using Elixir module instead of Erlang one matters in my case (Elixir 1.3.2, Erlang 18.3)

2 Likes