Skip to content

Instantly share code, notes, and snippets.

@sb8244
Created February 2, 2018 19:18
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 sb8244/78ba4e736f72aecc2717f6ac5b2c2d2d to your computer and use it in GitHub Desktop.
Save sb8244/78ba4e736f72aecc2717f6ac5b2c2d2d to your computer and use it in GitHub Desktop.
Distillery migrations

Distillery migrations

  1. Add migration command in rel/config.exs [1]
  2. Create rel/commands/migrate.sh script [2]
  3. Create lib/release/tasks.ex with contents of [3]
  4. Add priv/repo/migrations to your docker context [4]
  5. Now you can run the migrate command as a custom command from your Distillery output, this is just like running console or foreground. It will appear in help of your release

[1]

release :app_name do
  # all your stuff
  set commands: [
    "migrate": "rel/commands/migrate.sh"
  ]
end

[2]

#!/bin/sh

bin/app_name command Elixir.Release.Tasks migrate

[3]

defmodule Release.Tasks do
  @start_apps [
    :crypto,
    :ssl,
    :postgrex,
    :ecto
  ]

  @repo_module AppName.Repo

  def migrate do
    :ok = Application.load(:app_name)
    Enum.each(@start_apps, &Application.ensure_all_started/1)
    @repo_module.start_link(pool_size: 1)
    run_migrations_for(@repo_module)
  end

  defp migrations_path, do: Application.app_dir(:app_name, "priv/repo/migrations")
  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(), :up, all: true)
  end
end

[4]

COPY ./priv/repo/migrations/* /app/lib/app_name-$BUILD_VERSION/priv/repo/migrations/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment