bitwalker/distillery

Ecto / Postgrex connection params issue

kminevskiy opened this issue · 2 comments

Steps to reproduce

I was following migrations guide from the official hexdocs page. The only difference is that I'm generating a development release.

MIX_ENV=dev mix compile
MIX_ENV=dev mix release

This is the first time I'm deploying Elixir app using Distillery, so I'm pretty sure I'm missing something obvious. I assumed my app's repo / db config would've been read during release packaging stage from the relevant environment config. Was that a wrong assumption?

Verbose Logs

** (KeyError) key :database not found in: [types: Postgrex.DefaultTypes, hostname: "localhost", username: "user", port: 5432, repo: MyApp.Repo, telemetry_prefix: [:myapp, :repo], otp_app: :myapp, timeout: 15000, pool_size: 2, show_sensitive_data_on_connection_error: true, pool: DBConnection.ConnectionPool]
    (elixir) lib/keyword.ex:389: Keyword.fetch!/2
    (postgrex) lib/postgrex/protocol.ex:90: Postgrex.Protocol.connect/1
    (db_connection) lib/db_connection/connection.ex:66: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
** (EXIT from #PID<0.93.0>) shutdown

Description of issue

  • What are the expected results?

Custom command will start services and run migrations.

  • What version of Distillery?

2.0.13

  • What OS, Erlang/Elixir versions are you seeing this issue on?

Ubuntu 18.04.02, Kernel 4.15.0-48, Elixir 1.8.2, Erlang/OTP 22

  • If possible, also provide your rel/config.exs, as it is often
    my first troubleshooting question, and you'll save us both time :)

rel/config.exs contains default configuration.

  • Is there documentation that says one thing, but Distillery does
    another? If so, please link the doc here so it can be updated if
    it's a documentation issue, or so that the fix can be based around
    what's documented.

Not sure if I'm missing additional configuration for Ecto or the guide misses these details.

  • If this is a runtime configuration issue, please also provide your config file
    (with any sensitive information stripped of course). This is almost
    always necessary to understand why some configuration may not be working.

Relevant portion of my config/dev.exs:

config :myapp, MyAppWeb.Endpoint,
  http: [port: {:system, "PORT"}],
  url: [host: "localhost", port: {:system, "PORT"}],
  cache_static_manifest: "priv/static/cache_manifest.json",
  server: true,
  root: ".",
  version: Application.spec(:myapp, :vsn),
  debug_errors: true,
  code_reloader: false,
  check_origin: false,
  watchers: []

# Configure your database
config :myapp, MyApp.Repo,
  username: "user",
  password: "secret",
  database: "db_name_here",
  hostname: "localhost",
  pool_size: 10

You say you are deploying a dev release, but using the default configuration - are you building the release locally and then deploying to another machine, or building the release on the same machine it is being run on?

I just ran mix phx.new --no-html --no-webpack --database=postgres, added distillery (2.0.14) to deps, changed config/dev.exs to use the correct username/password for my local pgsql instance, but left the default database name, then built a release with MIX_ENV=dev mix release and ran it with _build/dev/rel/app/bin/app console. It fails to connect to the database, but clearly uses the right configuration, and I can dump the config with Application.get_all_env/1 and it is correct.

If you are moving a dev release to another machine, that will not work. By default, dev releases are built with dev_mode: true, which is designed for running releases during development from within _build. Much of the release is symlinked rather than copied, and so the release will not function correctly or at all if moved.

You either need to set dev_mode: false, or use a custom env in rel/config.exs to manage the configuration so that you can build releases for non-prod environments.

If that is not the issue, then I'll need a more complete reproduction case to help troubleshoot, because a newly created app with the exact same parameters you described works correctly, which means there has to be another factor in the mix here.

I am going to close this for now, but feel free to reopen if you put together a repro case for me, and I'll do what I can to help find a solution!

Paul,

Thanks a lot for your detailed response, really appreciate it!

I wanted to play with Distillery first, so I decided to spin up an Ubuntu VM, build my test releases there and then deploy to the actual server (which is also running Ubuntu).

I'll explain what I want(ed) to achieve: I'd like to have multi-environment (let's say development, staging and production) releases for my Elixir / Phoenix app. Obviously, they will be using different environment configs. I assumed (incorrectly, as it appears) that I can deploy development release just like production. But your explanation makes total sense.

I suppose I'll just add custom envs to my rel/config.exs. And as far as I understand, they will all have to have dev_mode set to false (if I want to deploy them to a different machine), right?