Created
May 5, 2017 04:06
-
-
Save barakyo/4d546b86efa07bc66a5ebb579f4eec18 to your computer and use it in GitHub Desktop.
Release Tasks
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.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