rathoud96
Commanded aggregate state is different than projection
I am working with commanded by @slashdotdash. Recently I encountered an issue where my aggregate’s state was different than my projection.
For example the aggregate state is %Aggregate.Example{is_valid: false} and my projection is %Projection.Example{is_valid: true}
Now I am stuck on how to sync them or is there a way the state can be updated?
Thanks
Most Liked
tcoopman
I’m lacking a bit of context to give a good response.
Do you mean your projection in production has a bug so that it’s state is wrong? Did you forget to handle an event? Or something else?
If it’s a bug in your projection, the default way to solve this would be to fix the bug, deploy it and then replay the eventstream.
Last Post!
tcoopman
On my phone, so I’ll do my best to respond.
I’m going to take a step back from commanded and talk about event sourcing in general for a moment.
In event sourcing an aggregate takes a list of events (it’s own history) and a command and it makes a decision. That decision is communicated by returning one or more events.
The aggregate should be pure. Meaning it should make the decision only based on the information in the history of the events and the command. It should do no queries or other side effects to make a decision.
I write should. I mean I strongly suggest doing it that way. It will give you easier to understand code. Easier to test code,…
Looking at your code it looks like you’re doing a query in the aggregate. From a design perspective I would try to extract that.
It’s not clear though where you’re doing the query in the aggregate. Is it when making the decision? So where you handle the command? Or is it in the apply function?
If it’s in the apply function it is definitely wrong. Your apply function MUST be pure.
If it’s in the code making a decision. The only thing I can imply is that at the time of the command the author was not in the DB. I would expect a different event though depending on if the author is in or not, so the projection should follow and have the same information.
Hopefully this makes sense. If not let me know.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









