Postgrex-related error while trying to start iex with mix from a non postgres project

Hello,

I was trying the jamdb_oracle ecto adapter, and apparently I cannot use it with mix to create databases and tables. However, I tried pointing to an existing database, and running iex when I got:

** (Mix) Could not start application postgrex: could not find application file: postgrex.app

Why is mix looking for postgrex.app? My dependencies look like:

defp deps do
[
{:phoenix, “~> 1.4.0”},
{:phoenix_pubsub, “~> 1.1”},
{:phoenix_ecto, “~> 4.0”},
{:ecto_sql, “~> 3.0”},
{:jamdb_oracle, git: “https://github.com/erlangbureau/jamdb_oracle.git”},
{:phoenix_html, “~> 2.11”},
{:phoenix_live_reload, “~> 1.2”, only: :dev},
{:gettext, “~> 0.11”},
{:jason, “~> 1.0”},
{:plug_cowboy, “~> 2.0”}
]
end

and my DB configuration is:

config :hello, Hello.Repo,
hostname: “localhost”,
port: 1521,
database: “XE”,
username: “user”,
password: “userpass”

The app compiles fine. I just not able to run iex with it.

This is just after running: “mix phx.new hello” BTW

1 Like

mix phx.new by default depends on Postgres. It looks like you may have removed that from mix.exs but have you removed it from config/dev.exs and config/test.exs? i.e. have you changed the Ecto adapter configuration to your new adapter?

Well, the configuration I provided comes from config/dev.exs. I didn’t changed config/test.exs but I thought that was just important for running tests. I’ll try in a moment anyway.

Perhaps you were thinking about lib/hello/repo.ex

I also changed that one. The app won’t compile without it

defmodule Hello.Repo do
use Ecto.Repo,
otp_app: :hello,
adapter: Ecto.Adapters.Jamdb.Oracle
end

Thank you

Just changed “config/test.exs” as well. It didn’t make any difference.

I also went through the motions with just an Elixir app, and was able to test the jamdb_oracle adapter without issues. So, it is Phoenix that’s introducing the problem. Should I open an issue in github?

Thank you

It seems like you must have a reference to Postgrex somewhere. Is your code publicly available?

1 Like

I think you’re right, but I’m not the one adding (or leaving in this case) the reference.

I found out that if I don’t get dependencies right away after creating a new phoenix project, change the files I need to change, and then get dependencies and compile, I don’t have an issue. Not only I’m able to load the new project in iex, but I’m able to use the jamdb adapter without issues:

iex(1)> Hello.Repo.aggregate("hr.countries", :count, :country_id)
[debug] QUERY OK source="hr.countries" db=16.0ms decode=15.0ms
SELECT count(h0.country_id) FROM hr.countries h0 []
25

So, it looks like if you do get dependencies right after “mix phx.new”, change the ecto configuration later, and recompile, Phoenix is leaving a reference to postgrex behind.

EDIT: Actually, since all you do after editing is “mix deps.get” and “mix compile”, perhaps is Mix which should be updating references. Your thoughts?

:wave:

Might be related: Sharing with the community: text transcoding libraries

Sometimes we need to “force recompile” a dependency for it to see the new config.

1 Like

Hmmm, is the library that needs recompilation Ecto?

I’d think phoenix or ecto or ecto_sql.

+ there used to be problems with mix picking up changes in umbrella apps’ configs …

1 Like

I think that’s precisely was happening, but like axelson I’m not sure which dependency needs to be recompiled. So, not sure how to proceed.

I now know hot to go around the issue, but people who start a new Phoenix application that uses an ecto adapter other than postgrex will encounter this problem if they try to use iex to test.

EDIT: Actually, what made more sense to me was recompiling ecto_sql:

mix deps.compile ecto_sql --force

I’m now able to run “iex -S mix”

Thank you both for your help :grinning:

Another edit: I wonder what’s the best way to let newcomers know about this

1 Like

That’s a little unexpected to me since I thought that part of the point of moving the adapter configuration from config/config.exs to my_app/repo.ex was so that when you change the adapter configuration, the repo is recompiled.

Actually, sorry, I spoke too soon. I’m now able to run iex, but the app dies right away:

[cfarfan@localhost hello]$ iex -S mix
Erlang/OTP 21 [erts-10.1.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Interactive Elixir (1.8.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> [info] Application hello exited: shutdown

So, back to not knowing what to do if you got dependencies while creating the Phoenix app

I also tried recompiling ecto and phoenix, no difference

Running mix deps.clean --unlock --unused should sort you out.

You seem to have the problem of having postgrex from the start, removing it from mix.exs and not running the above command. mix, as everything in the official Elixir tools/libs, prefers the explicit approach. You can read more about the argumentation in the above links but the gist is that dependencies might be conditionally included – depending on Mix environment or system environment variables for example – so Mix is conservative and doesn’t fantasize what to delete unless told.

It caught me by surprise a while ago but the motivation makes sense.

1 Like

That solved the problem indeed. Thank you. Even with the workaround I found, this was making me crazy :joy:

2 Likes

No worries! Being inquisitive and curious is a sign of genius so never stop being such. :023:

1 Like