ExAudit requires you to implement your own version schema and migration. This allows you to customize the primary key type (e.g., UUID) and add custom fields (e.g., user_id) to track who performed the action.
Key fields in the versions table include:
patch: The change in Erlang External Term Format.entity_id: The ID of the tracked entity.entity_schema: The name of the table/schema.action: The type of action (created, updated, deleted).recorded_at: Timestamp of the change.rollback: Boolean indicating if the change was a rollback.
# Example Schema
defmodule MyApp.Version do
use Ecto.Schema
import Ecto.Changeset
schema "versions" do
field :patch, ExAudit.Type.Patch
field :entity_id, :integer
field :entity_schema, ExAudit.Type.Schema
field :action, ExAudit.Type.Action
field :recorded_at, :utc_datetime
field :rollback, :boolean, default: false
belongs_to :user, MyApp.Accounts.User
end
def changeset(struct, params \ %{}) do
struct
|> cast(params, [:patch, :entity_id, :entity_schema, :action, :recorded_at, :rollback])
|> cast(params, [:user_id])
end
end
# Example Migration
defmodule MyApp.Migrations.AddVersions do
use Ecto.Migration
def change do
create table(:versions) do
add :patch, :binary
add :entity_id, :integer
add :entity_schema, :string
add :action, :string
add :recorded_at, :utc_datetime
add :rollback, :boolean, default: false
add :user_id, references(:users, on_update: :update_all, on_delete: :nilify_all)
end
create index(:versions, [:entity_schema, :entity_id])
end
end