-
-
Save edwardzhou/4f5c17966bddb37646f7a0adcd939f0c to your computer and use it in GitHub Desktop.
Distillery migrate command for ecto 3.0
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment