Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure nil is accepted as cached value #505

Merged

Conversation

zerc
Copy link
Contributor

@zerc zerc commented Mar 31, 2023

Context

If there are no previous transitions then nil will be returned and it should be cached. Otherwise, during the initial transition multiple database queries will be made to get the last transition (which doesn't exist).

See #504 for more details

@zerc zerc linked an issue Mar 31, 2023 that may be closed by this pull request
@zerc zerc self-assigned this Mar 31, 2023
@zerc zerc force-pushed the 504-last_transition-cache-doesnt-work-for-newly-created-objects branch 3 times, most recently from a51bc2d to 8678111 Compare March 31, 2023 11:37
@zerc zerc marked this pull request as ready for review March 31, 2023 11:39
@zerc zerc requested a review from JoeSouthan March 31, 2023 11:39
@Tabby
Copy link
Contributor

Tabby commented Mar 31, 2023

Can you please add a test case that fails without this PR and passes with?

I can't see how this makes any meaningful difference in behaviour

zerc added 2 commits April 1, 2023 11:45
It checks the number of db queries done when there are no state
transitions yet.
If there are no previous transitions `nil` will be returned and should
be cached. Otherwise, the caching doesn't work for the initial
transition.
@zerc zerc force-pushed the 504-last_transition-cache-doesnt-work-for-newly-created-objects branch from 8678111 to 8b8a9bb Compare April 1, 2023 10:45
Comment on lines -311 to -312
before { adapter.create(:x, :y) }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In tests of the #last method we were always creating a transition. This is not how it happens in reality.

expect(adapter.last).to eq(nil)
expect(adapter.last).to eq(nil)
end
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test verifies that the caching of the #last method works even when there are no transitions been made yet.

else
@last_transition ||= history.last
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tl;dr; this caching approach doesn't work when history.last returns nil


When the object is just created and there are no sate transitions, history.last returns nil because:

transitions_for_parent.order(:sort_key) => []
transitions_for_parent.order(:sort_key).last => nil

and then it's stored in @last_transition variable.

If we call #last again (which happens when there are multiple guard_transition against the initial state transition) this statement:

@last_transition ||= history.last

will call history.last again which would result in the database query (which will return zero rows).

@zerc
Copy link
Contributor Author

zerc commented Apr 1, 2023

@Tabby I've added the test and some comments to clarify the problem.

@zerc zerc requested a review from Tabby April 1, 2023 10:57
Copy link
Contributor

@Tabby Tabby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the change and explanation. I see what's happening now, and this change looks good to me :)

@zerc zerc merged commit e6c11eb into master Apr 3, 2023
56 checks passed
@zerc zerc deleted the 504-last_transition-cache-doesnt-work-for-newly-created-objects branch April 3, 2023 09:01
@zerc zerc mentioned this pull request Apr 3, 2023
zerc added a commit that referenced this pull request Apr 3, 2023
This was introduced in #505 and caused an error being thrown if one
attempts to reset the cache of `last_transition`.
zerc added a commit that referenced this pull request Apr 3, 2023
This was introduced in #505 and caused an error being thrown if one
attempts to reset the cache of `last_transition`.
dylan-hoefsloot pushed a commit to dylan-hoefsloot/statesman that referenced this pull request Jan 11, 2024
This was introduced in gocardless#505 and caused an error being thrown if one
attempts to reset the cache of `last_transition`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

last_transition cache doesn't work for newly created objects
2 participants