diff --git a/lib/event_store/base_query.ex b/lib/event_store/base_query.ex new file mode 100644 index 0000000..b9bb5a6 --- /dev/null +++ b/lib/event_store/base_query.ex @@ -0,0 +1,11 @@ +defmodule EventStore.BaseQuery do + alias Essig.Schemas.Event + use Essig.Repo + + def query() do + scope_uuid = Essig.Context.current_scope() + + from(event in Event) + |> where([event], event.scope_uuid == ^scope_uuid) + end +end diff --git a/lib/event_store/read_all_stream_backward.ex b/lib/event_store/read_all_stream_backward.ex index 87cd5e0..4b112a4 100644 --- a/lib/event_store/read_all_stream_backward.ex +++ b/lib/event_store/read_all_stream_backward.ex @@ -7,7 +7,7 @@ defmodule Essig.EventStore.ReadAllStreamBackward do end def query(from_id, amount) do - from(event in Event) + EventStore.BaseQuery.query() |> where([event], event.id < ^from_id) |> order_by(desc: :id) |> limit(^amount) diff --git a/lib/event_store/read_all_stream_forward.ex b/lib/event_store/read_all_stream_forward.ex index 0df468c..490c386 100644 --- a/lib/event_store/read_all_stream_forward.ex +++ b/lib/event_store/read_all_stream_forward.ex @@ -1,5 +1,4 @@ defmodule Essig.EventStore.ReadAllStreamForward do - alias Essig.Schemas.Event use Essig.Repo def run(from_id, amount) do @@ -7,7 +6,7 @@ defmodule Essig.EventStore.ReadAllStreamForward do end def query(from_id, amount) do - from(event in Event) + EventStore.BaseQuery.query() |> where([event], event.id > ^from_id) |> order_by(asc: :id) |> limit(^amount) diff --git a/lib/event_store/read_stream_backward.ex b/lib/event_store/read_stream_backward.ex index 6ed790d..fe59b02 100644 --- a/lib/event_store/read_stream_backward.ex +++ b/lib/event_store/read_stream_backward.ex @@ -1,13 +1,12 @@ defmodule Essig.EventStore.ReadStreamBackward do use Essig.Repo - alias Essig.Schemas.Event def run(stream_uuid, from_seq, amount) do query(stream_uuid, from_seq, amount) |> Repo.all() end def query(stream_uuid, from_seq, amount) do - from(Event) + EventStore.BaseQuery.query() |> where([event], event.stream_uuid == ^stream_uuid) |> where([event], event.seq < ^from_seq) |> order_by(desc: :seq) diff --git a/lib/event_store/read_stream_forward.ex b/lib/event_store/read_stream_forward.ex index 0339ee7..e0dd73f 100644 --- a/lib/event_store/read_stream_forward.ex +++ b/lib/event_store/read_stream_forward.ex @@ -1,13 +1,12 @@ defmodule Essig.EventStore.ReadStreamForward do use Essig.Repo - alias Essig.Schemas.Event def run(stream_uuid, from_seq, amount) do query(stream_uuid, from_seq, amount) |> Repo.all() end def query(stream_uuid, from_seq, amount) do - from(Event) + EventStore.BaseQuery.query() |> where([event], event.stream_uuid == ^stream_uuid) |> where([event], event.seq > ^from_seq) |> order_by(asc: :id) diff --git a/lib/event_store_test.exs b/lib/event_store_test.exs index 4449a85..f23fd5c 100644 --- a/lib/event_store_test.exs +++ b/lib/event_store_test.exs @@ -186,6 +186,37 @@ defmodule Essig.EventStoreTest do end end + describe "respects the current scope" do + test "when the scope is switched, the events from other scopes are not accessible and the seq order is correct!" do + scope1 = Essig.Context.current_scope() + + Essig.Server.start_scope(scope1) + uuid1 = Essig.Ecto.UUID7.generate() + init_stream(uuid1, 0) + + scope2 = Essig.Ecto.UUID7.generate() + Essig.Server.start_scope(scope2) + uuid2 = Essig.Ecto.UUID7.generate() + init_stream(uuid2, 0) + + # switch to scope1 + Essig.Server.start_scope(scope1) + events1 = Essig.EventStore.read_all_stream_forward(0, 3) + + # switch to scope2 + Essig.Server.start_scope(scope2) + events2 = Essig.EventStore.read_all_stream_forward(0, 3) + + assert events1 != events2 + + assert Enum.map(events1, & &1.seq) == [1, 2, 3] + assert Enum.map(events2, & &1.seq) == [1, 2, 3] + + assert Enum.map(events1, & &1.scope_uuid) |> Enum.uniq() == [scope1] + assert Enum.map(events2, & &1.scope_uuid) |> Enum.uniq() == [scope2] + end + end + def anonym_ids(list) when is_list(list) do Enum.map(list, &anonym_ids/1) end