Hi all,
I am trying to set up an Elixir project using Mix. It’s a CLI application. I want to connect it to a local SQLite3 database. Note that for now this is not a Web application, and as such no Phoenix is involved. I build the project using mix escript.build
.
I can figure out how to structure the application, including making it work with a test database (MIX_ENV=test
). What I don’t manage to do is to have it working in MIX_ENV=dev
, i.e., when actually issuing the commands from the shell.
A command looks like:
./eelarve add -a -13.50 -x "something" -r Rimi
where eelarve
is my executable, add
is a command, and the rest is switches (sorry if all of this is obvious).
I define my main function in a module called Eelarve.CLI
, where I call as first statement Application.ensure_all_started(:eelarve)
in the belief that it will start the DB.
My application is pretty run-of-the-mill:
application.ex:
use Application
@impl true
def start(_type, _args) do
children = [
# Start the Ecto repository
Eelarve.Repo
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Eelarve.Supervisor]
Supervisor.start_link(children, opts)
end
(notice I don’t know how Eelarve.Supervisor
is implemented, I assume that by use Application
something defines it for me).
Now my configuration:
config.exs:
...
config :eelarve,
ecto_repos: [Eelarve.Repo]
...
dev.exs:
import Config
# Configure your database
config :eelarve, Eelarve.Repo,
database: Path.expand("../eelarve_dev.db", Path.dirname(__ENV__.file)),
pool_size: 5,
stacktrace: true,
show_sensitive_data_on_connection_error: true
mix.exs:
...
def application do
[
extra_applications: [:logger, :ecto_sqlite3],
mod: {Eelarve.Application, []}
]
end
...
The problem, as you may have guessed, is that when I issue my command, the SQLite application is not running. Here’s the error:
** (exit) exited in: DBConnection.Holder.checkout(#PID<0.126.0>, [log: #Function<13.38471488/1 in Ecto.Adapters.SQL.with_log/3>, source: "transactions", cache_statement: "ecto_insert_all_transactions", cast_params: ["-13.50", "Uncategorized", :EUR, ~U[2024-06-12 15:45:13.021862Z], "varie", "Rimi"], stacktrace: [{Ecto.Repo.Supervisor, :tuplet, 2, [file: ~c"lib/ecto/repo/supervisor.ex", line: 163]}, {Eelarve.Repo, :insert_all, 3, [file: ~c"lib/eelarve/repo.ex", line: 2]}, {Eelarve.Add, :call, 1, [file: ~c"lib/eelarve/add.ex", line: 31]}, {Kernel.CLI, :"-exec_fun/2-fun-0-", 3, [file: ~c"lib/kernel/cli.ex", line: 136]}], repo: Eelarve.Repo, timeout: 15000, pool_size: 5, pool: DBConnection.ConnectionPool])
** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
(db_connection 2.6.0) lib/db_connection/holder.ex:97: DBConnection.Holder.checkout/3
(db_connection 2.6.0) lib/db_connection.ex:1280: DBConnection.checkout/3
(db_connection 2.6.0) lib/db_connection.ex:1605: DBConnection.run/6
(db_connection 2.6.0) lib/db_connection.ex:800: DBConnection.execute/4
(ecto_sqlite3 0.16.0) lib/ecto/adapters/sqlite3/connection.ex:89: Ecto.Adapters.SQLite3.Connection.query/4
(ecto_sql 3.11.2) lib/ecto/adapters/sql.ex:519: Ecto.Adapters.SQL.query!/4
(ecto_sql 3.11.2) lib/ecto/adapters/sql.ex:925: Ecto.Adapters.SQL.insert_all/9
(ecto 3.11.2) lib/ecto/repo/schema.ex:59: Ecto.Repo.Schema.do_insert_all/7
What am I missing?