ohmyohmyohmy

ohmyohmyohmy

Hello all,

Usually when I write a program in another language like Python, there’s a clear-cut way to start something without having to manually execute a function from the command line (mix run -e Module.function()). That’s because, from the way I see it, there’s ‘main’ entry-point function from which the program launches, and other functions get run from there.

I can’t find any article that simply points to Phoenix’s main entry-point function from which I can simply execute a simple function (My.Stuff.populate_database()) without having to write some Agent/GenServer module and Supervising children? I get article’s like “What’s the main use of Phoenix”, but nothing that points to an entry-point of the program from which I can just run a function.

I need help. Where is Elixir/Phoenix’s ‘main’ function or its equivalent which which I can run a simple function? Thank you.

Showing Posts 1 to 10

NobbZ

NobbZ

The :phoenix application has it’s application start callback in Phoenix.

https://github.com/phoenixframework/phoenix/blob/main/lib/phoenix.ex

But you can not really call anything from there, that code has already been written.

And even though you can reload modules at will, the application has probably already been started.

Maybe explain a bit better what you are trying to achieve.

ohmyohmyohmy

ohmyohmyohmy OP

I am experimenting with Ecto and I have a module called Custom.DB that creates an in-memory database.

I have included the module as one of the children in application.ex.

However, before I can use it, I have to first create the tables, and populate the database. I’ve created a function for doing so, but I don’t know how to get it to run when the program starts with iex -S mix phx.server.

defmodule Custom.DB do
  use Ecto.Repo,
    otp_app: :testing,
    adapter: Ecto.Adapters.SQLite3

  def create_tables do
    :testing
    |> :code.priv_dir()
    |> Path.join("repo/testing.sql")
    |> File.read!()
    |> query()
  end

  def show_tables do
    query("select * from sqlite_schema;")
  end
end

in application.ex:

    children = [
    ...

      {Custom.DB, database: :memory, pool_size: 1}
      ...
    ]

Where do I run Custom.DB.create_tables()?

LostKobrakai

LostKobrakai

There’s no single entrypoint on the beam, which you can customize. That’s not how the abstractions are setup. The beam when starting will start a set of application. Each application can optionally provide a callback module, which (among others) will have the beam execute the start/2 function of that application. That’s where you put code to execute on startup.

You’re already have that callback module and function, because that’s where the children list is defined, which you posted.

ohmyohmyohmy

ohmyohmyohmy OP

Mhmm… thank you for trying, but I still don’t get it. Maybe my question wasn’t specific enough.

My question is: Where do I run Custom.DB.create_tables() so my database is populated with whatever I have in ‘repo/testing.sql’ when I run iex -S mix phx.server?

NobbZ

NobbZ

That totally depends on your exact setup, though I would probably do that kind of seeding in the process that owns the database, such that it’s reseeded if the process ever has to be restarted.

LostKobrakai

LostKobrakai

Do you want to do that whenever your application starts, only in development, only when using the phx.server mix task, …. There’s a lot of versions to this question, which do not share their answer.

ohmyohmyohmy

ohmyohmyohmy OP

I see. So there’s a lot more to this.

I want to have this run whenever my application starts. So, Custom.DB.create_tables() should be executed everytime I start the server, in dev or prod.

Once the database is populated, I can render views using the data within wih Ecto (Repo, Query, Schema).

LostKobrakai

LostKobrakai

Then the start/2 callback of your application module (usually MyApp.Application) is the place to run such things.

NobbZ

NobbZ

I really think the owning process should be responsible, not the application, to ensure that a crash in the DB process at least recovers into “empty but usable” database, rather than a database that doesn’t even have a schema.

LostKobrakai

LostKobrakai

Maybe. Sometimes that additional level of rigor is worth it. But I’d argue that’s step two. It’s no longer just the question of where the beams “main” is.

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
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
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
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 & 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