How to "Time Travel" in general? #227
Replies: 1 comment
-
Alright....i tried something differently that still does not work completely, but i feel it is close to the solution. I've created a reset event. That reset event signals the consumers that the aggregate root was reset to its initial state. This allows consumers to reset their state too. For example, in my case, it clears all records in a mysql database that are associated with the aggregate root. After that reset event, i retrieve the messages like i've outlined in the previous comment. Then i user the messageRepository's persistsEvents method to persist them. At first, that looked fine! But then i did have a look at my events table. And the version column did not display the correct version numbers for the persisted events. My Reset event was the last one that was correct. It had 8 as its version number, the persisted events version numbers where 1 till 7 as one might expect. But.....In this case, because i want to time travel, the version numbers should be 9, 10, 11 etc right? I did had a look at the implementation of the persistEvents method and it makes sense on how it came at the version number of 1 to 7. But....how should i fix this? I also tried passing a different version to the persistEvents method like this: But that feels like that is not the way to go? Because i need to add the count of messages to the aggregateRootVersion |
Beta Was this translation helpful? Give feedback.
-
Hello everyone,
First of all, thank you for this package. I wasn't familiar with the event sourcing term up till last year. I've watched the complete eventsourcery youtube playlist and i like it! Now, i do have a project setup and i've implemented AggregateIds, Aggregates, implementations of SerializablePayload, all kinds of Consumers, and a IlluminateMessageRepository. And now i can perfectly work with my aggregate roots to store and emit events. Nice!
The next step i did take was implement ReplayMessages, so i could easily rebuild my read only mysql database. That also works.
Now i am curious on how one could implement a time travel mechanism. This is what i'm doing so far:
Search for an aggregate root id
I present the user with a way in which he can search for a given aggregate root id. My read only database has a column in which the aggregate root id is stored. I do have a question about this. Is this how i should keep track of aggregate root ids? Or is there a better way via the event store itself for example.
Lookup the messages which are applied for the aggregate root id
I then lookup all the messages which are applied for the aggregate root id as follows.
OffsetCursor::fromStart(PHP_INT_MAX)
$this->messageRepository->paginate($offsetCursor)
$message->aggregateRootId()?->toString() !== $aggregateRootId->toString()
Present the messages to the user
I then present the user with the message representations from the previous step. And let the user select an event to
"time travel too". The result of the selection will be the aggregateVersion of the choosen message representation.
Retrieve the payloads up till the choosen aggregateVersion
I then again create a OffsetCursor and use the paginate method on the message repository like in previous step to loop over all the messages and retrieve all the messages, up till the given aggregateVersion.
$message->aggregateVersion() > $aggregateVersion
. I map these messages, so i do have a list of payloads from them as the result.Reconstitute the aggregate root using the payloads
At this point i can use the messages to reconstitute the aggregteRoot like this using the serializeablePayloads from previous step.
The result is the aggregate root, but from the past.
Try to time travel
Now, here's the part where i question my self the most. Now i can time travel i guess.
First i retrieve the current (present) aggregate root like this:
Then i've created a method on the aggregate root to "time travel" using the aggregate root from the past. I use it like this
Now i do have difficulties on how to implement the timeTravelTo method. This is what i currently have
Now, is this how you should time travel? Or what would be the way in general? Important is that i want to store the aggregate root fro m the past. But what would be the best way. Since that would require a "TimeTravel" event with all the aggregateRoots data from up till that point?
Hopefully it makes sense what i am trying to accomplish. I real life, my aggregate root isn't an instance of MyAggregateRoot, but something else of course.
Beta Was this translation helpful? Give feedback.
All reactions