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

Clear @cache_keys cache after update_all, delete_all, destroy_all #41789

Merged
merged 1 commit into from Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions activerecord/lib/active_record/relation.rb
Expand Up @@ -488,7 +488,7 @@ def update_all(updates)
stmt = arel.compile_update(values, table[primary_key])
stmt.table(source)

klass.connection.update(stmt, "#{klass} Update All")
klass.connection.update(stmt, "#{klass} Update All").tap { reset }
end

def update(id = :all, attributes) # :nodoc:
Expand Down Expand Up @@ -616,10 +616,7 @@ def delete_all
stmt = arel.compile_delete(table[primary_key])
stmt.from(source)

affected = klass.connection.delete(stmt, "#{klass} Destroy")

reset
affected
klass.connection.delete(stmt, "#{klass} Destroy").tap { reset }
end

# Finds and destroys all records matching the specified conditions.
Expand Down Expand Up @@ -701,6 +698,7 @@ def reset
@delegate_to_klass = false
@to_sql = @arel = @loaded = @should_eager_load = nil
@offsets = @take = nil
@cache_keys = nil
@records = nil
self
end
Expand Down
28 changes: 28 additions & 0 deletions activerecord/test/cases/collection_cache_key_test.rb
Expand Up @@ -7,6 +7,7 @@
require "models/topic"
require "models/post"
require "models/comment"
require "models/ship"

module ActiveRecord
class CollectionCacheKeyTest < ActiveRecord::TestCase
Expand Down Expand Up @@ -101,6 +102,33 @@ class CollectionCacheKeyTest < ActiveRecord::TestCase
assert_match(/\Acomments\/query-(\h+)-(\d+)-(\d+)\z/, comments.cache_key)
end

test "update_all will update cache_key" do
developers = Developer.where(name: "David")
cache_key = developers.cache_key

developers.update_all(updated_at: Time.now.utc)

assert_not_equal cache_key, developers.cache_key
end

test "delete_all will update cache_key" do
developers = Developer.where(name: "David")
cache_key = developers.cache_key

developers.delete_all

assert_not_equal cache_key, developers.cache_key
end

test "destroy_all will update cache_key" do
developers = Developer.where(name: "David")
cache_key = developers.cache_key

developers.destroy_all

assert_not_equal cache_key, developers.cache_key
end

test "it triggers at most one query" do
developers = Developer.where(name: "David")

Expand Down