# `mix threadline.gen.row_history_index`
[🔗](https://github.com/szTheory/threadline/blob/v1.2.0/lib/mix/tasks/threadline.gen.row_history_index.ex#L1)

Generates an Ecto migration that adds `audit_changes_row_history_idx` to an
existing Threadline install.

Run this once, after upgrading to a release that ships this task, if your
install predates it. A new install already gets the index from
`mix threadline.install`; this task exists for adopters upgrading from an
earlier release.

## Usage

    mix threadline.gen.row_history_index

Writes one migration using `CREATE INDEX CONCURRENTLY IF NOT EXISTS`, so it
never blocks writes to `audit_changes` while it builds, even on a large,
hot table. Run `mix ecto.migrate` to apply it.

## If the concurrent build fails partway

A concurrent index build interrupted mid-way — the migration is killed, the
connection drops, a duplicate is detected — leaves an INVALID index behind
in PostgreSQL. `IF NOT EXISTS` then treats that INVALID index as already
present and skips it on a retry, so the fix and a retry look like nothing
happened. Drop the invalid index first, then rerun `mix ecto.migrate`:

```sql
DROP INDEX CONCURRENTLY IF EXISTS <storage_schema>.audit_changes_row_history_idx;
```

Rolling back the generated migration drops the index the same
non-blocking way, with `DROP INDEX CONCURRENTLY IF EXISTS`.

## If you cannot run this task

The raw SQL, run against your configured storage schema, is the fallback:

```sql
CREATE INDEX CONCURRENTLY IF NOT EXISTS audit_changes_row_history_idx
  ON <storage_schema>.audit_changes
  (table_schema, table_name, table_pk, captured_at DESC, id DESC);
```

## Options

* `--migrations-path` — directory the migration is written to, used as given
  relative to the current directory. The repo is not loaded.
* `--repo` / `-r` — one repo; the migration goes to that repo's migrations
  directory, the same place `mix threadline.install` writes.

Without `--migrations-path` or `--repo`, the task uses the first repo in
`:ecto_repos` and its `:priv` setting. A configured repo that cannot be
loaded falls back to `priv/repo/migrations`, with a warning naming it.

## Rerunning

Running this task again when a migration ending in
`_threadline_row_history_index.exs` already exists in the directory writes
nothing; it prints the existing file's path instead.

---

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