aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authornamusyaka <namusyaka@gmail.com>2017-02-10 02:04:28 +0900
committerJeremy Daer <jeremydaer@gmail.com>2017-02-12 18:10:15 -0700
commite8f170cec11873bfaab68d8b24737adb7b9331c6 (patch)
treef23ee5b34de5411a0a5e7e54af49a931ad03ad66 /activerecord
parentacdbe70a02f4eec9e7dc085dfce615a3ddaf9d37 (diff)
downloadrails-e8f170cec11873bfaab68d8b24737adb7b9331c6.tar.gz
rails-e8f170cec11873bfaab68d8b24737adb7b9331c6.tar.bz2
rails-e8f170cec11873bfaab68d8b24737adb7b9331c6.zip
Make `table_name=` reset current statement cache
So queries are not run against the previous table name. Closes #27953
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/active_record/model_schema.rb1
-rw-r--r--activerecord/test/cases/statement_cache_test.rb26
3 files changed, 32 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index c70de5a370..c87bdb160d 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Make `table_name=` reset current statement cache,
+ so queries are not run against the previous table name.
+
+ *namusyaka*
+
* Allow ActiveRecord::Base#as_json to be passed a frozen Hash.
*Isaac Betesh*
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index 2a28c6bf6d..54216caaaf 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -432,6 +432,7 @@ module ActiveRecord
connection.schema_cache.clear_data_source_cache!(table_name)
reload_schema_from_cache
+ initialize_find_by_cache
end
private
diff --git a/activerecord/test/cases/statement_cache_test.rb b/activerecord/test/cases/statement_cache_test.rb
index f45f63c68e..ab81ee1ad6 100644
--- a/activerecord/test/cases/statement_cache_test.rb
+++ b/activerecord/test/cases/statement_cache_test.rb
@@ -105,5 +105,31 @@ module ActiveRecord
refute_equal book, other_book
end
+
+ def test_find_by_does_not_use_statement_cache_if_table_name_is_changed
+ book = Book.create(name: "my book")
+
+ Book.find_by(name: "my book") # warming the statement cache.
+
+ # changing the table name should change the query that is not cached.
+ Book.table_name = :birds
+ assert_nil Book.find_by(name: "my book")
+ ensure
+ Book.table_name = :books
+ end
+
+ def test_find_does_not_use_statement_cache_if_table_name_is_changed
+ book = Book.create(name: "my book")
+
+ Book.find(book.id) # warming the statement cache.
+
+ # changing the table name should change the query that is not cached.
+ Book.table_name = :birds
+ assert_raise ActiveRecord::RecordNotFound do
+ Book.find(book.id)
+ end
+ ensure
+ Book.table_name = :books
+ end
end
end