journal v0.1.1 Journal behaviour View Source

Journal tracks changes (i.e. Insert, Updates, Deletes) on Ecto Schemas. This can be useful for a variety of cases, such as for keeping an activity log or for auditing changes to tables over time.

Note: this library is still very early-stage, use at your own risk.

Note: this library has only been tested with Postgres.

Setup

Make sure to have both ecto and ecto_sql in your dependencies, then add {journal: "0.1.0"}

First, you need to run an initialization script that will create a migration for a new table and a stored procedure with the following mix task:

$ mix journal.init

Now you're ready to add write-tracking to your existing Ecto.Schemas one-by-one by running this mix task:

$ mix journal.gen schema_name

Finally, commit your changes by running the migrations:

$ mix ecto.migrate

Once you've successfully migrated, drop the use Journal macro into your model to get access to journal of this model:

defmodule Todos.Todo do
  use Journal

  schema "todos" do
    field :title, :string
    field :status, :string
  end
end

Usage

To get a todos history, you can now call Todos.Todo.history_of(id) to get the query needed to list the changes. You can still append to this query, e.g. for filtering or sorting purposes.

More

This feature leverages a postgres trigger as described here: https://www.cybertec-postgresql.com/en/tracking-changes-in-postgresql/

Link to this section Summary

Link to this section Types

Link to this type

history_entry()

View Source
history_entry() :: %{
  id: Integer.t(),
  table_name: String.t(),
  operation: String.t(),
  new_val: %{},
  old_val: %{},
  inserted_at: DateTime.t()
}

Link to this section Callbacks

Link to this callback

history_of(id)

View Source
history_of(id()) :: Ecto.Query.t()