carbonite icon indicating copy to clipboard operation
carbonite copied to clipboard

Tests started failing with version 0.14.0

Open boon-wego opened this issue 1 year ago • 5 comments

My tests are showing that changes are no longer being captured

Carbonite.Query.current_transaction()
    |> preload([:changes])
    |> Shopcash.Repo.one!()

returns []

Even when i followed README

Carbonite.override_mode(MyApp.Repo, to: :capture)

boon-wego avatar Jul 29 '24 08:07 boon-wego

Hi @boon-wego,

this is pretty confidently related to https://github.com/bitcrowd/carbonite/commit/5b04118a8fc058bfe20067e55c0d4254b3190c29 , but don't understand why yet. Can you give some more information about your test setup? What's the default mode you set in migrations, are you using the sandbox, deferred triggers, etc.

maltoe avatar Jul 29 '24 09:07 maltoe

test helper

defmodule MyApp.CarboniteHelpers do
  import Ecto.Query

  @spec carbonite_override_mode(map()) :: :ok
  def carbonite_override_mode(_context) do
    Carbonite.Transaction.put_meta(:user_email, "[email protected]")
    Carbonite.override_mode(MyApp.Repo)
    :ok
  end

  @spec current_transaction :: Carbonite.Transaction.t()
  def current_transaction do
    Carbonite.Query.current_transaction()
    |> preload([:changes])
    |> MyApp.Repo.one!()
  end
end

config

config :myapp, carbonite_mode: :ignore

example migration

defmodule MyApp.Repo.Migrations.AddTriggerStores do
  @mode Application.compile_env!(:myapp, :carbonite_mode)

  use Ecto.Migration

  def up do
    Carbonite.Migrations.create_trigger("stores")
    Carbonite.Migrations.put_trigger_config(:stores, :mode, @mode)
    Carbonite.Migrations.put_trigger_config(:stores, :store_changed_from, true)
  end

  def down do
    Carbonite.Migrations.drop_trigger("stores")
  end
end

example test

...
:ok = carbonite_override_mode(%{})
store = insert_store(...)
carbonite_transaction = current_transaction()
assert carbonite_transaction.meta ==
               %{
                 "action" => "insert",
                 "user_email" => "[email protected]",
                 "entity_ids" => store.id,
                 "entity" => "Store"
               }
...

boon-wego avatar Jul 29 '24 11:07 boon-wego

@boon-wego

Thanks for providing this sample code. Unfortunately, just dropping this in a fresh demo app does not show the behaviour you're claiming it does. Are you using initially: :deferred triggers by any chance?

maltoe avatar Jul 31 '24 12:07 maltoe

Nope i am not using initially: :deferred. Migrations are all like the given.

I'm using https://github.com/bitcrowd/carbonite?tab=readme-ov-file#with-ectomulti to insert_store

boon-wego avatar Jul 31 '24 12:07 boon-wego

Are you sure you have migrated to v10 then?

Because, here's my test script that I'm expecting to fail based on your description, but it doesn't:

# carbonite_test.exs

Mix.install([
  {:postgrex, "> 0.0.0"},
  {:carbonite, "0.14.0"}
])

Application.put_env(:my_app, Repo,
  username: "postgres",
  password: "postgres",
  database: "carbonite_override_test"
)

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

defmodule Migration do
  use Ecto.Migration

  def up do
    create table(:items) do
      add :name, :string
    end

    Carbonite.Migrations.up(1..10)
    Carbonite.Migrations.create_trigger(:items)
    Carbonite.Migrations.put_trigger_config(:items, :mode, :ignore)
  end

  def down, do: :ok
end

# ecto.create
Repo.__adapter__().storage_up(Repo.config())

# start repo
{:ok, _} = Repo.start_link()

# ecto.migrate
Ecto.Migrator.run(Repo, [{1, Migration}], :up, all: true)

Repo.transaction(fn ->
  # This sets the override_mode setting to 'override' which should reverse the default mode of 'ignore'
  # https://github.com/bitcrowd/carbonite/blob/eb6da7e09358137590b7694e57c9fad22b29ce04/lib/carbonite/migrations/v10.ex#L28-L32
  Carbonite.override_mode(Repo)

  # According to #111 this should succeed even though we didn't insert a transaction,
  # but it does raise as intended.
  Repo.insert_all("items", [%{name: "foo"}])
end)

maltoe avatar Jul 31 '24 14:07 maltoe

@boon-wego Closing this for now, feel free to reopen. I think it works as intended though.

maltoe avatar Aug 11 '24 16:08 maltoe