To make deployment easier, I’ve been trying to set up the Ecto repo of a phoenix app at startup, rather than through mix tasks (that are not bundled in releases by distillery.)
There is a Ecto.Migrator class to run migrations, but mix ecto.create is still needed. I’ve resolved to using this :
def start(_type, _args) do
import Supervisor.Spec
# Ensure the ecto repo has been created
:ok = ensure_repo_created()
# Define workers and child supervisors to be supervised
children = [
...
]
...
start_return = Supervisor.start_link(children, opts)
# Run migrations
path = Application.app_dir(:myapp, "priv/repo/migrations")
Ecto.Migrator.run(MyApp.Repo, path, :up, all: true)
start_return
end
defp ensure_repo_created() do
case MyApp.Repo.__adapter__.storage_up(MyApp.Repo.config) do
:ok -> :ok
{:error, :already_up} -> :ok
{:error, term} -> {:error, term}
end
end
This seems to work, but this looks a bit ugly (using the adapter), is there a cleaner / better way to do this ?