Skip to content

Commit

Permalink
Feat: also store stream_uuid on essig_signals table
Browse files Browse the repository at this point in the history
  • Loading branch information
mindreframer committed Sep 5, 2024
1 parent b45fd28 commit c5fb31a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/event_store/append_to_stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Essig.EventStore.AppendToStream do
Essig.Crud.StreamsCrud.update_stream(stream, %{seq: last_event.seq})
end)
|> Ecto.Multi.run(:signal_new_events, fn _repo, _ ->
signal_new_events()
signal_new_events(stream_uuid)
end)
end

Expand Down Expand Up @@ -98,12 +98,16 @@ defmodule Essig.EventStore.AppendToStream do
end
end

defp signal_new_events() do
defp signal_new_events(stream_uuid) do
scope_uuid = Essig.Context.current_scope()
bin_uuid = Ecto.UUID.dump!(scope_uuid)
stream_uuid = Ecto.UUID.dump!(stream_uuid)

{:ok, _} =
Repo.query("insert into essig_signals(scope_uuid) values ($1)", [bin_uuid])
Repo.query("insert into essig_signals(scope_uuid, stream_uuid) values ($1, $2)", [
bin_uuid,
stream_uuid
])

{:ok, true}
end
Expand Down
2 changes: 2 additions & 0 deletions lib/migrations/migration003.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Migrations.Migration003 do
create table(:essig_signals, primary_key: false) do
add(:id, :bigserial, primary_key: true)
add(:scope_uuid, :uuid, null: false, default: fragment("gen_random_uuid()"))
add(:stream_uuid, :uuid, null: false)
add(:txid, :bigint)
add(:snapmin, :bigint)
end
Expand All @@ -27,6 +28,7 @@ defmodule Migrations.Migration003 do
-- Function to notify on new transactions (events) via pg_notify
payload := json_build_object(
'scope_uuid', NEW.scope_uuid,
'stream_uuid', NEW.stream_uuid,
'txid', NEW.txid,
'snapmin', NEW.snapmin
);
Expand Down

0 comments on commit c5fb31a

Please sign in to comment.