- Add migration command in rel/config.exs [1]
- Create rel/commands/migrate.sh script [2]
- Create lib/release/tasks.ex with contents of [3]
- Add priv/repo/migrations to your docker context [4]
- Now you can run the
migrate
command as a custom command from your Distillery output, this is just like runningconsole
orforeground
. It will appear inhelp
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/