Permalink
Browse files

Restart apps on successful migration

  • Loading branch information...
josevalim committed Mar 3, 2016
1 parent e5afbe9 commit df13b1c64f8edd128cec1316336b20f3153eafa3
View
@@ -95,6 +95,20 @@ defmodule Mix.Ecto do
repo
end
@doc """
Restarts the app if there was any migration command.
"""
def restart_app_if_migrated(_repo, []), do: :ok
def restart_app_if_migrated(repo, [_|_]) do
# Silence the logger to avoid application down messages.
Logger.remove_backend(:console)
app = repo.__adapter__.application
Application.stop(app)
Application.ensure_all_started(app)
after
Logger.add_backend(:console, flush: true)
end
@doc """
Gets the migrations path from a repository.
"""
@@ -66,8 +66,9 @@ defmodule Mix.Tasks.Ecto.Migrate do
ensure_migrations_path(repo)
{:ok, pid} = ensure_started(repo, opts)
migrator.(repo, migrations_path(repo), :up, opts)
migrated = migrator.(repo, migrations_path(repo), :up, opts)
pid && repo.stop(pid)
restart_app_if_migrated(repo, migrated)
end
Mix.Task.reenable "ecto.migrate"
@@ -67,8 +67,9 @@ defmodule Mix.Tasks.Ecto.Rollback do
ensure_migrations_path(repo)
{:ok, pid} = ensure_started(repo, opts)
migrator.(repo, migrations_path(repo), :down, opts)
migrated = migrator.(repo, migrations_path(repo), :down, opts)
pid && repo.stop(pid)
restart_app_if_migrated(repo, migrated)
end
Mix.Task.reenable "ecto.rollback"
@@ -58,6 +58,7 @@ defmodule Mix.Tasks.Ecto.MigrateTest do
Application.put_env(:ecto, :app_repo, Repo)
run ["--no-start"], fn _, _, _, _ ->
Process.put(:migrated, true)
[]
end
assert Process.get(:migrated)
assert Process.get(:started)
@@ -68,6 +69,7 @@ defmodule Mix.Tasks.Ecto.MigrateTest do
test "runs the migrator after starting repo" do
run ["-r", to_string(Repo), "--no-start"], fn _, _, _, _ ->
Process.put(:migrated, true)
[]
end
assert Process.get(:migrated)
assert Process.get(:started)
@@ -76,6 +78,7 @@ defmodule Mix.Tasks.Ecto.MigrateTest do
test "runs the migrator with the already started repo" do
run ["-r", to_string(StartedRepo), "--no-start"], fn _, _, _, _ ->
Process.put(:migrated, true)
[]
end
assert Process.get(:migrated)
assert Process.get(:already_started)
@@ -84,6 +87,7 @@ defmodule Mix.Tasks.Ecto.MigrateTest do
test "runs the migrator with two repos" do
run ["-r", to_string(Repo), "-r", to_string(StartedRepo), "--no-start"], fn _, _, _, _ ->
Process.put(:migrated, true)
[]
end
assert Process.get(:migrated)
assert Process.get(:started)
@@ -98,14 +102,15 @@ defmodule Mix.Tasks.Ecto.MigrateTest do
assert opts[:all] == true
assert opts[:log] == false
assert opts[:prefix] == "foo"
[]
end
assert Process.get(:started)
end
test "raises when migrations path does not exist" do
File.rm_rf!(unquote(migrations_path))
assert_raise Mix.Error, fn ->
run ["-r", to_string(Repo)], fn _, _, _, _ -> nil end
run ["-r", to_string(Repo)], fn _, _, _, _ -> [] end
end
assert !Process.get(:started)
end
@@ -56,6 +56,7 @@ defmodule Mix.Tasks.Ecto.RollbackTest do
test "runs the migrator after starting repo" do
run ["-r", to_string(Repo), "--no-start"], fn _, _, _, _ ->
Process.put(:migrated, true)
[]
end
assert Process.get(:migrated)
assert Process.get(:started)
@@ -64,6 +65,7 @@ defmodule Mix.Tasks.Ecto.RollbackTest do
test "runs the migrator with already started repo" do
run ["-r", to_string(StartedRepo), "--no-start"], fn _, _, _, _ ->
Process.put(:migrated, true)
[]
end
assert Process.get(:migrated)
end
@@ -75,14 +77,15 @@ defmodule Mix.Tasks.Ecto.RollbackTest do
assert direction == :down
assert opts[:step] == 1
assert opts[:prefix] == "foo"
[]
end
assert Process.get(:started)
end
test "raises when migrations path does not exist" do
File.rm_rf!(unquote(migrations_path))
assert_raise Mix.Error, fn ->
run ["-r", to_string(Repo)], fn _, _, _, _ -> nil end
run ["-r", to_string(Repo)], fn _, _, _, _ -> [] end
end
assert !Process.get(:started)
end

0 comments on commit df13b1c

Please sign in to comment.