Handle empty transactions and deferred triggers
mainBy default, Carbonite records a Carbonite.Transaction even if no actual data changes occur in the database. If you want to avoid storing "empty" transactions (where no changes were recorded), you must:
- Set the trigger to
INITIALLY DEFERREDduring creation:Carbonite.Migrations.create_trigger(:table, initially: :deferred) - Re-order your application logic so the transaction is only inserted if changes are actually going to happen.
Example of the re-ordered logic:
Ecto.Multi.new()
|> Ecto.Multi.run(:rabbit, &maybe_insert_rabbit/2)
|> Ecto.Multi.run(:carbonite_transaction, &maybe_insert_carbonite_transaction/2)
|> MyApp.Repo.transaction()# In the migration where the trigger is created
Carbonite.Migrations.create_trigger(:rabbits, initially: :deferred)