Skip to content

Commit

Permalink
Code review: rename override and move extraction of keywords to apend…
Browse files Browse the repository at this point in the history
…_to_stream function
  • Loading branch information
Johnabell committed Sep 19, 2024
1 parent 462d321 commit 705a8e2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
15 changes: 6 additions & 9 deletions lib/event_store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/event_store/storage/appender.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/event_store/streams/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
26 changes: 20 additions & 6 deletions test/event_store_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 705a8e2

Please sign in to comment.