elixir-toniq / vapor

Runtime configuration system for Elixir
MIT License
592 stars 36 forks source link

Vapor not loading when running ecto.migrate #97

Closed zamith closed 3 years ago

zamith commented 4 years ago

Trying to use vapor to configure Ecto Repos. Doing it in the main application supervisor, and passing down the configs, as per the README.

However when running migrate, it does not pick them up. My understanding is that the Repo is started directly, without going through the Application. Loading Vapor again in the Repo init callback works, but duplicates the loading. Have you faced this?

mhanberg commented 3 years ago

Since the Repo can be started independent to the Application, like your example of migration you will need to load the configuration in the init callback of your ecto repo.

Luckily, the Vapor.Planner module makes this more intuitive now.

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Ecto.Adapters.Postgres

  defmodule Config do
    use Vapor.Planner

    dotenv()

    config :repo,
           env([
             {:url, "DATABASE_URL"},
             {:pool_size, "PG_POOL_SIZE", default: 2, map: &String.to_integer/1},
             {:ssl, "PG_USE_SSL", default: false, map: &String.to_existing_atom/1}
           ])
  end

  def init(_type, config) do
    runtime_config = Vapor.load!(__MODULE__.Config).repo |> Enum.into([])

    {:ok, Keyword.merge(config, runtime_config)}
  end
end

If you need this available in any another configuration plan, you can use

config :repo, MyApp.Repo.Config
zamith commented 3 years ago

Yes, that's what I've done. Wondering if there was an alternative. Thanks anyway