defmodule ActionRequestsDemo.Release do @moduledoc """ Helpers for running tasks in a release environment. This module is intended for commands invoked via: bin/action_requests_demo eval "ActionRequestsDemo.Release.seed()" """ @app :action_requests_demo @doc """ Run the database seeds script inside a release. This is designed to be run against a *running* release (e.g. via `bin/action_requests_demo eval ...` inside your Docker container), so it does **not** try to start the application or endpoint again. It only ensures Faker is running, then evaluates the standard `priv/repo/seeds.exs` file, which uses `ActionRequestsDemo.Repo`. """ def seed do ensure_app_started(ActionRequestsDemo.Repo) ensure_app_started(:faker) seeds_path = Application.app_dir(@app, "priv/repo/seeds.exs") Code.require_file(seeds_path) end defp ensure_app_started(app_or_repo) do case Application.ensure_all_started(app_or_repo) do {:ok, _} -> :ok {:error, {:already_started, _pid}} -> :ok {:error, {:not_started, _}} -> start_repo(app_or_repo) {:error, reason} -> raise "Could not start #{inspect(app_or_repo)}: #{inspect(reason)}" end rescue UndefinedFunctionError -> start_repo(app_or_repo) end defp start_repo(repo) when is_atom(repo) do case repo.start_link() do {:ok, _pid} -> :ok {:error, {:already_started, _pid}} -> :ok other -> raise "Could not start #{inspect(repo)}: #{inspect(other)}" end end defp start_repo(app) do case Application.ensure_all_started(app) do {:ok, _} -> :ok {:error, {:already_started, _}} -> :ok other -> raise "Could not start #{inspect(app)}: #{inspect(other)}" end end end