1. 13 5月, 2020 1 次提交
    • R
      Fix `minimum` and `maximum` on time zone aware attributes · 11751b2e
      Ryuta Kamizono 提交于
      This is the opposite direction of #39039.
      
      #39111 fixes `minimum` and `maximum` on date columns with type casting
      by column type on the database. But column type has no information for
      time zone aware attributes, it means that attribute type should always
      be precedence over column type. I've realized that fact in the related
      issue report #39248.
      
      I've reverted the expectation of #39039, to make time zone aware
      attributes works.
      11751b2e
  2. 10 5月, 2020 2 次提交
  3. 07 5月, 2020 1 次提交
    • R
      Allow `unscope` to be aware of table name qualified values · 5136277f
      Ryuta Kamizono 提交于
      It is possible to unscope only the column in the specified table.
      
      ```ruby
      posts = Post.joins(:comments).group(:"posts.hidden")
      posts = posts.where("posts.hidden": false, "comments.hidden": false)
      
      posts.count
      # => { false => 10 }
      
      # unscope both hidden columns
      posts.unscope(where: :hidden).count
      # => { false => 11, true => 1 }
      
      # unscope only comments.hidden column
      posts.unscope(where: :"comments.hidden").count
      # => { false => 11 }
      ```
      Co-authored-by: NSlava Korolev <korolvs@gmail.com>
      5136277f
  4. 05 5月, 2020 2 次提交
    • R
      Fix `rewhere` to truly overwrite collided where clause by new where clause · 6187b713
      Ryuta Kamizono 提交于
      ```ruby
      steve = Person.find_by(name: "Steve")
      david = Author.find_by(name: "David")
      
      relation = Essay.where(writer: steve)
      
      # Before
      relation.rewhere(writer: david).to_a # => []
      
      # After
      relation.rewhere(writer: david).to_a # => [david]
      ```
      
      For now `rewhere` only works for truly column names, doesn't work for
      alias attributes, nested conditions, associations.
      
      To fix that, need to build new where clause first, and then get
      attribute names from new where clause.
      6187b713
    • A
      Inspect time attributes with subsec · a3f4202c
      akinomaeni 提交于
      before
      ```
      p Knot.create
      => #<Knot id: 1, created_at: "2016-05-05 01:29:47">
      ```
      
      after
      ```
      p Knot.create
      => #<Knot id: 1, created_at: "2016-05-05 01:29:47.116928000">
      ```
      a3f4202c
  5. 02 5月, 2020 1 次提交
  6. 30 4月, 2020 1 次提交
    • R
      Deprecate `allowed_index_name_length` in `DatabaseLimits` · ab2d859e
      Ryuta Kamizono 提交于
      `allowed_index_name_length` was used for internal temporary operations
      in SQLite3, since index name in SQLite3 must be globally unique and
      SQLite3 doesn't have ALTER TABLE feature (so it is emulated by creating
      temporary table with prefix).
      
      `allowed_index_name_length` was to reserve the margin for the prefix,
      but actually SQLite3 doesn't have a limitation for identifier name
      length, so the margin has removed at 36901e6e.
      
      Now `allowed_index_name_length` is no longer relied on by any adapter,
      so I'd like to remove the internal specific method which is no longer
      used.
      ab2d859e
  7. 27 4月, 2020 1 次提交
    • R
      Deprecate `in_clause_length` in `DatabaseLimits` · c0ca7625
      Ryuta Kamizono 提交于
      `in_clause_length` was added at c5a284f8 to address to Oracle IN clause
      length limitation.
      
      Now `in_clause_length` is entirely integrated in Arel visitor since
      #35838 and #36074.
      
      Since Oracle visitors are the only code that rely on `in_clause_length`.
      so I'd like to remove that from Rails code base, like has removed Oracle
      visitors (#38946).
      c0ca7625
  8. 25 4月, 2020 2 次提交
  9. 20 4月, 2020 1 次提交
  10. 16 4月, 2020 1 次提交
    • E
      Add `if_exists` option to `remove_index` · 36ea1084
      eileencodes 提交于
      This PR allows for passing `if_exists` options to the `remove_index`
      method so that we can ignore already removed indexes. This work follows
      column `if/if_not_exists` from #38352 and `:if_not_exists` on `add_index`
      from #38555.
      
      We've found this useful at GitHub, there are migrations where we don't
      want to raise if an index was already removed. This will allow us to
      remove a monkey patch on `remove_index`.
      
      I considered raising after the `index_name_for_remove` method is called
      but that method will raise if the index doesn't exist before we get to
      execute. I have a commit that refactors this but after much
      consideration this change is cleaner and more straightforward than other
      ways of implementing this.
      
      This change also adds a little extra validation to the `add_index` test.
      Fix `nodoc` on edited methods.
      36ea1084
  11. 15 4月, 2020 2 次提交
  12. 14 4月, 2020 1 次提交
    • J
      Prevent has_one from touching parent record unless persisted · ba3ef762
      Josh 提交于
      Previously, if `build_association` was called multiple times for a `has_one` association but never committed to the database, the first newly-associated record would trigger `touch` during the attempted removal of the record.
      
      For example:
      
          class Post < ActiveRecord::Base
            has_one :comment, inverse_of: :post, dependent: :destroy
          end
      
          class Comment < ActiveRecord::Base
            belongs_to :post, inverse_of: :comment, touch: true
          end
      
          post = Post.create!
          comment_1 = post.build_comment
          comment_2 = post.build_comment
      
      When `comment_2` is initialized, the `has_one` would attempt to destroy `comment_1`, triggering a `touch` on `post` from an association record that hasn't been committed to the database.
      
      This removes the attempt to delete an associated `has_one` unless it’s persisted.
      ba3ef762
  13. 29 3月, 2020 1 次提交
    • P
      Add support for `if_not_exists` to indexes · 7ba08037
      Prathamesh Sonpatki 提交于
      When we try to create a table which already exists which also adds
      indexes, then the `if_not_exists` option passed to `create_table` is
      not extended to indexes. So the migration results into an error if the
      table and indexes already exist.
      This change extends the `if_not_exists` support to `add_index` so that
      if the migration tries to create a table which also has existing
      indexes, error won't be raised.
      Also as a side-effect individual `add_index` calls will also accept
      `if_not_exists` option and respect it henceforth.
      
      [Prathamesh Sonpatki, Justin George]
      7ba08037
  14. 27 3月, 2020 1 次提交
  15. 25 3月, 2020 1 次提交
  16. 21 3月, 2020 1 次提交
  17. 20 3月, 2020 1 次提交
    • E
      Handle db:rollback and db:rollback:[NAME] for multi-db apps · e33075a0
      eileencodes 提交于
      With a multiple database application `db:rollback` becomes problematic.
      We can't rollback just the primary, that doesn't match the behavior in
      the other tasks. We can't rollback a migration for every database, that
      is unexpected.
      
      To solve this I handled `db:rollback` the same way I handled `:up` and
      `:down`. If `db:rollback` is called for a multi-db application then it
      will raise an error recommending you use `db:rollback:[NAME]` instead.
      Calling `db:rollback:primary` or `db:rollback:animals` will rollback
      the migration for the number of steps specified.
      
      Closes: #38513
      Follow-up to: #34078
      e33075a0
  18. 19 3月, 2020 3 次提交
  19. 18 3月, 2020 1 次提交
    • A
      Use non-exist enum string to get unrelated record in My SQL · 93b8c24f
      ak 提交于
      This behaviour is in
      rails/activerecord/lib/active_record/enum.rb #serialize(value) line no 143
      if value is not present in mapping we are sending the value back ,
      which in mysql returns unrelated record.
      
      I have changed to return nil is value is not present in mapping
      
      Implemented code review changes
      
      Improved test case coverage
      
      [ci skip] - cosmetic changes for better readibility of change log
      Signed-off-by: Nak <atulkanswal@gmail.com>
      93b8c24f
  20. 28 2月, 2020 2 次提交
  21. 27 2月, 2020 1 次提交
  22. 25 2月, 2020 4 次提交
    • R
      Fixup CHANGELOGs [ci skip] · 1d3eb7be
      Ryuta Kamizono 提交于
      1d3eb7be
    • E
      Add support for horizontal sharding · 384e7d13
      eileencodes 提交于
      Applications can now connect to multiple shards and switch between
      their shards in an application. Note that the shard swapping is
      still a manual process as this change does not include an API for
      automatic shard swapping.
      
      Usage:
      
      Given the following configuration:
      
      ```yaml
      production:
        primary:
          database: my_database
        primary_shard_one:
          database: my_database_shard_one
      ```
      
      Connect to multiple shards:
      
      ```ruby
      class ApplicationRecord < ActiveRecord::Base
        self.abstract_class = true
      
        connects_to shards: {
          default: { writing: :primary },
          shard_one: { writing: :primary_shard_one }
        }
      ```
      
      Swap between shards in your controller / model code:
      
      ```ruby
        ActiveRecord::Base.connected_to(shard: :shard_one) do
          # Read from shard one
        end
      ```
      
      The horizontal sharding API also supports read replicas. See
      guides for more details.
      
      This PR also moves some no-doc'd methods into the private namespace as
      they were unnecessarily public. We've updated some error messages and
      documentation.
      Co-authored-by: NJohn Crepezzi <john.crepezzi@gmail.com>
      384e7d13
    • E
      Deprecate `spec_name` and use `name` for configurations · 79ce7d9a
      eileencodes 提交于
      I have so. many. regrets. about using `spec_name` for database
      configurations and now I'm finally putting this mistake to an end.
      
      Back when I started multi-db work I assumed that eventually
      `connection_specification_name` (sometimes called `spec_name`) and
      `spec_name` for configurations would one day be the same thing. After
      2 years I no longer believe they will ever be the same thing.
      
      This PR deprecates `spec_name` on database configurations in favor of
      `name`. It's the same behavior, just a better name, or at least a
      less confusing name.
      
      `connection_specification_name` refers to the parent class name (ie
      ActiveRecord::Base, AnimalsBase, etc) that holds the connection for it's
      models. In some places like ConnectionHandler it shortens this to
      `spec_name`, hence the major confusion.
      
      Recently I've been working with some new folks on database stuff and
      connection management and realize how confusing it was to explain that
      `db_config.spec_name` was not `spec_name` and
      `connection_specification_name`. Worse than that one is a symbole while
      the other is a class name. This was made even more complicated by the
      fact that `ActiveRecord::Base` used `primary` as the
      `connection_specification_name` until #38190.
      
      After spending 2 years with connection management I don't believe that
      we can ever use the symbols from the database configs as a way to
      connect the database without the class name being _somewhere_ because
      a db_config does not know who it's owner class is until it's been
      connected and a model has no idea what db_config belongs to it until
      it's connected. The model is the only way to tie a primary/writer config
      to a replica/reader config. This could change in the future but I don't
      see value in adding a class name to the db_configs before connection or
      telling a model what config belongs to it before connection. That would
      probably break a lot of application assumptions. If we do ever end up in
      that world, we can use name, because tbh `spec_name` and
      `connection_specification_name` were always confusing to me.
      79ce7d9a
    • K
  23. 22 2月, 2020 1 次提交
  24. 21 2月, 2020 1 次提交
  25. 20 2月, 2020 1 次提交
  26. 06 2月, 2020 1 次提交
    • S
      This PR adds support to retrieve partitioned indexes when asking for · db6eb846
      Sebastián Palma 提交于
      indexes in a table.
      
      Currently the pg_class catalog is filtered out to retrieve the indexes in a
      table by its relkind value. Which in versions lower than 11 of PostgreSQL
      is always `i` (lower case). But since version 11, PostgreSQL
      supports partitioned indexes referenced with a relkind value of `I`
      (upper case). This makes any feature within the current code base to exclude those
      partitioned indexes.
      
      The solution proposed is to make use of the `IN` clause to filter those
      relkind values of `i` and/or `I` when retrieving a table indexes.
      db6eb846
  27. 31 1月, 2020 1 次提交
  28. 30 1月, 2020 1 次提交
    • E
      Add support for `if_exists/if_not_exists` on `remove_column/add_column` · 95ed7e78
      eileencodes 提交于
      This PR adds support for `if_exists` on `remove_column` and
      `if_not_exists` on `add_column` to support silently ignoring migrations
      if the remove tries to remove a non-existent column or an add tries to
      add an already existing column.
      
      We (GitHub) have custom monkey-patched support for these features and
      would like to upstream this behavior.
      
      This matches the same behavior that is supported for `create_table` and
      `drop_table`. The behavior for sqlite is different from mysql/postgres
      and sqlite for remove column and that is reflected in the tests.
      95ed7e78
  29. 29 1月, 2020 1 次提交
  30. 24 1月, 2020 1 次提交