Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow @defoverridable protocol in Ecto repo #959

Merged
merged 1 commit into from
Aug 26, 2024
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
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,9 +4,20 @@
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.13.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.12.x, 24.x)

unused alias Repo
alias Appsignal.Test

setup do
Expand All @@ -17,12 +28,21 @@
end

test "use Appsignal.Ecto.Repo passes through options to Ecto.Repo" do
Appsignal.TestEctoRepo.get_received_opts() == [

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

View workflow job for this annotation

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

use of operator == has no effect

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

View workflow job for this annotation

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

use of operator == has no effect
otp_app: :plug_example,
adapter: Ecto.Adapters.Postgres
]
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
Loading