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

**Experimental in 0.1.0.** Creates and drops the table used by
`TraceGuard.Storage.Ecto`, from **your application's own migration**.
TraceGuard never runs migrations itself.

Generate a migration with `mix trace_guard.gen.ecto_migration`, or write one:

    defmodule MyApp.Repo.Migrations.AddTraceGuardReports do
      use Ecto.Migration

      def up, do: TraceGuard.Storage.Ecto.Migration.up(version: 1)
      def down, do: TraceGuard.Storage.Ecto.Migration.down(version: 1)
    end

then run `mix ecto.migrate` as usual.

## Options

  * `:version`: the schema version to migrate to (`up`) or from (`down`).
    Only `1` exists. Pin it in your migration so that a later TraceGuard
    release can't change what an already-run migration does.
  * `:prefix`: a PostgreSQL schema to create the table in (default: the
    Repo's default, usually `public`). Use the same `:prefix` in the
    adapter's configuration.

## Schema version 1

Table `trace_guard_reports`:

| Column | Type | Purpose |
| --- | --- | --- |
| `id` | `bigserial` (primary key) | insertion order (newest = highest) |
| `trace_id` | `varchar(32)`, unique | lookup |
| `outcome`, `method`, `kind` | `varchar` | filters |
| `route`, `exception` | `text` | filters |
| `status` | `integer` | filters |
| `request_id` | `varchar`, indexed | filter (from `Plug.RequestId`) |
| `incoming_id` | `varchar(128)`, indexed | filter (client correlation ID) |
| `summary` | `jsonb` | `TraceGuard.Storage.summary/1` |
| `report` | `jsonb` | the **redacted** report |
| `inserted_at` | `timestamp`, indexed | age-based retention |

Indexes: unique `trace_id`, `inserted_at`, `incoming_id`, `request_id`,
and `(route, status)`.

The filter columns duplicate fields of the redacted summary; nothing in
the table comes from anywhere other than the redacted report.

## Rollback

`down(version: 1)` drops the table and all stored reports. Before rolling
back, switch TraceGuard to another storage adapter or deploy without the
Ecto adapter. While the table is missing, writes are dropped with
`{:ecto_error, :undefined_table}` and reads return `{:error, :unavailable}`.

# `down`

Drops TraceGuard's table (and every stored report).

# `up`

Creates TraceGuard's table (schema `:version`, default and latest: 1).

---

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