From 80a16278d2569b66f8f502927b7d82ac343814ac Mon Sep 17 00:00:00 2001 From: Philip Giuliani Date: Fri, 16 Feb 2024 14:00:33 +0100 Subject: [PATCH 1/2] Make sure to read the last snapshot --- lib/commanded/event_store/spear.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/commanded/event_store/spear.ex b/lib/commanded/event_store/spear.ex index 31dd347..a9025f4 100644 --- a/lib/commanded/event_store/spear.ex +++ b/lib/commanded/event_store/spear.ex @@ -179,7 +179,7 @@ defmodule Commanded.EventStore.Adapters.Spear do Logger.debug(fn -> "Spear event store read snapshot from stream: " <> inspect(stream) end) - case execute_read(adapter_meta, stream, :start, 1, :backwards) do + case execute_read(adapter_meta, stream, :end, 1, :backwards) do {:ok, stream} -> case Enum.take(stream, 1) do [recorded_event] -> {:ok, Mapper.to_snapshot_data(recorded_event)} From d213d49f84518248d0ee8adb253e8d1fc3597595 Mon Sep 17 00:00:00 2001 From: Philip Giuliani Date: Fri, 16 Feb 2024 15:16:25 +0100 Subject: [PATCH 2/2] Fix snapshot deserializing when using JSON --- .../event_store/adapters/spear/mapper.ex | 3 ++- test/event_store/snapshot_test.exs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/commanded/event_store/adapters/spear/mapper.ex b/lib/commanded/event_store/adapters/spear/mapper.ex index 92a2448..38fb394 100644 --- a/lib/commanded/event_store/adapters/spear/mapper.ex +++ b/lib/commanded/event_store/adapters/spear/mapper.ex @@ -140,7 +140,8 @@ defmodule Commanded.EventStore.Adapters.Spear.Mapper do |> Spear.Event.to_proposed_message(%{serializer_content_type => &serializer.serialize/1}) end - def to_snapshot_data(%RecordedEvent{data: %SnapshotData{} = snapshot} = event) do + def to_snapshot_data(%RecordedEvent{data: %SnapshotData{} = snapshot} = event) + when is_struct(snapshot.data) do %SnapshotData{snapshot | created_at: event.created_at} end diff --git a/test/event_store/snapshot_test.exs b/test/event_store/snapshot_test.exs index 1a643fa..706bebe 100644 --- a/test/event_store/snapshot_test.exs +++ b/test/event_store/snapshot_test.exs @@ -22,4 +22,21 @@ defmodule Commanded.EventStore.Adapters.Spear.SnapshotTest do assert %{data: data} = snapshot assert %BankAccountOpened{account_number: 100, initial_balance: :foo} = data end + + test "works with json serializer", %{ + event_store: event_store, + event_store_meta: event_store_meta + } do + event_store_meta = + event_store_meta + |> Map.replace!(:serializer, Commanded.Serialization.JsonSerializer) + |> Map.put(:content_type, "application/json") + + snapshot = build_snapshot_data(100) + assert :ok = event_store.record_snapshot(event_store_meta, snapshot) + + {:ok, snapshot} = event_store.read_snapshot(event_store_meta, snapshot.source_uuid) + assert %{data: data} = snapshot + assert %BankAccountOpened{account_number: 100} = data + end end