How to create a custom command in Release.ex file to register a new user?

Hi. I want to add a function to Release.ex file in order to register a new user in production. Currently I have generated phoenix Authentication codes. In Accounts.ex file there is a register_user function like this:

  def register_user(attrs) do
    %User{}
    |> User.registration_changeset(attrs)
    |> Repo.insert()
  end

I created a new function in Release.ex file and used the above function from Accounts module to define a create_user custom command:

  def create_user(email, password) do
    load_app()

    %{"email" => email, "password" => password}
    |> Accounts.register_user()
  end

Then I ran the create_user command using eval:

bin/my_app eval "MyApp.Release.create_user('some_email', 'some password')"

But I get this error:

...
 (RuntimeError) could not lookup Ecto repo MyApp.Repo because it was not started or it does not exist.

Could you please tell me what is going wrong?

Use bin/my_app rpc ā€¦ eval does not boot your release.

ā†’ mix release ā€” Mix v1.15.5

1 Like