Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Created September 25, 2017 16:47
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pmarreck/a627387dbef93df40ed80856b75aa27a to your computer and use it in GitHub Desktop.
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
# 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])
@astavonintsc
Copy link

is there a way to run mix ecto.create from console?

@glennr
Copy link

glennr commented Jul 28, 2020

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