Created
September 20, 2018 01:59
-
-
Save mazz/a71f32693031c10d9e2ba24ac8dd4b39 to your computer and use it in GitHub Desktop.
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 Olivetree.ReleaseTasks do | |
@start_apps [ | |
:crypto, | |
:ssl, | |
:postgrex, | |
:ecto | |
] | |
def olivetree, do: Application.get_application(__MODULE__) | |
def repos, do: Application.get_env(olivetree(), :ecto_repos, []) | |
def seed do | |
me = olivetree() | |
IO.puts "Loading #{me}.." | |
# Load the code for olivetree, but don't start it | |
:ok = Application.load(:olivetree) | |
IO.puts "Starting dependencies.." | |
# Start apps necessary for executing migrations | |
Enum.each(@start_apps, &Application.ensure_all_started/1) | |
# Start the Repo(s) for olivetree | |
IO.puts "Starting repos.." | |
Enum.each(repos(), &(&1.start_link(pool_size: 1))) | |
# Run migrations | |
migrate() | |
# Run seed script | |
Enum.each(repos(), &run_seeds_for/1) | |
# Signal shutdown | |
IO.puts "Success!" | |
:init.stop() | |
end | |
def migrate, do: Enum.each(repos(), &run_migrations_for/1) | |
def priv_dir(app), do: "#{:code.priv_dir(app)}" | |
defp run_migrations_for(repo) do | |
app = Keyword.get(repo.config, :otp_app) | |
IO.puts "Running migrations for #{app}" | |
Ecto.Migrator.run(repo, migrations_path(repo), :up, all: true) | |
end | |
# SEED-OFF | |
def run_seeds_for(repo) do | |
# Run the seed script if it exists | |
seed_script = seeds_path(repo) | |
if File.exists?(seed_script) do | |
IO.puts "Running seed script.." | |
Code.eval_file(seed_script) | |
end | |
end | |
def migrations_path(repo), do: priv_path_for(repo, "migrations") | |
# SEED-OFF | |
def seeds_path(repo), do: priv_path_for(repo, "seeds.exs") | |
def priv_path_for(repo, filename) do | |
app = Keyword.get(repo.config, :otp_app) | |
repo_underscore = repo |> Module.split |> List.last |> Macro.underscore | |
Path.join([priv_dir(app), repo_underscore, filename]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment