Repository navigation
Publish events as they are stored, so they project the same when replayed - #558
Draft
erikrozendaal wants to merge 3 commits into
Draft
erikrozendaal wants to merge 3 commits into
erikrozendaal wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved serialization issues can produce different live, replay, and dry-run behavior, while verification can mutate timestamps and a new test fails on supported Ruby 3.3.
6 open issues
Replay skips PostgreSQL metadata normalization · New Dry runs skip event normalization · New Hard-coded hash formatting breaks Ruby 3.3 CI · New Hash comparison misses indifferent-access behavior changes · New Time precision exemption accepts deserialized type changes · New UTC verification mutates original event timestamps · New
What changed in this PR
This PR aims to make live event handling match replay behavior by publishing serialized-and-deserialized events, while leaving the aggregate’s applied values unchanged.
Changes:
- Normalize published events in the real and fake event stores.
- Add optional serialization verification to detect changes in event values.
- Add regression specs and document the new behavior and setting.
| File | Description |
|---|---|
spec/lib/sequent/test/test_spec.rb |
Tests fake-store normalization and verification. |
spec/lib/sequent/core/event_store_spec.rb |
Tests publication consistency and serialization errors. |
lib/sequent/test/command_handler_helpers.rb |
Normalizes events published by the fake store. |
lib/sequent/core/event_store.rb |
Normalizes events before publication. |
lib/sequent/core/event_serialization.rb |
Adds event round-tripping and value verification. |
lib/sequent/configuration.rb |
Adds the disabled-by-default verification setting. |
docs/docs/concepts/event.md |
Explains publication behavior and verification. |
docs/docs/concepts/configuration.md |
Documents the new setting. |
CHANGELOG.md |
Announces the behavior change and verification option. |
Review effort: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved serialization issues can produce publish/replay differences in hash order and timestamp precision, reject date-only timestamps, and make fake event-store operations depend on a database.
4 open issues
6 resolved since last review
Review effort: Balanced
| # The events table keeps the aggregate_id, sequence_number and created_at in columns, from which they are loaded: | ||
| # the aggregate_id as a lowercase UUID, and the created_at in the time zone of the database session. | ||
| def loaded_event_json(event_json) | ||
| created_at = event_json['created_at'] && Time.iso8601(event_json['created_at']) |
| created_at = event_json['created_at'] && Time.iso8601(event_json['created_at']) | ||
| event_json.merge( | ||
| 'aggregate_id' => event_json['aggregate_id']&.downcase, | ||
| 'created_at' => created_at && convert_timestamp(created_at).iso8601(Sequent.configuration.time_precision), |
Comment on lines
+49
to
+50
| serialized = Oj.strict_load(Oj.dump(serialize_event(event))) | ||
| stored = deserialize_event_json(serialized['event_type'], loaded_event_json(serialized['event_json'])) |
| def serialize_events(events) | ||
| events.map { |event| [event.class.name, Sequent::Core::Oj.dump(event)] } | ||
| events.map { |event| Sequent::Core::Oj.dump(serialize_event(event)) } | ||
| end |
…ayed The event store published the events the aggregate applied, while a replay deserializes them from the JSON in the event store. Times were truncated to the time_precision, symbols became strings and attributes were cast to their type only when replaying, so a projector could store other values live than after a replay. The event stores, including the FakeEventStore of the test helpers, now publish the events as they are deserialized from the JSON they store. The aggregate keeps the values it applied. The new verify_event_serialization setting fails storing an event that changes other than by truncating its times, so specs can point out the domain code that creates values it does not store.
…as loaded The EventStore and the FakeEventStore each serialized and deserialized events themselves, and the round trip for publishing added a third way. They now share Helpers::EventSerialization, which deserializes events like a replay does, and publish the events using its stored_event. The EventStore also applies what loading the events from their columns does: the aggregate_id as a lowercase UUID and created_at in the time zone of the database session. A dry run publishes and records the events the same way. Verifying the serialization no longer converts the times of the event to UTC in place, only accepts actual times as truncated times, and reports a hash that changes its class, like one with indifferent access, or its values.
erikrozendaal
force-pushed
the
improvement/publish-stored-events
branch
from
November 4, 2026 11:57
14fa8c1 to
287eaae
Compare
erikrozendaal
changed the base branch from
master
to
improvement/remove-text-json-columns
November 4, 2026 11:57
Serializing an event is dumping it with its class name next to it, and each event store stores that in its own way, so serialize_event only wrapped it to be unwrapped again. The event stores parse their JSON before deserializing it, so deserialize_event_json only takes the parsed JSON.
Base automatically changed from
improvement/remove-text-json-columns
to
master
November 4, 2026 17:01
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Builds on #559, which simplifies how events are deserialized.
A projection should be the same whether its events are projected right after they are stored or during a replay. It was not, because projectors got the event objects the aggregate applied, while a replay deserializes the events from their JSON: times lose their sub-millisecond precision, symbols become strings and values are only cast to their attribute type when deserializing. So a projector could store other values live than after a replay, or behave differently when it reads, for example, a hash key.
The event stores now publish the events as they are stored, so projectors and workflows see the same values live and when replaying. The aggregate still keeps the values it applied, so a new
verify_event_serializationsetting lets specs fail on events that change when they are stored, pointing out the domain code to fix.