1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Reset cache_versions on relation

Currently, when #reset is called on a relation object it does not reset
the cache_versions ivar. This can lead to a confusing and buggy
situation where despite having the correct data the relation is still
reporting a stale cache_version. Resetting this ivar along with the
other relation state corrects this situation.

Update test assertion

Assert the specific cache_version matches the latest .all relation

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
This commit is contained in:
Austen Madden 2022-06-13 11:30:52 -04:00
parent 6ee37a3917
commit a237867e8f
3 changed files with 37 additions and 0 deletions

View file

@ -1,3 +1,26 @@
* Resolve issue where a relation cache_version could be left stale.
Previously, when `reset` was called on a relation object it did not reset the cache_versions
ivar. This led to a confusing situation where despite having the correct data the relation
still reported a stale cache_version.
Usage:
```ruby
developers = Developer.all
developers.cache_version
Developer.update_all(updated_at: Time.now.utc + 1.second)
developers.cache_version # Stale cache_version
developers.reset
developers.cache_version # Returns the current correct cache_version
```
Fixes #45341.
*Austen Madden*
* Add support for exclusion constraints (PostgreSQL-only).
```ruby

View file

@ -713,6 +713,7 @@ module ActiveRecord
@to_sql = @arel = @loaded = @should_eager_load = nil
@offsets = @take = nil
@cache_keys = nil
@cache_versions = nil
@records = nil
self
end

View file

@ -254,6 +254,19 @@ module ActiveRecord
end
end
test "reset will reset cache_version" do
with_collection_cache_versioning do
developers = Developer.all
assert_equal Developer.all.cache_version, developers.cache_version
Developer.update_all(updated_at: Time.now.utc + 1.second)
developers.reset
assert_equal Developer.all.cache_version, developers.cache_version
end
end
test "cache_key_with_version contains key and version regardless of collection_cache_versioning setting" do
key_with_version_1 = Developer.all.cache_key_with_version
assert_match(/\Adevelopers\/query-(\h+)-(\d+)-(\d+)\z/, key_with_version_1)