Skip to content

Instantly share code, notes, and snippets.

@zillou
Last active November 9, 2018 07:55
Show Gist options
  • Save zillou/da509e717891f0323ddae63806425a8f to your computer and use it in GitHub Desktop.
Save zillou/da509e717891f0323ddae63806425a8f to your computer and use it in GitHub Desktop.
Distillery migrate command for ecto 3.0
defmodule MyApp.Release.Tasks do
@app :my_app
@start_apps [:logger, :ssl, :postgrex, :ecto_sql, :telemetry]
@repos Application.get_env(@app, :ecto_repos, [])
def migrate do
start_services()
run_migrations()
stop_services()
end
defp start_services() do
IO.puts("Loading #{@app} form migrations...")
Application.load(@app)
IO.puts("Starting dependencies...")
Enum.each(@start_apps, &Application.ensure_all_started/1)
IO.puts("Starting repos...")
Enum.each(@repos, & &1.start_link(pool_size: 2))
end
defp run_migrations() do
IO.puts("Running migrations for #{@app}")
Enum.each(@repos, &run_migrations_for/1)
end
defp stop_services do
IO.puts("Success!")
:init.stop()
end
defp run_migrations_for(repo) do
app = Keyword.get(repo.config, :otp_app)
IO.puts("Running migrations for #{app}")
migrations_path = priv_path_for(repo, "migrations")
Ecto.Migrator.run(repo, migrations_path, :up, all: true)
end
defp priv_path_for(repo, filename) do
app = Keyword.get(repo.config, :otp_app)
repo_underscore =
repo
|> Module.split()
|> List.last()
|> Macro.underscore()
priv_dir = "#{:code.priv_dir(app)}"
Path.join([priv_dir, repo_underscore, filename])
end
end
@zillou
Copy link
Author

zillou commented Oct 25, 2018

The original distillery guide is here: https://hexdocs.pm/distillery/guides/running_migrations.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment