# `TraceGuard.Storage.Ecto`
[🔗](https://github.com/HataluliRandima/trace_guard/blob/v0.1.0/lib/trace_guard/storage/ecto.ex#L2)

**Experimental in 0.1.0** (the Ecto/PostgreSQL adapter): the API may
change before 1.0.

Stores reports in your application's **existing PostgreSQL database
through your existing Ecto Repo**. No second Repo, connection pool or
database is created.

    # mix.exs: ecto_sql and postgrex come from your application
    # config/config.exs
    config :trace_guard, storage: {TraceGuard.Storage.Ecto, repo: MyApp.Repo}

The table is created by **your** migration (see
`TraceGuard.Storage.Ecto.Migration` and `mix trace_guard.gen.ecto_migration`).
Nothing is created or migrated at startup.

## Options

  * `:repo` (required): your `Ecto.Repo`, using `Ecto.Adapters.Postgres`.
  * `:prefix`: the PostgreSQL schema of the table, if not the default.
  * `:max_reports`: rows kept (default: the `:max_reports` config). The
    oldest are deleted in the same transaction as each insert.
  * `:max_age`: if set, rows older than this many milliseconds (database
    clock) are deleted by cleanup.
  * `:cleanup_interval`: periodic cleanup in ms (default 60_000).
  * `:write_timeout`: per-statement timeout for the writer, in ms
    (default 5_000).
  * `:call_timeout`: default timeout for reads, in ms (default 5_000).

Unknown or invalid options raise `ArgumentError` when the adapter starts.

## How it uses your Repo

  * **Writes** happen only in this adapter's writer process, which
    TraceGuard starts at boot, before your Repo. The writer never queries
    in `init/1`, so a missing table or an unreachable database doesn't
    stop it from starting. It uses at most **one** connection from your
    pool at a time.
  * **Reads** (`get_report/1`, `list_reports/1`, `report_status/1`, ...)
    run in the calling process, through your pool.
  * Every statement passes `log: false` and `telemetry_event: nil`, so
    TraceGuard's own queries don't appear in your Ecto logs or telemetry,
    and are never traced by TraceGuard itself.
  * With `Ecto.Adapters.SQL.Sandbox` in your tests, use shared mode (or
    allow the writer, `Process.whereis(TraceGuard.Storage.Ecto)`), as for
    any other process that queries.

## Request path, queue and failures

`put/2` runs in the request process and only does admission (an
`:atomics` increment against `:max_pending_reports`) and a `send/2` to
the writer. That is the same bounded-queue contract as
`TraceGuard.ReportStore`: **no database work happens in the request**.
The writer's mailbox never holds more than `:max_pending_reports`
reports.

Each report is one transaction: upsert by `trace_id`, then delete rows
beyond `:max_reports`. It is announced as stored only after the commit.
On any error, the report is dropped with an explicit reason, never
retried, and the writer carries on:

  * `{:ecto_error, code}`, where `code` is the PostgreSQL error code as an
    atom (for example `:undefined_table`, `:read_only_sql_transaction`),
    or `:connection` (database unreachable), `:repo_not_started`,
    `:timeout`, `:serialization`. Server error *messages* are never
    stored or logged: they can quote row values.

The writer logs one warning when writes start failing and one notice when
they succeed again (error code only), never per report.

## Deleted data

Deletion is logical (`DELETE`). PostgreSQL keeps old row versions until
`VACUUM` reclaims them, and deleted data can remain in the WAL, in
replicas, in backups and in PITR archives for as long as those are
retained. TraceGuard can't erase it from any of those. Use your
database's own retention controls.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `cleanup`

```elixir
@spec cleanup(timeout()) :: {:ok, non_neg_integer()} | {:error, term()}
```

Applies `:max_age` and `:max_reports` now, in the writer. Returns
`{:ok, deleted_rows}` or `{:error, reason}`.

---

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