From dda9d732685d82d3c6d98a4ee825e616b0dcef2c Mon Sep 17 00:00:00 2001 From: Gary Rennie Date: Thu, 14 Sep 2023 19:39:20 +0100 Subject: [PATCH] Ignore migrations when a repo is read_only For a read_only repo, it is likely that attempting to run the migrations will result in an error. If the `read_only: true` config is set then the migrations check is skipped. This closes #169 --- lib/phoenix_ecto/check_repo_status.ex | 8 +++++-- test/phoenix_ecto/check_repo_status_test.exs | 24 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/phoenix_ecto/check_repo_status.ex b/lib/phoenix_ecto/check_repo_status.ex index 924d4be..705d558 100644 --- a/lib/phoenix_ecto/check_repo_status.ex +++ b/lib/phoenix_ecto/check_repo_status.ex @@ -28,7 +28,7 @@ defmodule Phoenix.Ecto.CheckRepoStatus do repos = Application.get_env(opts[:otp_app], :ecto_repos, []) for repo <- repos, Process.whereis(repo) do - unless check_pending_migrations!(repo, opts) do + unless check_pending_migrations!(repo, opts, Map.new(repo.config)) do check_storage_up!(repo) end end @@ -51,7 +51,11 @@ defmodule Phoenix.Ecto.CheckRepoStatus do end end - defp check_pending_migrations!(repo, opts) do + defp check_pending_migrations!(_repo, _opts, %{read_only: true}) do + false + end + + defp check_pending_migrations!(repo, opts, _repo_opts) do repo_status = with {:ok, migration_directories} <- migration_directories(repo, opts), {:ok, migrations} <- migrations(repo, migration_directories, opts) do diff --git a/test/phoenix_ecto/check_repo_status_test.exs b/test/phoenix_ecto/check_repo_status_test.exs index b926bec..aed8ca9 100644 --- a/test/phoenix_ecto/check_repo_status_test.exs +++ b/test/phoenix_ecto/check_repo_status_test.exs @@ -17,6 +17,12 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do def __adapter__, do: Adapter end + defmodule StorageReadOnly do + defmodule Adapter, do: def(storage_status(_repo_config), do: :up) + def config, do: [read_only: true] + def __adapter__, do: Adapter + end + defmodule StorageDownRepo do defmodule Adapter, do: def(storage_status(_repo_config), do: :down) def config, do: [] @@ -96,6 +102,24 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do Process.unregister(StorageUpRepo) end + test "does not raise when there are pending migrations for a read only repo" do + Process.register(self(), StorageReadOnly) + Application.put_env(:check_repo_ready, :ecto_repos, [StorageReadOnly]) + mock_migrations_fn = fn _repo, _directories -> [{:down, 1, "migration"}] end + + conn = conn(:get, "/") + + assert CheckRepoStatus.call( + conn, + otp_app: :check_repo_ready, + mock_default_migration_directory_fn: &default_mock_migration_directory_fn/1, + mock_migrations_fn: mock_migrations_fn + ) + after + Application.delete_env(:check_repo_ready, :ecto_repos) + Process.unregister(StorageReadOnly) + end + test "supports the `ecto_migration_lock` option" do Process.register(self(), StorageUpRepo) Application.put_env(:check_repo_ready, :ecto_repos, [StorageUpRepo])