Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgrex error calling append_to_stream on event store with JSONB serializer #212

Closed
philiph opened this issue Jul 30, 2020 · 1 comment
Closed

Comments

@philiph
Copy link

philiph commented Jul 30, 2020

Hello! First of all, thank you for an excellent library with commanded and eventstore!

I'm in the process of upgrading an app from commanded 0.19.1 to 1.1.1, and as part of this also upgrading from eventstore 0.17.0 to 1.1.0. I've got to the point where the actual app works fine but I'm having some issues with tests.

The application was already using the jsonb Postgres type for the data and metadata columns on the events table, and I'd like to keep this if possible. I used the JSONB serializer from a gist that you mentioned in issue #185 .

The tests I'm having trouble with are the "Given events / When command / Then assert tests" type for event processors. The Testing your application guide on the commanded wiki mentions the following kind of setup:

causation_id = UUID.uuid4()
correlation_id = UUID.uuid4()

event_data =
  Enum.map(events, fn -> event
    %Commanded.EventStore.EventData{
      causation_id: causation_id,
      correlation_id: correlation_id,
      event_type: Commanded.EventStore.TypeProvider.to_string(event),
      data: event,
      metadata: %{},
    }
  )

{:ok, _} = Commanded.EventStore.append_to_stream(stream_uuid, expected_version, event_data)

That seems to be for an older version of commanded / eventstore. I've updated it to:

causation_id = UUID.uuid4()
correlation_id = UUID.uuid4()

event_data =
  Enum.map(events, fn -> event
    %EventStore.EventData{
      causation_id: causation_id,
      correlation_id: correlation_id,
      event_type: Commanded.EventStore.TypeProvider.to_string(event),
      data: event,
      metadata: %{},
    }
  )

{:ok, _} = MyApp.EventStore.append_to_stream(stream_uuid, expected_version, event_data)

When I run my tests, and it gets to the MyApp.EventStore.append_to_stream/3 call, I get an error:

     ** (DBConnection.EncodeError) Postgrex expected a binary, got %{"foo" => "bar"} Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.
     stacktrace:
       (postgrex 0.15.5) lib/postgrex/type_module.ex:897: Postgrex.DefaultTypes.encode_params/3
       (postgrex 0.15.5) lib/postgrex/query.ex:75: DBConnection.Query.Postgrex.Query.encode/3
       (db_connection 2.2.2) lib/db_connection.ex:1148: DBConnection.encode/5
       (db_connection 2.2.2) lib/db_connection.ex:1246: DBConnection.run_prepare_execute/5
       (db_connection 2.2.2) lib/db_connection.ex:539: DBConnection.parsed_prepare_execute/5
       (db_connection 2.2.2) lib/db_connection.ex:532: DBConnection.prepare_execute/4
       (postgrex 0.15.5) lib/postgrex.ex:235: Postgrex.query_prepare_execute/4
       (eventstore 1.1.0) lib/event_store/storage/appender.ex:147: EventStore.Storage.Appender.insert_event_batch/3
       (eventstore 1.1.0) lib/event_store/storage/appender.ex:50: anonymous fn/4 in EventStore.Storage.Appender.append/4
       (elixir 1.10.3) lib/enum.ex:789: anonymous fn/3 in Enum.each/2
       (elixir 1.10.3) lib/enum.ex:3383: anonymous fn/3 in Enum.each/2
       (elixir 1.10.3) lib/stream.ex:1597: anonymous fn/3 in Enumerable.Stream.reduce/3
       (elixir 1.10.3) lib/stream.ex:285: Stream.after_chunk_while/2
       (elixir 1.10.3) lib/stream.ex:1626: Enumerable.Stream.do_done/2
       (elixir 1.10.3) lib/enum.ex:3383: Enum.each/2
       (db_connection 2.2.2) lib/db_connection.ex:1427: DBConnection.run_transaction/4
       (eventstore 1.1.0) lib/event_store/storage/appender.ex:25: EventStore.Storage.Appender.append/4

In the example above %{"foo" => "bar"} is the event data from the first domain event I'm trying to append.

So it seems that MyApp.EventStore.append_to_stream/3 isn't working properly when the event store is configured to use the JSONB serializer. I was wondering if you have any ideas on how to fix this?

Note: if I switch from the JSONB serializer to the included commanded JSON serializer, this particular test passes. However this causes issues elsewhere, and I'd like my event store setup in tests to match dev & prod.

Thank you for your time!

@philiph
Copy link
Author

philiph commented Jul 30, 2020

I figured out this issue.

During the upgrade I had left out the column_data_type: "jsonb" config option on the event store. This was causing the test DB to be created with wrong column type.

Config should look like:

config :my_app, MyApp.EventStore,
  serializer: JsonbSerializer,
  column_data_type: "jsonb"

@philiph philiph closed this as completed Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant