Skip to content

Commit

Permalink
Validate repo without behaviour check
Browse files Browse the repository at this point in the history
Repo wrappers that don't implement all functions of the Ecto.Repo
behaviour are still viable and shouldn't be validated with a behaviour
check. This changes repo validation back to the way it was done in older
versions, by checking that it's a valid module that exports `config/0`.

All other `:behaviour` checks are for behavious defined by Oban, and
remain intact.

Closes #1014
  • Loading branch information
sorentwo committed Jan 10, 2024
1 parent 06e18b4 commit e0819b9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/oban/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ defmodule Oban.Config do
plugins: {:custom, &validate_plugins/1},
prefix: {:or, [:falsy, :string]},
queues: {:custom, &validate_queues(opts, &1)},
repo: {:behaviour, Ecto.Repo},
repo: {:module, [config: 0]},
shutdown_grace_period: :non_neg_integer,
stage_interval: :timeout,
testing: {:enum, @testing_modes}
Expand Down
13 changes: 13 additions & 0 deletions lib/oban/validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ defmodule Oban.Validation do
{:error, "expected #{inspect(key)} to be between #{min}..#{max}, got: #{inspect(val)}"}
end

defp validate_type({:module, funs}, key, val) do
cond do
not Code.ensure_loaded?(val) ->
{:error, "expected #{inspect(key)} to be a loaded module, got: #{inspect(val)}"}

not Enum.all?(funs, fn {fun, arity} -> function_exported?(val, fun, arity) end) ->
{:error, "expected #{inspect(key)} to implement #{inspect(funs)}, got: #{inspect(val)}"}

true ->
:ok
end
end

defp validate_type(:schedule, key, val) do
case Expression.parse(val) do
{:ok, _cron} ->
Expand Down
14 changes: 14 additions & 0 deletions test/oban/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ defmodule Oban.ConfigTest do
assert_valid(queues: [default: [limit: 1, dispatch_cooldown: 10]])
end

test ":repo is validated as a repo-like module" do
defmodule NotRepo do
end

defmodule SomeRepo do
def config, do: []
end

refute_valid(repo: NotReal)
refute_valid(repo: NotRepo)

assert_valid(repo: SomeRepo)
end

test ":shutdown_grace_period is validated as an integer" do
refute_valid(shutdown_grace_period: -1)
refute_valid(shutdown_grace_period: "5")
Expand Down

0 comments on commit e0819b9

Please sign in to comment.