AuditTrail.RepoWatcher (audit_trail v0.1.0)

Copy Markdown

Add to your Ecto.Repo (after use Ecto.Repo) to enable automatic schema-level change tracking for any schema that uses AuditTrail.Schema.

Setup

defmodule MyApp.Repo do
  use Ecto.Repo, otp_app: :my_app, adapter: Ecto.Adapters.Postgres
  use AuditTrail.RepoWatcher
end

Only schemas marked with use AuditTrail.Schema are tracked. All other Repo operations pass through unchanged.

Transactions and Ecto.Multi

Repo.transaction/2, Repo.transact/2, and Ecto.Multi (which both funnel through transact/2) are also wrapped. Any audit log produced while the transaction is open — including from Ecto.Multi.insert/update/ delete steps, or from a manual AuditTrail.emit/monitor call made inside the transaction function — is held until the transaction resolves:

  • commits ({:ok, _}) → held logs ship, in the order they were emitted
  • rolls back (anything else) → held logs are dropped

This prevents the audit trail from showing a change that was actually rolled back. Nested transactions only flush/drop once the outermost one resolves. Ecto.Multi.insert_or_update/3 and the *_all steps (insert_all/update_all/delete_all) are not tracked — see the bulk-operation limitation in the README.

Summary

Functions

maybe_track(operation, source, result)

maybe_track_bulk(operation, queryable, result)