aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/query_cache_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Fix query cache when using shared connectionsHeinrich Lee Yu2019-07-081-0/+17
| | | | | Enables the query cache on the correct connection when shared connections across threads are enabled
* Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-131-1/+0
| | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* All modern adapters returns a numeric value as the result of numeric calculationRyuta Kamizono2019-06-111-5/+1
|
* Invalidate query cache for all connections in the current threadEileen Uchitelle2019-02-011-0/+38
| | | | | | This change ensures that all query cahces are cleared across all connections per handler for the current thread so if you write on one connection the read will have the query cache cleared.
* No need to handle if FrozenError is availableYasuo Honda2018-12-231-1/+1
| | | | | | | Rails 6 requires Ruby 2.5, which introduces `FrozenError` https://docs.ruby-lang.org/en/2.5.0/NEWS.html Related to #31520
* Merge pull request #34753 from ↵Eileen M. Uchitelle2018-12-211-0/+5
|\ | | | | | | | | eileencodes/raise-less-confusing-error-if-handler-doesnt-exist Raise helpful error when role doesn't exist
| * Raise helpful error when role doesn't existEileen Uchitelle2018-12-211-0/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If you try to call `connected_to` with a role that doesn't have an established connection you used to get an error that said: ``` >> ActiveRecord::Base.connected_to(role: :i_dont_exist) { Home.first } ActiveRecord::ConnectionNotEstablished Exception: No connection pool with 'primary' found. ``` This is confusing because the connection could be established but we spelled the role wrong. I've changed this to raise if the `role` used in `connected_to` doesn't have an associated handler. Users who encounter this should either check that the role is spelled correctly (writin -> writing), establish a connection to that role in the model with connects_to, or use the `database` keyword for the `role`. I think this will provide a less confusing error message for those starting out with multiple databases.
* | Enable `Style/RedundantBegin` cop to avoid newly adding redundant begin blockRyuta Kamizono2018-12-211-57/+53
|/ | | | | | | | | | Currently we sometimes find a redundant begin block in code review (e.g. https://github.com/rails/rails/pull/33604#discussion_r209784205). I'd like to enable `Style/RedundantBegin` cop to avoid that, since rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5 (https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with that situation than before.
* Fix query cache for multiple connectionsEileen Uchitelle2018-11-201-0/+16
| | | | | | | | | | Currently the query cache is only aware of one handler so once we added multiple databases switching on the handler we broke query cache for those reading connections. While #34054 is the proper fix, that fix is not straight forward and I want to make sure that the query cache isn't just broken for all other connections not in the main handler.
* Use `assert_no_queries` not to ignore BEGIN/COMMIT queriesRyuta Kamizono2018-10-051-3/+4
| | | | | | | | | `test_update_does_not_run_sql_if_record_has_not_changed` would pass without #18501 since `assert_queries` ignores BEGIN/COMMIT unless `ignore_none: true` is given. Since #32647, empty BEGIN/COMMIT is ommited. So we no longer need to use `assert_queries(0)` to ignore BEGIN/COMMIT in the queries.
* No private def in the codebaseRafael Mendonça França2018-09-211-10/+11
|
* Enable `Layout/EmptyLinesAroundBlockBody` to reduce review cost in the futureRyuta Kamizono2018-07-121-1/+0
| | | | | | | We sometimes ask "✂️ extra blank lines" to a contributor in reviews like https://github.com/rails/rails/pull/33337#discussion_r201509738. It is preferable to deal automatically without depending on manpower.
* Add ability to configure cache notifications infoEileen Uchitelle2018-06-121-1/+23
| | | | | | | | | | | | | | | | | | | | | | This may seem like an unnecessary refactoring but some apps want / need to configure the information passed to the query cache logger. In order to do that we can add a method here that can be easily overridden by the app itself, rather than hacking the query cache logger to include that information. To override apps can call ``` def cache_notifications_info super.merge(connected_host: "hostname") end ``` This will take what's already in the query cache logger and add `@something="yea"` to the object. At GitHub we use this to log the number of queries that are cached, the connection host and the connection url.
* Fix `CustomCops/AssertNot` to allow it to have failure messageRyuta Kamizono2018-05-131-2/+2
| | | | Follow up of #32605.
* Replace `assert !` with `assert_not`Daniel Colson2018-04-191-3/+3
| | | | | This autocorrects the violations after adding a custom cop in 3305c78dcd.
* Optimize the code inside AR::QueryCache middlewareBogdan Gusiev2018-04-041-1/+2
|
* Use the query cache when connection is already connectedyuuji.yaginuma2018-02-191-0/+11
| | | | Fixes #32021.
* Deprecate update_attributes and update_attributes!Eddie Lebow2018-02-171-2/+2
| | | | Closes #31998
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-251-3/+3
|
* Change refute to assert_notDaniel Colson2018-01-251-7/+7
|
* Handle `FrozenError` if it is availableYasuo Honda2017-12-201-1/+1
| | | | | | | | | | | | | | | This pull request handles `FrozenError` introduced by Ruby 2.5. Refer https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/61131 Since `FrozenError` is a subclass of `RuntimeError` minitest used by master branch can handle it, though it would be better to handle `FrozenError` explicitly if possible. `FrozenError` does not exist in Ruby 2.4 or lower, `frozen_error_class` handles which exception is expected to be raised. This pull request is intended to be merged to master, then backported to `5-1-stable` to address #31508
* Merge pull request #28869 from eugeneius/query_cache_all_poolsMatthew Draper2017-11-171-0/+9
|\ | | | | Enable query cache on all connection pools
| * Enable query cache on all connection poolsEugene Kenny2017-04-241-0/+9
| | | | | | | | | | | | Since the query cache no longer eagerly checks out a connection, we can enable it on all connection pools at the start of every request, and it will only take effect for requests that actually use those pools.
* | sqlite3 adapter returns integer value which used to be stringYasuo Honda2017-09-011-7/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `to_i` was added for SQLite3 adapter which did not handle number but sqlite3 gem already supports it then `to_i` is unnecessary. else condition is kept for adapters which return string, i.e. mysql(not mysql2) and sqlserver. Renamed `test_cache_does_not_wrap_string_results_in_arrays` to `test_cache_does_not_wrap_results_in_arrays` to explain the current behavior. most of adapters return integer, not only string. * Refer these commits: "future proofing the sqlite3 adapter code" https://github.com/rails/rails/commit/beda2d43d6ac5c3435fc2fba0cbd108c20fe1c67 "Refactor calculation test to remove unneeded SQLite special case." https://github.com/rails/rails/commit/47d568ed3fc701934ebe80b276f3d8bf6951c93f "no need to to_i, sqlite does that for us" https://github.com/rails/rails/commit/6cf44a1bd64ba10497742d70ad78fe68faa16e99
* | Merge remote-tracking branch 'origin/master' into unlock-minitestRafael Mendonça França2017-08-011-3/+3
|\ \
| * \ Merge pull request #29869 from kamipo/make_type_map_to_privateRafael França2017-07-211-3/+1
| |\ \ | | | | | | | | Make `type_map` to private because it is only used in the connection adapter
| | * | Make `type_map` to private because it is only used in the connection adapterRyuta Kamizono2017-07-201-3/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | `type_map` is an internal API and it is only used in the connection adapter. And also, some type map initializer methods requires passed `type_map`, but those instances already has `type_map` in itself. So we don't need explicit passing `type_map` to the initializers.
| * | | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
| |/ /
* | | Merge branch 'master' into unlock-minitestKasper Timm Hansen2017-07-151-23/+53
|\| |
| * | Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | | | | | | | | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
| * | Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
| | |
| * | Should use the same connection in using query cacheRyuta Kamizono2017-06-291-7/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `test_cache_is_available_when_using_a_not_connected_connection` is always failed if running only the test since #29609. ``` % ARCONN=mysql2 be ruby -w -Itest test/cases/query_cache_test.rb -n test_cache_is_available_when_using_a_not_connected_connection Using mysql2 Run options: -n test_cache_is_available_when_using_a_not_connected_connection --seed 15043 F Finished in 0.070519s, 14.1806 runs/s, 28.3612 assertions/s. 1) Failure: QueryCacheTest#test_cache_is_available_when_using_a_not_connected_connection [test/cases/query_cache_test.rb:336]: 2 instead of 1 queries were executed. Queries: SELECT `tasks`.* FROM `tasks` WHERE `tasks`.`id` = ? LIMIT ? SET NAMES utf8 COLLATE utf8_unicode_ci, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483. Expected: 1 Actual: 2 1 runs, 2 assertions, 1 failures, 0 errors, 0 skips ``` This failure is due to `LogSubscriber` will use not connected `ActiveRecord::Base.connection` even if `Task.connection` is connected. I fixed to always pass `type_casted_binds` to log subscriber to avoid the issue.
| * | Merge pull request #29614 from kamipo/show_query_cache_keys_2Rafael França2017-06-281-1/+2
| |\ \ | | | | | | | | Show query cache keys in `test_middleware_caches`
| | * | Show query cache keys in `test_middleware_caches`Ryuta Kamizono2017-06-291-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | `test_middleware_caches` also failed same as #29600. https://travis-ci.org/rails/rails/jobs/248017174#L487-L489
| * | | Enable query cache if set a configurationsTsukasa OISHI2017-06-291-14/+2
| |/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ActiveRecord query cache is available when a connection is connected. Therefore, query cache is unavailable when entering the ActiveRecord::Base.cache block without being connected. ```ruby ActiveRecord::Base.cache do Task.find(1) # access to database. Task.find(1) # access to database. unavailable query cache end ``` If we use query cache with batch script etc, we need to connect before that. ```ruby Task.connection ActiveRecord::Base.cache do Task.find(1) # access to database. Task.find(1) # available query cache end ``` Before version 3.1, query cache had been enabled if a configuration was set up. In order to solve the `DATABASE_URL` issue(#8074), ActiveRecord has checked whether a connection is connected or not. Today, ActiveRecord.configurations respect `DATABASE_URL`. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/core.rb#L46
| * | Show query cache keys in ↵Ryuta Kamizono2017-06-281-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `test_exceptional_middleware_clears_and_disables_cache_on_error` `test_exceptional_middleware_clears_and_disables_cache_on_error` in postgresql adapter sometime fails recently. Show the query cache keys to investigte the cause. https://travis-ci.org/rails/rails/jobs/246467252#L490-L493
| * | Ensure query caching for `select_*` methods in connection adaptersRyuta Kamizono2017-06-151-0/+40
| | |
| * | Fix `Relation#exists?` queries with query cacheRyuta Kamizono2017-06-151-0/+6
| |/ | | | | | | | | | | | | | | If a connection adapter overrides `select_*` methods, query caching will doesn't work. This patch changes `select_value` to `select_one` in `Relation#exists?` to ensure query caching. Fixes #29449.
* / Only clean the connection of the current connection poolRafael Mendonça França2017-04-261-1/+1
|/ | | | This will avoid us to close the connection of the saved connection pool.
* Ensure test threads share a DB connectioneileencodes2017-02-201-0/+12
| | | | | | | | | | | | | | | | | | | | | | | This ensures multiple threads inside a transactional test to see consistent database state. When a system test starts Puma spins up one thread and Capybara spins up another thread. Because of this when tests are run the database cannot see what was inserted into the database on teardown. This is because there are two threads using two different connections. This change uses the statement cache to lock the threads to using a single connection ID instead of each not being able to see each other. This code only runs in the fixture setup and teardown so it does not affect real production databases. When a transaction is opened we set `lock_thread` to `Thread.current` so we can keep track of which connection the thread is using. When we rollback the transaction we unlock the thread and then there will be no left-over data in the database because the transaction will roll back the correct connections. [ Eileen M. Uchitelle, Matthew Draper ]
* Use temporary connection pool for sqlite3_mem adapterYasuo Honda2017-01-311-11/+22
| | | | | Fixes #27826 Refer https://github.com/rails/rails/commit/f7b317175430a2d9300d9c4acfc1f34f4fdb2fbc
* Make sure to clear query cache to prevent sporadic test failureAkira Matsuda2017-01-181-0/+4
|
* oops! :scream_cat:Akira Matsuda2017-01-101-1/+1
|
* Use temporary connection pool for the tests clearing AR::Base's ↵Akira Matsuda2017-01-101-59/+92
| | | | | | | active_connections clearing AR::Base's active_connections on the "primary" pool loses connections to the in_memory DB when running sqlite3_mem tests
* Fix style guide violationsRafael Mendonça França2017-01-051-3/+3
|
* Merge pull request #25460 from maclover7/jm-uncachedSean Griffin2017-01-031-0/+21
|\ | | | | Add explicit testing for `uncached` vs. `cached`
| * Add explicit testing for `uncached` vs. `cached`Jon Moss2016-06-211-0/+21
| | | | | | | | | | | | There are a ton of middleware related tests in this file, and it seems like `cached vs. `uncached` is being neglected. Added in a test to confirm the expected behavior.
* | fix QueryCache nil dupRichard Monette2016-12-151-0/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | make sql statements frozen dup if arel is not our string expect runtime error dont wrap runtime error in invalid log errors will now be treated as runtime errors update changelog
* | Configure query caching (per thread) on the connection poolMatthew Draper2016-11-061-0/+34
| |
* | Clear query cache during checkin, instead of an execution callbackMatthew Draper2016-10-271-28/+68
| | | | | | | | | | | | | | It doesn't make sense for the query cache to persist while a connection moves through the pool and is assigned to a new thread. [Samuel Cochran & Matthew Draper]