1. 21 9月, 2020 1 次提交
  2. 17 9月, 2020 1 次提交
    • E
      Make `role` required when using `shard` in `connected_to` · d4df616c
      eileencodes 提交于
      While working on some more indepth changes to Rails internal connection
      management we noticed that it's confusing in some cases that the `role`
      is implcit when using a `shard`. For example, if you passed a `shard`
      and not a `role` in an un-nested block the default `role` would be
      `writing`.
      
      ```
      ActiveRecord::Base.connected_to(shard: :one) do
        # connected to writing
      end
      ```
      
      However in cases where nesting is used it could be confusing to
      application authors that the role is inherited:
      
      ```
      ActiveRecord::Base.connected_to(role: :reading) do
        ActiveRecord::Base.connected_to(shard: :one) do
          # will read from shard one replica, not write to primary
        end
      end
      ```
      
      Since this could be potentially confusing, and extremely hard to track
      in complex applications, the best approach is to require `role` when
      using `shard` which is what this PR does.
      
      Note: the code for this method is...getting unweildy. Once the
      `database` argument is fully deprecated we can remove most of the guards
      and make `role` required by removing `nil` from the keyword argument.
      Until then we need to support required arguments in this round about way.
      d4df616c
  3. 16 9月, 2020 2 次提交
  4. 15 9月, 2020 1 次提交
  5. 14 9月, 2020 2 次提交
  6. 10 9月, 2020 1 次提交
  7. 09 9月, 2020 3 次提交
  8. 08 9月, 2020 2 次提交
  9. 07 9月, 2020 1 次提交
  10. 04 9月, 2020 2 次提交
    • G
      Raise error when `from` is used with `delete_all`. · 9e477df7
      Guo Xiang Tan 提交于
      This prevents the `from` query method from being ignored silently in the
      resulting SQL query that is generated.
      9e477df7
    • J
      Support reversing of Arel expressions in reverse_order · e32fd534
      Josh Goodall 提交于
      The current behaviour of reverse_order, when presented with an
      existing order that is an Arel expression, is to ignore it.
      This isn't quite in keeping with how it handles Arel attributes
      (which it reverse) or arbitrary strings (which throw an exception).
      
      The easy path would be to throw an exception, but we don't have to.
      Any SQL expression that is a valid default sort expression can be
      reversed with DESC.
      
      It follows that if there's an existing Arel::Nodes::NodeExpression
      that is not already wrapped with an Arel::Nodes::Ordering, then
      it can be reversed with DESC.
      e32fd534
  11. 02 9月, 2020 2 次提交
  12. 01 9月, 2020 3 次提交
  13. 27 8月, 2020 1 次提交
  14. 26 8月, 2020 4 次提交
    • J
    • G
    • R
      Allow `where` references association names as joined table alias names · 999bf3d0
      Ryuta Kamizono 提交于
      If a table is joined multiple times, those tables are aliased other than
      the first one.
      
      It happens easily on self referential associations, and in that case
      currently there is no way to work custom attribute (type casting) and
      attribute alias resolution for aliased tables in `where` conditions.
      
      To address the issue, it will allow `where` references association names
      as table aliases. If association names are referenced in `where`, those
      names are used for joined table alias names.
      
      ```ruby
      class Comment < ActiveRecord::Base
        enum label: [:default, :child]
        has_many :children, class_name: "Comment", foreign_key: :parent_id
      end
      
      # ... FROM comments LEFT OUTER JOIN comments children ON ... WHERE children.label = 1
      Comment.includes(:children).where("children.label": "child")
      ```
      
      Fixes #39727.
      999bf3d0
    • P
      Rename horrible and dirty to puzzled and confused in test models · 504a9d50
      Petrik 提交于
      In 7f938cac the test model `Man` was renamed to `Human`. Maybe this is a
      good time to also change `horrible_human` and `dirty_human` to
      `happy_human` and `confused_human`.
      
      While this change is mostly cosmetic change, the phrase "dirty man" has
      a negative meaning.
      
      The adjectives "confused" and "puzzled" were chosen because they are
      used for defining associations with errors.
      504a9d50
  15. 25 8月, 2020 2 次提交
    • G
      Disconnect connections used for acquiring advisory lock in migration. · ae330afe
      Guo Xiang Tan 提交于
      Not disconnecting the connections in the pool results in a session left idling in the DB.
      
      An idle session will prevent the database being dropped and affect
      commands like `bin/rails db:test:prepare`. While this is an edge case,
      it is still good practice to clean up the connections in the pool.
      ae330afe
    • P
      Add DidYouMean for InverseOfAssociationNotFoundError · c639ef17
      Petrik 提交于
      If an inverse association isn't found we can suggest similar associations:
      
      ```
      class Human < ActiveRecord::Base
        has_one :dirty_face, class_name: "Face", inverse_of: :dirty_human
      end
      
      class Face < ActiveRecord::Base
        belongs_to :human, inverse_of: :face
        belongs_to :horrible_human, class_name: "Human", inverse_of: :horrible_face
      end
      Human.first.dirty_face
      
      Could not find the inverse association for dirty_face (:dirty_human in Face)
      Did you mean?  human
                     horrible_human
                    ....
      ```
      Co-authored-by: NDaniel Colson <danieljamescolson@gmail.com>
      c639ef17
  16. 24 8月, 2020 1 次提交
  17. 23 8月, 2020 1 次提交
    • R
      Support storing demodulized class name for polymorphic type · 62cfbdf3
      Ryuta Kamizono 提交于
      This is an alternative of #29722.
      
      Before Rails 6.1, storing demodulized class name is supported only for
      STI type by `store_full_sti_class` class attribute.
      
      Now `store_full_class_name` class attribute can handle both STI and
      polymorphic types.
      
      Closes #29722.
      
      See also #29601, #32048, #32148.
      62cfbdf3
  18. 17 8月, 2020 1 次提交
    • R
      Fix eager loading that non-select columns will be loaded · 46393182
      Ryuta Kamizono 提交于
      Related to #35210.
      
      We sometimes use `select` to limit unused columns for performance.
      
      For example, `GET /posts/1` (post detail) usually use (almost) all
      columns, but `GET /posts` (post list) does not always use all columns
      (e.g. use `id` and `title` for the list view, but `body` is not used).
      
      If an association is eager loaded, the limited `select` doesn't works as
      expected, eager loading will load all columns on the model, plus also
      load the `select` columns additionally. It works differently with
      natural load and preload. It means that changing natural load or preload
      to eager load (or vice versa) is unsafe.
      
      This fixes eager loading that always load all columns (plus extra
      `select` columns), to respect the `select` columns like as others.
      
      ```ruby
      post = Post.select("UPPER(title) AS title").first
      post.title # => "WELCOME TO THE WEBLOG"
      post.body  # => ActiveModel::MissingAttributeError
      
      # Rails 6.0 (ignore the `select` values)
      post = Post.select("UPPER(title) AS title").eager_load(:comments).first
      post.title # => "Welcome to the weblog"
      post.body  # => "Such a lovely day"
      
      # Rails 6.1 (respect the `select` values)
      post = Post.select("UPPER(title) AS title").eager_load(:comments).first
      post.title # => "WELCOME TO THE WEBLOG"
      post.body  # => ActiveModel::MissingAttributeError
      ```
      46393182
  19. 16 8月, 2020 1 次提交
    • R
      Fix preloader to associate preloaded records by default · f80179db
      Ryuta Kamizono 提交于
      Someone had relied on the behavior that preloading with a given scope,
      but the behavior has lost in #35496 to fix the minor bug that unloading
      through association.
      
      Basically we don't guarantee the internal behavior, but the bugfix can
      be achieved without any breaking change, so I've restored the lost
      functionality.
      
      Fixes #36638.
      Fixes #37720.
      f80179db
  20. 14 8月, 2020 4 次提交
    • E
      Fix incorrect removal of current_shard in establish_connection · ba2f38e4
      eileencodes 提交于
      If we enter a `connected_to` block and call `establish_connection` like
      the test added here we need to ensure that `shard: current_shard` is passed
      to the handler, otherwise the connection will be established on
      `default` not on `shard_one`.
      Co-authored-by: NJohn Crepezzi <john.crepezzi@gmail.com>
      ba2f38e4
    • D
      Rename single letter variables · d79519f2
      Daniel Colson 提交于
      After renaming `Man` to `Human` the variable letter `m` in these tests
      ends up being pretty confusing. Rather than rename it to `h`, this
      commit replaces it with the full word `human`.
      
      Since I was already renaming things, I also went ahead and replaced `f`
      with `face`, `i` with `interest`, and `a` with `author`.
      d79519f2
    • D
      Replace test `Man` with `Human` · 7f938cac
      Daniel Colson 提交于
      The commit replaces the `Man` model used in tests with a `Human` model. It
      also replaces the existing `Human` model with a `SuperHuman` model
      inheriting from `Human`.
      
      While this may seem like a cosmetic change, I see it as more of an
      inclusivity change. I think it makes sense for a number of reasons:
      
      * Prior to this commit the `Human` model inherited from `Man`. At best
        this makes no sense (it should be the other way around). At worst it
        is offensive and harmful to the community.
      * It doesn't seem inclusive to me to have exclusively male-gendered
        examples in the codebase.
      * There is no particular reason for these examples to be gendered.
      * `man` is hard to grep for, since it also matches `many, manager,
        manual, etc`
      
      For the most part this is a simple search and replace. The one exception
      to that is that I had to add the table name to the model so we could use
      "humans" instead of "humen".
      7f938cac
    • R
      Fix incorrect result when eager loading with duplicated through association with join scope Part 2 · 62de28fb
      Ryuta Kamizono 提交于
      Follow up of #40000.
      
      In #40000, `eager_load(:general_categorizations, :general_posts)` works,
      but `eager_load(:general_posts, :general_categorizations)` doesn't work
      yet.
      
      This implements the deduplication for the case of reversed eager loading
      order.
      62de28fb
  21. 11 8月, 2020 1 次提交
    • E
      Fix missed establish_connection · 919eb6dc
      eileencodes 提交于
      In `connected_to` one of the deprecated arguments wasn't well tested so
      the incorrect methods signature wasn't caught by the tests.
      
      This change updates the caller when `connected_to` uses the database
      key.
      
      I've also cleaned up a few arguments that weren't necessary. Since
      the handler methods set defaults for the `shard` key, we don't need to
      pass that in `establish_connection` when not using the sharding API.
      919eb6dc
  22. 10 8月, 2020 1 次提交
    • E
      Add helper method for resetting connection handlers in tests · 61ab1543
      eileencodes 提交于
      This change makes a helper method for resetting connection handlers in
      the Active Record tests. The change here is relatively small and may
      seem unnecessary. The reason we're pushing this change is for upcoming
      refactoring to connection management. This change will mean that we can
      update one location instead of 9+ files to reset connections. It will
      reduce the diff of our refactoring and make reusing this code easier in
      the future.
      
      The method name chosen is purposefully `clean_up_connection_handler`
      over `clean_up_connection_handlers` because in the future there will
      only be one handler.
      Co-authored-by: NJohn Crepezzi <john.crepezzi@gmail.com>
      61ab1543
  23. 08 8月, 2020 2 次提交
    • E
      Update connection methods to use kwargs · d3061cda
      eileencodes 提交于
      This change ensures that the connection methods are using kwargs instead
      of positional arguments. This change may look unnecessary but we're
      working on refactoring connection management to make it more robust and
      flexible so the method signatures of the methods changed here will
      continue to evolve and change.
      
      This commit does not change any released public APIs. The `shard` and
      `owner_name` arguments were added in 6.1 which is not released yet.
      Using kwargs will allow these methods to be more flexible and not get
      super ugly as we change their underlying behavior. The kwargs let us
      support multiple non-positional arguments with default.
      Co-authored-by: NJohn Crepezzi <john.crepezzi@gmail.com>
      d3061cda
    • E
      Remove unnecessary with_temporary_connection_pool calls · 9289232b
      eileencodes 提交于
      While debugging a different problem I'm working on I realized that this
      method `with_temporary_connection_pool` isn't necessary in most of the
      cases we're using it for.
      
      Anywhere we establish new connections inside the block won't throw away
      those new connections. I also removed this from places that can use the
      existing connection and don't need a new temporary pool. I'm not sure if
      this file was using it in many places because of copy / paste or real
      issues that are no longer present.
      9289232b