Skip to content

Instantly share code, notes, and snippets.

@barakyo
Created May 5, 2017 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barakyo/4d546b86efa07bc66a5ebb579f4eec18 to your computer and use it in GitHub Desktop.
Save barakyo/4d546b86efa07bc66a5ebb579f4eec18 to your computer and use it in GitHub Desktop.
Release Tasks
defmodule MyApp.ReleaseTasks do
@start_apps [
:postgrex,
:ecto
]
@myapps [
:myapp
]
@repos [
MyApp.Repo
]
def seed do
IO.puts "Loading MyApp.."
# Load the code for myapp, but don't start it
:ok = Application.load(:myapp)
IO.puts "Starting dependencies.."
# Start apps necessary for executing migrations
Enum.each(@start_apps, &Application.ensure_all_started/1)
# Start the Repo(s) for myapp
IO.puts "Starting repos.."
Enum.each(@repos, &(&1.start_link(pool_size: 1)))
# Run migrations
Enum.each(@myapps, &run_migrations_for/1)
# Run the seed script if it exists
seed_script = Path.join([priv_dir(:myapp), "repo", "seeds.exs"])
if File.exists?(seed_script) do
IO.puts "Running seed script.."
Code.eval_file(seed_script)
end
# Signal shutdown
IO.puts "Success!"
:init.stop()
end
def priv_dir(app), do: "#{:code.priv_dir(app)}"
defp run_migrations_for(app) do
IO.puts "Running migrations for #{app}"
Ecto.Migrator.run(MyApp.Repo, migrations_path(app), :up, all: true)
end
defp migrations_path(app), do: Path.join([priv_dir(app), "repo", "migrations"])
defp seed_path(app), do: Path.join([priv_dir(app), "repo", "seeds.exs"])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment