Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/phoenix_ecto/check_repo_status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ defmodule Phoenix.Ecto.CheckRepoStatus do
end

def call(%Conn{} = conn, opts) do
repos = Application.get_env(opts[:otp_app], :ecto_repos, [])
try do
repos = Application.get_env(opts[:otp_app], :ecto_repos, [])

for repo <- repos, Process.whereis(repo) do
check_pending_migrations!(repo, opts) || check_storage_up!(repo)
end
for repo <- repos, Process.whereis(repo) do
check_pending_migrations!(repo, opts) || check_storage_up!(repo)
end

conn
conn
rescue
err in [Phoenix.Ecto.StorageNotCreatedError, Phoenix.Ecto.PendingMigrationError] ->
Plug.Conn.WrapperError.reraise(conn, :error, err, __STACKTRACE__)
end
end

defp check_storage_up!(repo) do
Expand Down
24 changes: 16 additions & 8 deletions test/phoenix_ecto/check_repo_status_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do

alias Phoenix.Ecto.CheckRepoStatus

defmacro assert_wrapped(kind, func) do
quote do
wrapper_error = assert_raise(Plug.Conn.WrapperError, unquote(func))
assert %unquote(kind){} = wrapper_error.reason
wrapper_error.reason
end
end

defmodule LongLivedProcess do
def run do
Process.sleep(1_000)
Expand Down Expand Up @@ -62,7 +70,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do

conn = conn(:get, "/")

assert_raise(Phoenix.Ecto.StorageNotCreatedError, fn ->
assert_wrapped(Phoenix.Ecto.StorageNotCreatedError, fn ->
CheckRepoStatus.call(
conn,
otp_app: :check_repo_ready,
Expand All @@ -82,7 +90,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
conn = conn(:get, "/")

exception =
assert_raise(Phoenix.Ecto.PendingMigrationError, fn ->
assert_wrapped(Phoenix.Ecto.PendingMigrationError, fn ->
CheckRepoStatus.call(
conn,
otp_app: :check_repo_ready,
Expand All @@ -104,7 +112,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
conn = conn(:get, "/")

exception =
assert_raise(Phoenix.Ecto.PendingMigrationError, fn ->
assert_wrapped(Phoenix.Ecto.PendingMigrationError, fn ->
CheckRepoStatus.call(
conn,
otp_app: :check_repo_ready,
Expand Down Expand Up @@ -134,7 +142,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
conn = conn(:get, "/")

exception =
assert_raise(Phoenix.Ecto.PendingMigrationError, fn ->
assert_wrapped(Phoenix.Ecto.PendingMigrationError, fn ->
CheckRepoStatus.call(
conn,
otp_app: :check_repo_ready,
Expand All @@ -159,7 +167,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
mock_migrations_fn = fn _repo, ["foo"], _opts -> [{:down, 1, "migration"}] end

exception =
assert_raise(Phoenix.Ecto.PendingMigrationError, fn ->
assert_wrapped(Phoenix.Ecto.PendingMigrationError, fn ->
CheckRepoStatus.call(
conn,
otp_app: :check_repo_ready,
Expand All @@ -174,7 +182,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
mock_migrations_fn = fn _repo, ["foo", "bar"], _opts -> [{:down, 1, "migration"}] end

exception =
assert_raise(Phoenix.Ecto.PendingMigrationError, fn ->
assert_wrapped(Phoenix.Ecto.PendingMigrationError, fn ->
CheckRepoStatus.call(
conn,
otp_app: :check_repo_ready,
Expand Down Expand Up @@ -261,7 +269,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do

conn = conn(:get, "/")

assert_raise(Phoenix.Ecto.StorageNotCreatedError, fn ->
assert_wrapped(Phoenix.Ecto.StorageNotCreatedError, fn ->
CheckRepoStatus.call(
conn,
otp_app: :check_repo_ready,
Expand All @@ -287,7 +295,7 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
conn = conn(:get, "/")

exception =
assert_raise(Phoenix.Ecto.PendingMigrationError, fn ->
assert_wrapped(Phoenix.Ecto.PendingMigrationError, fn ->
CheckRepoStatus.call(
conn,
otp_app: :check_repo_ready,
Expand Down
Loading