sturmer

sturmer

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?

Showing Posts 1 to 10

joelpaulkoch

joelpaulkoch

Hi

Do you have a database file? Did you run mix ecto.create?

sturmer

sturmer OP

Hi Joel, yes, the eelarve_dev.db is in the project root directory and I ran mix ecto.create. (I believe the first fact is a consequence of the second?)

joelpaulkoch

joelpaulkoch

Alright, that’s good. What’s the rest of your mix.exs file (and why do you have :ecto_sqlite3 as extra_application?

And do you have Eelarve.Repo defined somewhere?

E.g. taken from this guide

defmodule Eelarve.Repo do
  use Ecto.Repo,
    otp_app: :eelarve,
    adapter: Ecto.Adapters.SQLite3
end
sturmer

sturmer OP

This is my mix.exs in its entirety:

defmodule Eelarve.MixProject do
  use Mix.Project

  @name :eelarve
  @version "0.1.0"

  def project do
    [
      app: @name,
      version: @version,
      elixir: "~> 1.13",
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      escript: [main_module: Eelarve.CLI]
    ]
  end

  def application do
    [
      extra_applications: [:logger, :ecto_sqlite3],
      mod: {Eelarve.Application, []}
    ]
  end

  defp deps do
    [
      ecto: "~> 3.11.2",
      money: "~> 1.12",
      ecto_sqlite3: "~> 0.16"
    ]
  end
end

I don’t know why I put :ecto_sqlite3 as an extra application. That’s just me fumbling around and see what works :sweat_smile:

The file lib/eelarve/repo.ex contains:

defmodule Eelarve.Repo do
  use Ecto.Repo,
    otp_app: :eelarve,
    adapter: Ecto.Adapters.SQLite3
end

– seems to have everything you mentioned, if I can compare correctly.

arcanemachine

arcanemachine

You may not be making a Phoenix project, but the Phoenix generators may be helpful for your case.

In these situations, I will commonly spin up a Phoenix project with the correct flags (see the mix phx.new docs for examples) just to see what the generators spit out. It’s surprisingly barebones if you disable all the unneeded stuff, so it shouldn’t be too difficult to check for the relevant snippets from the generated files.

sturmer

sturmer OP

Hey @arcanemachine, that’s a great idea and in fact at some point I believe I’ve done something like that in order to get going. But I inspected visually, and I may have missed some important piece when transporting bits to my already existing (tiny and DB-less) project. I’ll retry and report back on the progress :slight_smile:

sturmer

sturmer OP

I tried harder. Generated a new project with

mix phx.new salvage_prj --database sqlite3 \
    --no-assets --no-html --no-live \
    --no-mailer --no-tailwind

Checked config.exs, application.exs, dev.exs, priv/repo/ (copied the seeds.exs), repo.ex. Same error (but somehow the output is now colorized :smiley: ).

sturmer

sturmer OP

I failed to mention something that I thought was irrelevant, but now that my output is colorized it seems more important. The error is much longer than what I pasted initially, and a part of it is this, occurring several times:

15:40:14.100 [error] Process #PID<0.184.0> raised an exception
** (FunctionClauseError) no function clause matching in :filename.join/2
    (stdlib 5.2) filename.erl:454: :filename.join({:error, :bad_name}, ~c"sqlite3_nif")
    (exqlite 0.23.0) lib/exqlite/sqlite3_nif.ex:16: Exqlite.Sqlite3NIF.load_nif/0
    (kernel 9.2) code_server.erl:1398: anonymous fn/1 in :code_server.handle_on_load/5

15:40:14.102 [warning] The on_load function for module Elixir.Exqlite.Sqlite3NIF returned:
{:function_clause,
 [
   {:filename, :join, [{:error, :bad_name}, ~c"sqlite3_nif"],
    [file: ~c"filename.erl", line: 454]},
   {Exqlite.Sqlite3NIF, :load_nif, 0,
    [file: ~c"lib/exqlite/sqlite3_nif.ex", line: 16]},
   {:code_server, :"-handle_on_load/5-fun-0-", 1,
    [file: ~c"code_server.erl", ...]}
 ]}

(I asked ChatGPT, and it tried to make me implement a copy_nif function to copy a dynamic library somewhere. Didn’t work :confused:)

D4no0

D4no0

have you configured your database name? Also check that your path doesn’t contain strange things like folder with spaces or non-ascii.

joelpaulkoch

joelpaulkoch

I followed your error message, in particular this part:

(FunctionClauseError) no function clause matching in :filename.join/2
    (stdlib 5.2) filename.erl:454: :filename.join({:error, :bad_name}, ~c"sqlite3_nif")
    (exqlite 0.23.0) lib/exqlite/sqlite3_nif.ex:16: Exqlite.Sqlite3NIF.load_nif/0

So in exqlite lib/exqlite/sqlite3_nix.ex line 16 is this:

path = :filename.join(:code.priv_dir(:exqlite), ~c"sqlite3_nif")

and apparently :code.priv_dir(:exqlite) gives you an error.

Looking at the documentation of escript, it says:

priv directory support
escripts do not support projects and dependencies that need to store or read artifacts from the priv directory.

So I believe what’s happening is that exqlite wants to load the nif library from its priv directory, but that’s somehow not supported when using escript.

As a sidenote, if you just want to store some data for you CLI application and can’t make it work with SQLite, you could try cubdb.
Never used it myself but given that it doesn’t need any NIFs I guess it should work in escript.
Or, the other way around, use burrito to build your CLI application.

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews