# `EctoDoctor.RepeatedQueries`
[🔗](https://github.com/Daniel-Brai/ecto-doctor/blob/v0.0.1/lib/ecto_doctor/repeated_queries.ex#L1)

Catches N+1 queries while your tests run and while you use the app in dev.

It reports a query that ran at least `:threshold` times, two by default, from the same call stack in one unit of work.
Each test, request, LiveView event and Oban job is a unit.
Only reads count, never inserts, updates or deletes.

Turn on stack traces for the repo in `config/dev.exs` and `config/test.exs` so findings point at your code.

    config :my_app, MyApp.Repo, stacktrace: true

Attach it in `test/test_helper.exs` and scan every test from your case templates.

    EctoDoctor.RepeatedQueries.attach(repo: MyApp.Repo)

    # in the setup of DataCase and ConnCase
    setup tags do
      :ok = EctoDoctor.RepeatedQueries.scan_test(tags)
      MyApp.DataCase.setup_sandbox(tags)
      :ok
    end

A test tagged `@tag repeated_queries: false` isn't scanned.
`mix ecto_doctor.install` does all of this for you.

## Adopting it in an existing app

Until the baseline file exists findings only print.
Run `ECTO_DOCTOR_RECORD=1 mix test` once to record the N+1 queries you have today.
From then on only new ones fail a test.

## Options

* `:repo` - Required. Names the repo to watch or a list of repos.

* `:threshold` (`t:pos_integer/0`) - Sets how many identical queries make an N+1. The default value is `2`.

* `:mode` - Chooses whether a test with a new N+1 fails or only prints it. The default value is `:raise`.

* `:baseline` - Leaves out the N+1 queries listed in this file. The default value is `".ecto_doctor_baseline.exs"`.

* `:apps` (list of `t:atom/0`) - Picks the apps whose code counts in call stacks. Defaults to the repos' apps.

* `:allow` (list of `t:String.t/0`) - Ignores N+1 queries whose call stack has a file path containing one of these. The default value is `[]`.

* `:focus` (list of `t:String.t/0`) - Reports only N+1 queries whose call stack has a file path containing one of these. The default value is `[]`.

* `:ignore_queries` (list of `t:String.t/0`) - Ignores queries whose SQL contains one of these. The default value is `[]`.

* `:ignore_frames` (list of `t:atom/0`) - Leaves these modules out of the compared call stacks, such as wrappers around Repo. The default value is `[]`.

* `:min_duration_ms` (`t:non_neg_integer/0`) - Reports only N+1 queries that took at least this long in total. The default value is `0`.

* `:sample_rate` - Sets the share of requests and jobs to scan, from 0 to 1. Tests are always scanned. The default value is `1.0`.

* `:log` (`t:boolean/0`) - Prints each N+1 to standard error. The default value is `true`.

* `:github_annotations` (`t:boolean/0`) - Prints GitHub Actions annotations. On by default inside GitHub Actions.

* `:json_report` - Appends each N+1 to this file as a line of JSON. The default value is `nil`.

## Telemetry

Each finding emits `[:ecto_doctor, :repeated_query]` with `:count` and `:duration_ms`.
Its metadata holds `:sql`, `:frames`, `:fix`, `:key` and `:label`.

# `attach`

```elixir
@spec attach(keyword()) :: :ok
```

Starts watching the repo's queries.

Call it once in `test/test_helper.exs` or when your app starts in dev.

# `detach`

```elixir
@spec detach() :: :ok
```

Stops watching.

# `findings`

```elixir
@spec findings((-&gt; term())) :: [EctoDoctor.RepeatedQueries.Finding.t()]
```

Runs the function in a scan of its own and returns its N+1 queries without reporting them.

The assertions in `EctoDoctor.RepeatedQueries.Assertions` use it.

# `pause`

```elixir
@spec pause((-&gt; result)) :: result when result: term()
```

Leaves the code in the function out of the scan it runs in and returns what the function returns.

# `scan`

```elixir
@spec scan(nil | String.t(), (-&gt; result)) :: result when result: term()
```

Scans the code in the function and reports its N+1 queries without raising.

The label shows in each finding. It returns what the function returns.

    EctoDoctor.RepeatedQueries.scan("nightly import", &Importer.run_nightly/0)

# `scan_test`

```elixir
@spec scan_test(map()) :: :ok
```

Scans one ExUnit test and fails it on a new N+1 query.

    setup {EctoDoctor.RepeatedQueries, :scan_test}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
