From 23bd82183824cd2fbaf5c38288629ba8adab4ddd Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 17 Aug 2020 18:48:53 -0400 Subject: [PATCH] add release conf for performing migrations --- lib/philomena/release.ex | 24 ++++++++++++++++++++++++ post-receive | 12 ++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 lib/philomena/release.ex 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