Skip to content

Commit

Permalink
Merge pull request #959 from appsignal/ecto-repo-overridable
Browse files Browse the repository at this point in the history
Follow `@defoverridable` protocol in Ecto repo
  • Loading branch information
unflxw authored Aug 26, 2024
2 parents 4291eee + 3d9634c commit f54ded3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
bump: patch
type: fix
---

Make `Appsignal.Ecto.Repo`'s `default_options/1` function overridable. If your Ecto repo uses `Appsignal.Ecto.Repo` and implements its own `default_options/1`, it must call `super` to merge its default options with those of `Appsignal.Ecto.Repo`:

```elixir
defmodule MyEctoRepo
use Appsignal.Ecto.Repo

def default_options(operation) do
super(operation) ++ [
# ... your default options here ...
]
end
end
```
8 changes: 5 additions & 3 deletions lib/appsignal/ecto_repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ defmodule Appsignal.Ecto.Repo do
quote do
use unquote(@ecto_repo), unquote(opts)

def default_options(atom) do
Appsignal.Ecto.Repo.default_options(atom)
def default_options(operation) do
super(operation) ++ Appsignal.Ecto.Repo.default_options()
end

defoverridable default_options: 1
end
end

def default_options(_atom) do
def default_options(_operation \\ nil) do
[
telemetry_options: [
_appsignal_current_span: Appsignal.Tracer.current_span()
Expand Down
20 changes: 20 additions & 0 deletions test/appsignal/ecto_repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ defmodule Appsignal.TestEctoRepo do
adapter: Ecto.Adapters.Postgres
end

defmodule Appsignal.TestEctoRepoWithOverride do
use Appsignal.Ecto.Repo

def default_options(operation) do
super(operation) ++
[
foo: "bar"
]
end
end

defmodule Appsignal.EctoRepoTest do
use ExUnit.Case
alias Appsignal.Ecto.Repo

Check warning on line 20 in test/appsignal/ecto_repo_test.exs

View workflow job for this annotation

GitHub Actions / test (1.12.x, 24.x)

unused alias Repo

Check warning on line 20 in test/appsignal/ecto_repo_test.exs

View workflow job for this annotation

GitHub Actions / test (1.13.x, 24.x)

unused alias Repo
Expand All @@ -23,6 +34,15 @@ defmodule Appsignal.EctoRepoTest do
]
end

test "use Appsignal.Ecto.Repo can have overriden default options" do
assert Appsignal.TestEctoRepoWithOverride.default_options(:all) == [
telemetry_options: [
_appsignal_current_span: nil
],
foo: "bar"
]
end

describe "use Appsignal.Ecto.Repo, with a root span" do
setup do
%{span: Appsignal.Tracer.create_span("http_request")}
Expand Down
5 changes: 5 additions & 0 deletions test/support/fake_ecto_repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ defmodule FakeEctoRepo do
def get_received_opts do
unquote(opts)
end

# As implemented in `Ecto.Repo`:
# https://github.com/elixir-ecto/ecto/blob/9df9b35044d74322cdd5c263b6d593ba98a19c44/lib/ecto/repo.ex#L260-L261
def default_options(_operation), do: []
defoverridable default_options: 1
end
end
end

0 comments on commit f54ded3

Please sign in to comment.