Skip to content
Closed
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
11 changes: 9 additions & 2 deletions lib/ecto/repo/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,26 @@ defmodule Ecto.Repo.Supervisor do
name = if is_atom(name), do: name, else: nil
cache = Ecto.Query.Planner.new_query_cache(name)
meta = Map.merge(meta, %{repo: repo, cache: cache})
child_spec = wrap_child_spec(child, [name, adapter, meta])
child_spec = wrap_child_spec(child, [name, adapter, meta, opts])
Supervisor.init([child_spec], strategy: :one_for_one, max_restarts: 0)

:ignore ->
:ignore
end
end

def start_child({mod, fun, args}, name, adapter, meta) do
def start_child({mod, fun, args}, name, adapter, meta, opts) do
case apply(mod, fun, args) do
{:ok, pid} ->
meta = Map.merge(meta, %{pid: pid, adapter: adapter})
Ecto.Repo.Registry.associate(self(), name, meta)

:telemetry.execute(
[:ecto, :repo, :started],
%{system_time: System.system_time()},
%{repo: meta.repo, adapter: meta.adapter, config: opts}
)

{:ok, pid}

other ->
Expand Down
35 changes: 35 additions & 0 deletions test/ecto/repo/supervisor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,41 @@ defmodule Ecto.Repo.SupervisorTest do
:telemetry.detach(:telemetry_test)
end

test "emits :started telemetry event after repo process starts" do
:telemetry.attach_many(
:telemetry_started_test,
[[:ecto, :repo, :started]],
&__MODULE__.handle_event/4,
%{pid: self()}
)

{:ok, _pid} = Ecto.TestRepo.start_link(name: :telemetry_started_test)

assert_receive {[:ecto, :repo, :started], %{system_time: _}, %{repo: Ecto.TestRepo, adapter: Ecto.TestAdapter, config: config}}
assert config[:name] == :telemetry_started_test
assert config[:database] == "hello"

:telemetry.detach(:telemetry_started_test)
end

test "emits :started telemetry event even when repo modifies config in init" do
:telemetry.attach_many(
:telemetry_started_test_init,
[[:ecto, :repo, :started]],
&__MODULE__.handle_event/4,
%{pid: self()}
)

{:ok, _pid} = Ecto.TestRepoInitModify.start_link(name: :telemetry_started_test_init_modify)

assert_receive {[:ecto, :repo, :started], %{system_time: _}, %{repo: Ecto.TestRepoInitModify, adapter: Ecto.TestAdapter, config: config}}
assert config[:name] == :telemetry_started_test_init_modify
assert config[:database] == "hello"
refute Keyword.has_key?(config, :telemetry_prefix)

:telemetry.detach(:telemetry_started_test_init)
end

test "reads otp app configuration" do
put_env(database: "hello")
{:ok, config} = init_config(:runtime, __MODULE__, :ecto, [])
Expand Down
10 changes: 10 additions & 0 deletions test/support/test_repo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,14 @@ defmodule Ecto.TestRepo do
end
end

defmodule Ecto.TestRepoInitModify do
use Ecto.Repo, otp_app: :ecto, adapter: Ecto.TestAdapter

def init(_type, opts) do
opts = [url: "ecto://user:pass@local/hello"] ++ opts
opts = Keyword.drop(opts, [:telemetry_prefix])
{:ok, opts}
end
end

Ecto.TestRepo.start_link()
Loading