Skip to content

Instantly share code, notes, and snippets.

@andreapavoni
Last active May 14, 2018 08:55
Show Gist options
  • Save andreapavoni/29a5363593bef2561113d23c5f3466da to your computer and use it in GitHub Desktop.
Save andreapavoni/29a5363593bef2561113d23c5f3466da to your computer and use it in GitHub Desktop.
A phoenix release task to run migration. It's a modified version of the one found on the official [Distillery documentation](https://hexdocs.pm/distillery/running-migrations.html)
# ...
release :my_app do
# ...
set(
applications: [
:runtime_tools,
my_app: :permanent
]
)
set(
commands: [
migrate: "rel/commands/migrate.sh"
seed: "rel/commands/seed.sh"
]
)
end
defmodule MyApp.ReleaseTasks do
@start_apps [
:crypto,
:ssl,
:postgrex,
:ecto
]
@app :my_app
@repos Application.get_env(@app, :ecto_repos, [])
def migrate do
start_services()
run_migrations()
stop_services()
end
def seed do
start_services()
run_migrations()
run_seeds()
stop_services()
end
defp start_services do
IO.puts("Loading #{@app}..")
# Load the code for myapp, but don't start it
:ok = Application.load(@app)
IO.puts("Starting dependencies..")
# Start apps necessary for executing migrations
Enum.each(@start_apps, &Application.ensure_all_started/1)
# Start the Repo(s) for app
IO.puts("Starting repos..")
Enum.each(@repos, & &1.start_link(pool_size: 1))
end
defp stop_services do
IO.puts("Success!")
:init.stop()
end
defp run_migrations do
Enum.each(@repos, &run_migrations_for/1)
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 run_seeds do
Enum.each(@repos, &run_seeds_for/1)
end
defp run_seeds_for(repo) do
# Run the seed script if it exists
seed_script = priv_path_for(repo, "seeds.exs")
if File.exists?(seed_script) do
IO.puts("Running seed script..")
Code.eval_file(seed_script)
end
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