-
-
Save jwarlander/809deb2bb06c2c43abafd471591b2dea 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 Wolf.ReleaseTasks do | |
@moduledoc ~S""" | |
Mix is not available in a built release. Instead we define the tasks here, | |
and invoke it using the application script generated in the release: | |
bin/wolf command Elixir.Wolf.ReleaseTasks create | |
bin/wolf command Elixir.Wolf.ReleaseTasks migrate | |
bin/wolf command Elixir.Wolf.ReleaseTasks seed | |
bin/wolf command Elixir.Wolf.ReleaseTasks drop | |
""" | |
def create do | |
repo = Wolf.Repo | |
load_app() | |
info "Creating database for #{inspect repo}.." | |
case Ecto.Storage.up(repo) do | |
:ok -> | |
info "The database for #{inspect repo} has been created." | |
{:error, :already_up} -> | |
info "The database for #{inspect repo} has already been created." | |
{:error, term} when is_binary(term) -> | |
fatal "The database for #{inspect repo} couldn't be created, reason given: #{term}." | |
{:error, term} -> | |
fatal "The database for #{inspect repo} couldn't be created, reason given: #{inspect term}." | |
end | |
System.halt(0) | |
end | |
def drop do | |
repo = Wolf.Repo | |
load_app() | |
info "Dropping database for #{inspect repo}.." | |
case Ecto.Storage.down(repo) do | |
:ok -> | |
info "The database for #{inspect repo} has been dropped." | |
{:error, :already_down} -> | |
info "The database for #{inspect repo} has already been dropped." | |
{:error, term} when is_binary(term) -> | |
fatal "The database for #{inspect repo} couldn't be dropped, reason given: #{term}." | |
{:error, term} -> | |
fatal "The database for #{inspect repo} couldn't be dropped, reason given: #{inspect term}." | |
end | |
System.halt(0) | |
end | |
def migrate do | |
repo = Wolf.Repo | |
start_repo(repo) | |
migrations_path = Application.app_dir(:wolf, "priv/repo/migrations") | |
info "Executing migrations for #{inspect repo} in #{migrations_path}:" | |
migrations = Ecto.Migrator.run(repo, migrations_path, :up, all: true) | |
info "Applied versions: #{inspect migrations}" | |
System.halt(0) | |
end | |
def seed do | |
repo = Wolf.Repo | |
start_repo(repo) | |
info "Seeding data for #{inspect repo}.." | |
# Put any needed seeding data here, or maybe run priv/repo/seeds.exs | |
System.halt(0) | |
end | |
defp start_applications(apps) do | |
Enum.each(apps, fn app -> | |
{:ok, _} = Application.ensure_all_started(app) | |
end) | |
end | |
defp start_repo(repo) do | |
load_app() | |
{:ok, _} = repo.start_link() | |
end | |
defp load_app do | |
start_applications([:logger, :postgrex, :ecto]) | |
:ok = Application.load(:wolf) | |
end | |
defp info(message) do | |
IO.puts(message) | |
end | |
defp fatal(message) do | |
IO.puts :stderr, message | |
System.halt(1) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With Ecto 2.0.0, I believe
Ecto.Storage.up(repo)
has changed torepo.__adapter__.storage_up(repo.config)
, andEcto.Storage.down(repo)
has changed torepo.__adapter__.storage_down(repo.config)