diff --git a/lib/philomena/release.ex b/lib/philomena/release.ex new file mode 100644 index 00000000..2ae529de --- /dev/null +++ b/lib/philomena/release.ex @@ -0,0 +1,24 @@ +defmodule Philomena.Release do + @app :philomena + + def migrate do + load_app() + + for repo <- repos() do + {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true)) + end + end + + def rollback(repo, version) do + load_app() + {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version)) + end + + defp repos do + Application.fetch_env!(@app, :ecto_repos) + end + + defp load_app do + Application.load(@app) + end +end diff --git a/post-receive b/post-receive index a3078e8e..c224455e 100755 --- a/post-receive +++ b/post-receive @@ -21,12 +21,6 @@ if git diff --name-only $oldrev $newrev | grep -Ee "^mix.(exs|lock)"; then mix deps.get || die "mix failed to update" fi -# Run migrations -if git diff --name-only $oldrev $newrev | grep "^priv/repo/migrations"; then - echo "Running database migrations" - mix ecto.migrate || die "ecto.migrate failed" -fi - # Compile assets if git diff --name-only $oldrev $newrev | grep "^assets/"; then echo "Compiling assets" @@ -38,6 +32,12 @@ fi echo "Building release" mix release --overwrite || die "failed to generate release" +# Run migrations +if git diff --name-only $oldrev $newrev | grep "^priv/repo/migrations"; then + echo "Running database migrations" + _build/prod/rel/philomena/bin/philomena eval "Philomena.Release.migrate()" || die "ecto.migrate failed" +fi + # Include a task to restart your running appserver instances here. # # In general, you should have many app instances configured on different