From 705a8e24ef83fed9c855f214d2bfcddcd300174a Mon Sep 17 00:00:00 2001 From: John Bell Date: Thu, 19 Sep 2024 20:26:16 +0100 Subject: [PATCH] Code review: rename override and move extraction of keywords to apend_to_stream function --- lib/event_store.ex | 15 ++++++--------- lib/event_store/storage/appender.ex | 2 +- lib/event_store/streams/stream.ex | 2 +- test/event_store_test.exs | 26 ++++++++++++++++++++------ 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/lib/event_store.ex b/lib/event_store.ex index 5ad4207c..a02560a8 100644 --- a/lib/event_store.ex +++ b/lib/event_store.ex @@ -294,13 +294,17 @@ defmodule EventStore do Supervisor.stop(supervisor, :normal, timeout) end + @accepted_overrides_append_to_stream [:created_at_override] + def append_to_stream(stream_uuid, expected_version, events, opts \\ []) def append_to_stream(@all_stream, _expected_version, _events, _opts), do: {:error, :cannot_append_to_all_stream} def append_to_stream(stream_uuid, expected_version, events, opts) do + overrides = Keyword.take(opts, @accepted_overrides_append_to_stream) {conn, opts} = parse_opts(opts) + opts = Keyword.merge(opts, overrides) Stream.append_to_stream(conn, stream_uuid, expected_version, events, opts) end @@ -527,16 +531,9 @@ defmodule EventStore do Snapshotter.delete_snapshot(conn, source_uuid, opts) end - @accepted_overrides [:created_at] defp parse_opts(opts) do - overrides = Keyword.take(opts, @accepted_overrides) - - config = - opts - |> name() - |> Config.lookup() - |> Keyword.merge(overrides) - + name = name(opts) + config = Config.lookup(name) conn = Keyword.get(opts, :conn) || Keyword.fetch!(config, :conn) timeout = timeout(opts, config) diff --git a/lib/event_store/storage/appender.ex b/lib/event_store/storage/appender.ex index 46d1d0b1..0b71d3e4 100644 --- a/lib/event_store/storage/appender.ex +++ b/lib/event_store/storage/appender.ex @@ -99,7 +99,7 @@ defmodule EventStore.Storage.Appender do defp insert_event_batch(conn, stream_id, stream_uuid, events, event_count, opts) do {schema, opts} = Keyword.pop(opts, :schema) {expected_version, opts} = Keyword.pop(opts, :expected_version) - {created_at, opts} = Keyword.pop(opts, :created_at) + {created_at, opts} = Keyword.pop(opts, :created_at_override) statement = case expected_version do diff --git a/lib/event_store/streams/stream.ex b/lib/event_store/streams/stream.ex index 2cd1a998..2221b615 100644 --- a/lib/event_store/streams/stream.ex +++ b/lib/event_store/streams/stream.ex @@ -154,7 +154,7 @@ defmodule EventStore.Streams.Stream do %StreamInfo{stream_uuid: stream_uuid, stream_version: stream_version} = stream events - |> Enum.map(&map_to_recorded_event(&1, opts[:created_at] || utc_now(), serializer)) + |> Enum.map(&map_to_recorded_event(&1, opts[:created_at_override] || utc_now(), serializer)) |> Enum.with_index(1) |> Enum.map(fn {recorded_event, index} -> %RecordedEvent{ diff --git a/test/event_store_test.exs b/test/event_store_test.exs index 32b943cd..66aa64e5 100644 --- a/test/event_store_test.exs +++ b/test/event_store_test.exs @@ -208,7 +208,7 @@ defmodule EventStore.EventStoreTest do stream_uuid = UUID.uuid4() events = EventFactory.create_events(1) - :ok = EventStore.append_to_stream(stream_uuid, 0, events, created_at: created_at) + :ok = EventStore.append_to_stream(stream_uuid, 0, events, created_at_override: created_at) [recorded_event] = EventStore.stream_all_forward() |> Enum.to_list() {:ok, stream_info} = EventStore.stream_info(stream_uuid) @@ -224,8 +224,12 @@ defmodule EventStore.EventStoreTest do events = EventFactory.create_events(1) events2 = EventFactory.create_events(1) - :ok = EventStore.append_to_stream(stream_uuid, 0, events, created_at: created_at) - :ok = EventStore.append_to_stream(stream_uuid, :any_version, events2, created_at: created_at2) + :ok = EventStore.append_to_stream(stream_uuid, 0, events, created_at_override: created_at) + + :ok = + EventStore.append_to_stream(stream_uuid, :any_version, events2, + created_at_override: created_at2 + ) [event1, event2] = EventStore.stream_all_forward() |> Enum.to_list() {:ok, stream_info} = EventStore.stream_info(stream_uuid) @@ -240,7 +244,10 @@ defmodule EventStore.EventStoreTest do stream_uuid = UUID.uuid4() events = EventFactory.create_events(1) - :ok = EventStore.append_to_stream(stream_uuid, :any_version, events, created_at: created_at) + :ok = + EventStore.append_to_stream(stream_uuid, :any_version, events, + created_at_override: created_at + ) [recorded_event] = EventStore.stream_all_forward() |> Enum.to_list() {:ok, stream_info} = EventStore.stream_info(stream_uuid) @@ -256,8 +263,15 @@ defmodule EventStore.EventStoreTest do events = EventFactory.create_events(1) events2 = EventFactory.create_events(1) - :ok = EventStore.append_to_stream(stream_uuid, :any_version, events, created_at: created_at) - :ok = EventStore.append_to_stream(stream_uuid, :any_version, events2, created_at: created_at2) + :ok = + EventStore.append_to_stream(stream_uuid, :any_version, events, + created_at_override: created_at + ) + + :ok = + EventStore.append_to_stream(stream_uuid, :any_version, events2, + created_at_override: created_at2 + ) [event1, event2] = EventStore.stream_all_forward() |> Enum.to_list() {:ok, stream_info} = EventStore.stream_info(stream_uuid)