1. 03 1月, 2020 2 次提交
    • R
      14149105
    • A
      Enforce fresh ETag header after collection changes · 58b04096
      Aaron Lipman 提交于
      Add ActiveRecord::Relation#cache_key_with_version. This method will be
      used by ActionController::ConditionalGet to ensure that when collection
      cache versioning is enabled, requests using ConditionalGet don't return
      the same ETag header after a collection is modified.
      
      Prior to the introduction of collection cache versioning in
      4f2ac80d, all collection cache keys
      included a version. However, with cache versioning enabled, collection
      cache keys remain constant. In turn, ETag headers remain constant,
      rendering them ineffective.
      
      This commit takes the cache_key_with_version method used for individual
      Active Record objects (from aa8749eb),
      and adds it to collections.
      58b04096
  2. 27 12月, 2019 2 次提交
    • Y
      Add test case about assigning nil in enum · f5ec5246
      Yoshiyuki Hirano 提交于
      This commit will be squished into f4fbdb1b after maintainer's review.
      f5ec5246
    • E
      Clear callback triggers when transaction completes · d3060af7
      Eugene Kenny 提交于
      The `_trigger_update_callback` and `_trigger_destroy_callback`
      attributes were added in 9252da96 to
      avoid running transactional callbacks when an attempt to modify a record
      fails inside a transaction due to the record being invalid, for example.
      
      However the values weren't being reset between transactions, which meant
      they leaked from one transaction to another and caused false positives
      where unsuccessful modifications still triggered callbacks. Clearing
      them when a transaction commits or is rolled back fixes the problem.
      d3060af7
  3. 25 12月, 2019 1 次提交
    • Y
      Allow AR::Enum definitions with boolean values · f4fbdb1b
      Yoshiyuki Hirano 提交于
      If `AR::Enum` is used for boolean field, it would be not expected
      behavior for us.
      
      fixes #38075
      
      Problem:
      
      In case of using boolean for enum, we can set with string (hash key)
      to instance, but we cannot set with actual value (hash value).
      
      ```ruby
      class Post < ActiveRecord::Base
        enum status: { enabled: true, disabled: false }
      end
      
      post.status = 'enabled'
      post.status # 'enabled'
      
      post.status = true
      post.status # 'enabled'
      
      post.status = 'disabled'
      post.status # 'disabled'
      
      post.status = false
      post.status # nil (This is not expected behavior)
      ```
      
      After looking into `AR::Enum::EnumType#cast`, I found that `blank?`
      method converts from false value to nil (it seems it may not intentional behavior).
      
      In this patch, I improved that if it defines enum with boolean,
      it returns reasonable behavior.
      f4fbdb1b
  4. 23 12月, 2019 2 次提交
  5. 21 12月, 2019 1 次提交
  6. 20 12月, 2019 3 次提交
  7. 19 12月, 2019 2 次提交
  8. 18 12月, 2019 7 次提交
    • E
      Update tests to assert something · 334450bd
      eileencodes 提交于
      These tests weren't calling assert, so if the execute didn't raise but
      also didn't return anything it would be a broken test that never fails.
      We need to always add an assertion so we know what the expected behavior
      is.
      334450bd
    • R
      Remove `:connection_id` in an internal instrument · ed33d869
      Ryuta Kamizono 提交于
      Related #36456.
      
      I grepped the code base by `git grep -n 'connection_id: '` then I found
      extra `connection_id: object_id` which is added at #20818 but unused.
      
      Actually the `connection_id: object_id` is not a connection's object_id
      but a connection_handler's object_id, it is very confusing.
      
      Since the `:connection_id` in an internal instrument is not used, we can
      just remove the incorrect information.
      ed33d869
    • J
      Move `name` key on configuration hash into `DatabaseConfig` · b76659e1
      John Crepezzi 提交于
      `name` is used by Rails to find the configuration by connection
      specification name, but database adapters don't need to use `name` in
      order to establish a connection. This is part of our work to separate
      what the database needs to connect (the configuration hash) and the
      what Rails needs to find connections (everything else).
      Co-authored-by: NJohn Crepezzi <john.crepezzi@gmail.com>
      b76659e1
    • E
      Make `belongs_to_required_by_default` a class attribute: · f2873d59
      Edouard CHIN 提交于
      - I'm hoping to get this change accepted even though this flag was
        introduced years ago in 6576f735
      
        My use case is the following:
      
        We were never enforcing belongs to association and we have a lot
        of models that implicitely declare an association as optional.
        We are now changing all our models to make associations required
        by default.
        Since we have a lot of models (more than 1000), I'd like to
        progressively enable them to use the `belongs_to_required_by_default`
        flag.
      
        The problem is that this flag is a mattr_accessor and doesn't to be
        set per model. We basically need to modify all our models (which
        could take years) before being able to modify the global flag.
      
        I'd like to change this flag to a class_attribute to solve the
        issue.
      f2873d59
    • J
      Rename a test method to not conflict with a deprecated method · 3f743b01
      John Crepezzi 提交于
      This method in test has a confusing name that also is the same as a
      method with a different behavior on `ActiveRecord::Base` so we're just
      renaming it to avoid confusion between the two!
      Co-authored-by: Neileencodes <eileencodes@gmail.com>
      3f743b01
    • J
      Deprecate `connection_config` · 5a374351
      John Crepezzi 提交于
      The `connection_config` method returns a `Hash`, but since we're moving
      toward a place where we're using `DatabaseConfiguration::DatabaseConfig`
      objects everywhere, we're introducing a new method here to replace it
      called `connection_db_config`.
      Co-authored-by: Neileencodes <eileencodes@gmail.com>
      5a374351
    • E
      Don't run concurrent transaction test on sqlite3: · 30e4c199
      Edouard CHIN 提交于
      - #37798 had to be reverted because a new flakyness appeared ([see
        CI build](https://buildkite.com/rails/rails/builds/65467#efaa1dd5-aaf4-43a1-a204-d1c42abf614d))
      
        This failure isn't related to the change itself.
        If you apply this diff, the test will some time to time fail.
        Even without my changes.
      
        ```diff
        diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb
      index b5c1cac3d9..09fe0ddd7f 100644
      --- a/activerecord/test/cases/transactions_test.rb
      +++ b/activerecord/test/cases/transactions_test.rb
      @@ -1127,7 +1127,6 @@ def test_no_automatic_savepoint_for_inner_transaction
         end
       end if Topic.connection.supports_savepoints?
      
      -if ActiveRecord::Base.connection.supports_transaction_isolation?
         class ConcurrentTransactionTest < TransactionTest
           # This will cause transactions to overlap and fail unless they are performed on
           # separate database connections.
      @@ -1198,4 +1197,3 @@ def test_transaction_isolation__read_committed
             assert_equal original_salary, Developer.find(1).salary
           end
         end
      -end
        ```
      
        SQlite3 isn't safe to run in a multi threaded environment unless
        sqlite is compiled with a special flag which isn't our case.
        Ref https://www.sqlite.org/threadsafe.html
      30e4c199
  9. 16 12月, 2019 1 次提交
  10. 11 12月, 2019 1 次提交
  11. 10 12月, 2019 1 次提交
    • J
      Database URL supports query value with equal sign · 43a6420e
      Joshua Flanagan 提交于
      A querystring value should be allowed to include an equal sign `=`.
      
      This is necessary to support passing `options` for a PostgresSQL connection.
      
      ```
      
      development:
        url: postgresql://localhost/railsdevapp_development?options=-cmysetting.debug=on
      ```
      
      Before this PR, attempting to start the rails process with that configuration would result in an error:
      
      ```
      > bundle exec rails console
      Traceback (most recent call last):
      	49: from bin/rails:4:in `<main>'
      	48: from bin/rails:4:in `require'
      ...
      	 1: from /rails/activerecord/lib/active_record/database_configurations/connection_url_resolver.rb:58:in `query_hash'
      /rails/activerecord/lib/active_record/database_configurations/connection_url_resolver.rb:58:in `[]': invalid number of elements (3 for 1..2) (ArgumentError)
      ```
      
      After this PR, rails can properly parse the configuration:
      
      ```
      > bundle exec rails console
      Loading development environment (Rails 6.1.0.alpha)
      2.6.5 :001 > ActiveRecord::Base.connection.select_all("show mysetting.debug").to_a
         (0.4ms)  show mysetting.debug
       => [{"mysetting.debug"=>"on"}]
      ```
      43a6420e
  12. 09 12月, 2019 2 次提交
    • E
      Don't assume all database in SQLite are files: · 82e767f3
      Edouard CHIN 提交于
      - SQlite allow database to be specified as URL (given that URI filename
        interpretation was turned on on the connection.)
      
        This commit is necessary for the read_uncommitted transaction
        feature because SQLite doesn't allow to enable the shared-cache mode
        if the database name is `:memory:`. It has to be a URI (`file::memory`)
      
        Ref https://www.sqlite.org/sharedcache.html#shared_cache_and_in_memory_databases
      82e767f3
    • E
      Added posibility to open a `read_uncommitted` transaction on SQLite: · cf5b6199
      Edouard CHIN 提交于
      - ### Use case
      
        I'd like to be able to see changes made by a connection writer
        within a connection reader before the writer transaction commits
        (aka `read_uncommitted` transaction isolation level).
      
        ```ruby
        conn1.transaction do
          Dog.create(name: 'Fido')
          conn2.transaction do
            Dog.find(name: 'Fido') # -> Can't see the dog untill conn1 commits the transaction
          end
        end
        ```
      
        Other adapters in Rails (mysql, postgres) already supports multiple
        types of isolated db transaction.
        SQLite doesn't support the 4 main ones but it supports
        `read_uncommitted` and `serializable` (the default one when opening
        a transaction)
      
        ### Solution
      
        This PR allow developers to open a `read_uncommitted` transaction by
        setting the PRAGMA `read_uncommitted` to true for the duration
        of the transaction. That PRAGMA can only be enabled if the SQLite
        connection was established with the [shared-cache mode](https://www.sqlite.org/sharedcache.html)
      
        This feature can also benefit the framework and we could potentially
        get rid of the `setup_shared_connection_pool` inside tests which
        was a solution in the context of a multi-db app so that the reader
        can see stuff from the open transaction writer but has some [caveats](https://github.com/rails/rails/issues/37765#event-2828609021).
      
        ### Edge case
      
        Shared-cache mode can be enabled for in memory database as well,
        however for backward compatibility reasons, SQLite only allows
        to set the shared-cache mode if the database name is a URI.
        It won't allow it if the database name is `:memory`; it has to be
        changed to `file::memory` instead.
      cf5b6199
  13. 04 12月, 2019 3 次提交
    • P
      Retain selections with `includes` and `joins` · 2d6088ce
      Patrick Rebsch 提交于
      Applying `includes` and `joins` to a relation that selected additional
      database fields would cause those additional fields not to be included
      in the results even though they were queried from the database:
      
          posts = Post.select('1 as other').includes(:tbl).joins(:tbl)
      
          posts.to_sql.include?('1 as other')       #=> true
          posts.first.attributes.include?('other')  #=> false
      
      This commit includes these additionally selected fields in the
      instantiated results.
      2d6088ce
    • E
      Deprecate `database` kwarg from `connected_to` without replacement · 254ba464
      eileencodes 提交于
      The `database` kwarg in `connected_to` has resulted in a lot of bug
      reports that are trying to use it for sharding when that's not the
      intent of the key. After considering where the database kwarg is used in
      tests and thinking about usecases for it, we've determined it should be
      removed.
      
      There are plans to add sharding support and in the mean time the
      database kwarg isn't the right solution for that. Applications that need
      to create new connections can use establish_connection or connects_to.
      Since the database key causes new connections to be established on every
      call, that causes bugs if connected_to with a database kwarg is used
      during a request or for any connection that's not a one-off.
      Co-authored-by: NJohn Crepezzi <john.crepezzi@gmail.com>
      254ba464
    • E
      Fix bug in configs_for · 2791b7c2
      eileencodes 提交于
      If a spec name was provided without an env name, config_for would return
      the first config that matched the spec, regardless of environment name.
      
      Now configs_for will return the database config that matches the
      default env and requested spec name.
      
      Additionally this commit has moved the default env call into a method
      because I'm tired of typing so many lines every single time.
      
      We considered either returning all configs that match that spec name or
      raising an error if only spec was passed, but this change has the least
      impact on current behavior and matches Active Record's assumptions: that
      if you ask for configs it will always consider the current environment.
      Co-authored-by: NJohn Crepezzi <john.crepezzi@gmail.com>
      2791b7c2
  14. 03 12月, 2019 1 次提交
  15. 26 11月, 2019 1 次提交
    • T
      Fix making an extra db file after testing for sqlite3 · a8009c6d
      Takayuki Nakata 提交于
      Testing for sqlite3 makes an extra db file `primary.sqlite3` as below.
      ```
      $ bundle exec rake test:sqlite3
      (snip)
      $ git status
      On branch test
      Untracked files:
        (use "git add <file>..." to include in what will be committed)
      
              db/
      ```
      a8009c6d
  16. 25 11月, 2019 1 次提交
  17. 24 11月, 2019 1 次提交
  18. 22 11月, 2019 3 次提交
    • E
      Fix connection pools not shared between writer -> replica during tests: · 13015b38
      Edouard CHIN 提交于
      - ### Problem
      
        Connection pools are not properly shared. A replica can't see the
        data in the open transaction on the writing connection.
        ```ruby
        Dog.create!(name: 'bilou')
      
        AnimalsBase.connected_to(role: :reading) do
          Dog.find_by(name: 'bilou') # No result
        end
        ```
      
        The reason of this bug is because when test starts, we iterate
        over `AR::Base.connection_handlers` which only contains the `writing`
        handler since models haven't been loaded (app isn't eagerloaded
        test env).
      
        ### Solution
      
        Share connection pools as soon as a connection is established
        by creating a subcriber.
      13015b38
    • E
      Modify ActiveRecord::TestFixtures to not rely on AS::TestCase: · d4367eb7
      Edouard CHIN 提交于
      - ### Problem
      
        If one wants to use ActiveRecord::TestFixtures it is mandatory for
        the test suite to inherit from `ActiveSupport::TestCase`.
        TestFixtures makes use of specific method from AS::TestCase
        (`file_fixture_path` and `method_name`).
      
        ### Solution
      
        This PR fixes that by not making use of method_name and file_fixture_path.
      d4367eb7
    • G
      Fix unscoped grouped where · 12afdba8
      Gannon McGibbon 提交于
      12afdba8
  19. 21 11月, 2019 1 次提交
    • T
      Remove an unused connection handler in a test · 3c3eb40f
      Takayuki Nakata 提交于
      There is an unused connection handler in a test and an extra
      connection handler is made, so testing for sqlite3 makes an empty db
      file as below.
      
      ```
      $ bundle exec rake test:sqlite3
      (snip)
      $ git status
      On branch test
      Untracked files:
        (use "git add <file>..." to include in what will be committed)
      
              db/
      ```
      3c3eb40f
  20. 20 11月, 2019 1 次提交
    • B
      Check that entire collection has been loaded before short circuiting · 0a5b41c4
      Brad Price 提交于
      Currently, when checking that the collection has been loaded, only the first
      record is checked. In specific scenarios, if a record is fetched via an `after_initialize`
      hook, there is a chance that the first record has been loaded, but other records in the
      collection have not.
      
      In order to successfully short circuit the fetching of data, we need to verify that
      all of the records in the collection have been loaded.
      
      * Create test for edge case
      * Move `reset_callbacks` method to `cases/helper`, since it has been defined in multiple
        locations.
      
      Closes #37730
      0a5b41c4
  21. 16 11月, 2019 3 次提交