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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews