Carbonite.Multi (Carbonite v0.3.1) View Source

This module provides functions for dealing with audit trails in the context of Ecto.Multi.

Link to this section Summary

Functions

Sets the current transaction to "override mode" for all tables in a translation log.

Link to this section Types

Link to this type

insert_transaction_option()

View Source

Specs

insert_transaction_option() :: {:carbonite_prefix, prefix()} | {:params, map()}
Link to this type

override_mode_option()

View Source

Specs

override_mode_option() :: {:carbonite_prefix, prefix()}

Specs

params() :: map()

Specs

prefix() :: binary() | atom()

Link to this section Functions

Link to this function

insert_transaction(multi, params \\ %{}, opts \\ [])

View Source (since 0.2.0)

Specs

insert_transaction(Ecto.Multi.t(), params(), [insert_transaction_option()]) ::
  Ecto.Multi.t()

Adds an insert operation for a Carbonite.Transaction to an Ecto.Multi.

Options

  • carbonite_prefix defines the audit trail's schema, defaults to "carbonite_default"
  • params map of params for the Carbonite.Transaction (e.g., :meta)

Multiple inserts in the same transaction

Normally, you should have exactly one insert_transaction/3 call per database transaction. In practise, there are two scenarios in this function may be called multiple times:

  1. If an operation A, which calls insert_transaction/3, sometimes is nested within an outer operation B, which also calls insert_transaction/3.
  2. In tests using Ecto's SQL sandbox, subsequent calls to transactional operations (even to the same operation twice) are wrapped inside the overarching test transaction, and hence also effectively call insert_transaction/3 within the same transaction.

While the first scenario can be resolved using appropriate control flow (e.g. by conditionally disabling the inner insert_transaction/3 call), the second scenario is quite common and often unavoidable.

Therefore, insert_transaction/3 ignores subsequent calls within the same database transaction (equivalent to ON CONFLICT DO NOTHING), discarding metadata passed to all calls but the first.

Link to this function

override_mode(multi, opts \\ [])

View Source (since 0.2.0)

Specs

override_mode(Ecto.Multi.t(), [override_mode_option()]) :: Ecto.Multi.t()

Sets the current transaction to "override mode" for all tables in a translation log.