Created
September 25, 2017 16:47
-
-
Save pmarreck/a627387dbef93df40ed80856b75aa27a to your computer and use it in GitHub Desktop.
How to run Ecto migrations in Elixir/Phoenix from an `iex -S mix` or production console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How to run Ecto migrations from IEx console... Examples | |
# preliminaries assumed in the following code, change to fit your environment: | |
alias YourAppName.Repo | |
your_app_name_as_atom = :mpnetwork | |
downto_version = 20170724182558 | |
# Down: | |
Ecto.Migrator.run(Repo, "priv/repo/migrations/", :down, [to: downto_version]) | |
# Up: | |
Ecto.Migrator.run(Repo, "priv/repo/migrations/", :up, [all: true]) | |
# In production when not sure if working directory is inside app dir: | |
Ecto.Migrator.run(Repo, Application.app_dir(your_app_name_as_atom, "priv/repo/migrations"), :down, [to: downto_version]) | |
Ecto.Migrator.run(Repo, Application.app_dir(your_app_name_as_atom, "priv/repo/migrations"), :up, [all: true]) | |
is there a way to run
mix ecto.create
from console?
The secret sauce is here; https://github.com/elixir-ecto/ecto/blob/v3.4.5/lib/mix/tasks/ecto.create.ex#L53
For example (via https://elixirforum.com/t/create-phoenix-repo-storage-at-application-startup/4124)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is there a way to run
mix ecto.create
from console?