Skip to content

Commit 4aa366c

Browse files
Allow migration_lock to be specified in check_repo_status (#160)
1 parent 3ff1818 commit 4aa366c

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ jobs:
1414
matrix:
1515
include:
1616
- pair:
17-
elixir: '1.7'
18-
otp: 20
17+
elixir: '1.10'
18+
otp: 21
1919
- pair:
20-
elixir: '1.12'
21-
otp: 24
20+
elixir: '1.14'
21+
otp: 25
2222
lint: lint
2323
steps:
2424
- uses: actions/checkout@v2

lib/phoenix_ecto/check_repo_status.ex

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ defmodule Phoenix.Ecto.CheckRepoStatus do
99
1010
* `:otp_app` - name of the application which the repos are fetched from
1111
* `:migration_paths` - a function that accepts a repo and returns a migration directory, or a list of migration directories, that is used to check for pending migrations
12+
* `:migration_lock` - the locking strategy used by the Ecto Adapter when checking for pending migrations. Set to `false` to disable migration locks.
1213
"""
1314

1415
@behaviour Plug
1516

1617
alias Plug.Conn
1718

19+
@migration_opts [:migration_lock]
20+
@compile {:no_warn_undefined, Ecto.Migrator}
21+
1822
def init(opts) do
1923
Keyword.fetch!(opts, :otp_app)
2024
opts
@@ -83,20 +87,30 @@ defmodule Phoenix.Ecto.CheckRepoStatus do
8387
end
8488

8589
def migrations(repo, migration_directories, opts) do
90+
migration_opts = Keyword.take(opts, @migration_opts)
91+
8692
case Keyword.fetch(opts, :mock_migrations_fn) do
8793
{:ok, migration_fn} ->
88-
migrations = migration_fn.(repo, migration_directories)
94+
migrations = get_migrations(migration_fn, repo, migration_directories, migration_opts)
8995
{:ok, migrations}
9096

9197
:error ->
9298
if Code.ensure_loaded?(Ecto.Migrator) do
93-
{:ok, Ecto.Migrator.migrations(repo, migration_directories)}
99+
{:ok, Ecto.Migrator.migrations(repo, migration_directories, migration_opts)}
94100
else
95101
:error
96102
end
97103
end
98104
end
99105

106+
defp get_migrations(fun, repo, directories, _opts) when is_function(fun, 2) do
107+
fun.(repo, directories)
108+
end
109+
110+
defp get_migrations(fun, repo, directories, opts) when is_function(fun, 3) do
111+
fun.(repo, directories, opts)
112+
end
113+
100114
defp default_migration_directory(repo, opts) do
101115
case Keyword.fetch(opts, :mock_default_migration_directory_fn) do
102116
{:ok, migration_directories_fn} ->

mix.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule PhoenixEcto.Mixfile do
88
[
99
app: :phoenix_ecto,
1010
version: @version,
11-
elixir: "~> 1.7",
11+
elixir: "~> 1.10",
1212
deps: deps(),
1313

1414
# Hex
@@ -52,7 +52,7 @@ defmodule PhoenixEcto.Mixfile do
5252
defp deps do
5353
[
5454
{:phoenix_html, "~> 2.14.2 or ~> 3.0", optional: true},
55-
{:ecto, "~> 3.3"},
55+
{:ecto, "~> 3.5"},
5656
{:plug, "~> 1.9"},
5757
{:ex_doc, ">= 0.0.0", only: :docs}
5858
]

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%{
22
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
33
"earmark_parser": {:hex, :earmark_parser, "1.4.15", "b29e8e729f4aa4a00436580dcc2c9c5c51890613457c193cc8525c388ccb2f06", [:mix], [], "hexpm", "044523d6438ea19c1b8ec877ec221b008661d3c27e3b848f4c879f500421ca5c"},
4-
"ecto": {:hex, :ecto, "3.4.6", "08f7afad3257d6eb8613309af31037e16c36808dfda5a3cd0cb4e9738db030e4", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6f13a9e2a62e75c2dcfc7207bfc65645ab387af8360db4c89fee8b5a4bf3f70b"},
4+
"ecto": {:hex, :ecto, "3.9.2", "017db3bc786ff64271108522c01a5d3f6ba0aea5c84912cfb0dd73bf13684108", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "21466d5177e09e55289ac7eade579a642578242c7a3a9f91ad5c6583337a9d15"},
55
"ex_doc": {:hex, :ex_doc, "0.25.1", "4b736fa38dc76488a937e5ef2944f5474f3eff921de771b25371345a8dc810bc", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "3200b0a69ddb2028365281fbef3753ea9e728683863d8cdaa96580925c891f67"},
66
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
77
"makeup_elixir": {:hex, :makeup_elixir, "0.15.1", "b5888c880d17d1cc3e598f05cdb5b5a91b7b17ac4eaf5f297cb697663a1094dd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "db68c173234b07ab2a07f645a5acdc117b9f99d69ebf521821d89690ae6c6ec8"},

test/phoenix_ecto/check_repo_status_test.exs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,27 @@ defmodule Phoenix.Ecto.CheckRepoStatusTest do
9696
Process.unregister(StorageUpRepo)
9797
end
9898

99+
test "supports the `ecto_migration_lock` option" do
100+
Process.register(self(), StorageUpRepo)
101+
Application.put_env(:check_repo_ready, :ecto_repos, [StorageUpRepo])
102+
mock_migrations_fn = fn _repo, _directories, _opts -> [{:down, 1, "migration"}] end
103+
104+
conn = conn(:get, "/")
105+
106+
assert_raise(Phoenix.Ecto.PendingMigrationError, fn ->
107+
CheckRepoStatus.call(
108+
conn,
109+
otp_app: :check_repo_ready,
110+
mock_default_migration_directory_fn: &default_mock_migration_directory_fn/1,
111+
mock_migrations_fn: mock_migrations_fn,
112+
migration_lock: false
113+
)
114+
end)
115+
after
116+
Application.delete_env(:check_repo_ready, :ecto_repos)
117+
Process.unregister(StorageUpRepo)
118+
end
119+
99120
test "supports the 'migration_paths' option" do
100121
Process.register(self(), StorageUpRepo)
101122
Application.put_env(:check_repo_ready, :ecto_repos, [StorageUpRepo])

0 commit comments

Comments
 (0)