1. 01 5月, 2020 4 次提交
    • R
      Avoid quite useless loop in `find_by_sql` · 71e45582
      Ryuta Kamizono 提交于
      `column_types` is empty except PostgreSQL adapter, and
      `attribute_types.each_key { |k| column_types.delete k }` is also empty
      even if PostgreSQL adapter almost all case, so that code is quite
      useless. This improves performance for `find_by_sql` to avoid that
      useless loop as much as possible.
      
      ```ruby
      ActiveRecord::Schema.define do
        create_table :active_storage_blobs do |t|
          t.string   :key,          null: false
          t.string   :filename,     null: false
          t.string   :content_type
          t.text     :metadata
          t.string   :service_name, null: false
          t.bigint   :byte_size,    null: false
          t.string   :checksum,     null: false
          t.datetime :created_at,   null: false
      
          t.index [ :key ], unique: true
        end
      end
      
      class ActiveStorageBlob < ActiveRecord::Base
      end
      
      Benchmark.ips do |x|
        x.report("find_by") { ActiveStorageBlob.find_by(id: 1) }
      end
      ```
      
      Before:
      
      ```
      Warming up --------------------------------------
                   find_by     1.256k i/100ms
      Calculating -------------------------------------
                   find_by     12.595k (± 3.4%) i/s -     64.056k in   5.091599s
      ```
      
      After:
      
      ```
      Warming up --------------------------------------
                   find_by     1.341k i/100ms
      Calculating -------------------------------------
                   find_by     13.170k (± 3.5%) i/s -     67.050k in   5.097439s
      ```
      
      To avoid column types loop for PostgreSQL adapter, this skips returning
      additional column types if a column has already been type casted by pg
      decoders. Fortunately this fixes #36186 partly for common types.
      71e45582
    • R
      Replace `result_as_array` by type mapping · 348e142b
      Ryuta Kamizono 提交于
      348e142b
    • R
    • R
      Should not rely on the global `Arel::Table.engine` in the framework · bc99e401
      Ryuta Kamizono 提交于
      Relying on the `Arel::Table.engine` is convenient if an app have only a
      single kind of database, but if not so, the global state is not always
      the same with the current connection.
      bc99e401
  2. 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
  3. 28 4月, 2020 1 次提交
  4. 27 4月, 2020 2 次提交
    • R
      Fix typo · 60ff1194
      Ryuta Kamizono 提交于
      60ff1194
    • 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
  5. 25 4月, 2020 5 次提交
  6. 24 4月, 2020 1 次提交
  7. 23 4月, 2020 2 次提交
  8. 21 4月, 2020 1 次提交
  9. 20 4月, 2020 2 次提交
    • I
      Preserve column comment on renaming column · c22cad96
      Islam Taha 提交于
      Update activerecord changelog
      
      Specify DB name in changelog and fix typo
      c22cad96
    • E
      Use __id__ to dedup records for transactional callbacks · 8d3ca970
      Eugene Kenny 提交于
      While not a particularly good idea, it's possible to use `object_id` as
      an attribute name, typically by defining a polymorphic association named
      `object`. Since 718a32ca, transactional
      callbacks deduplicate records by their `object_id`, but this causes
      incorrect behaviour when the record has an attribute with that name.
      
      Using `__id__` instead makes a naming collision much less likely.
      8d3ca970
  10. 17 4月, 2020 2 次提交
  11. 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
  12. 15 4月, 2020 2 次提交
    • R
      Fix unscoping association scope on joins not to raise an error · e93f416a
      Ryuta Kamizono 提交于
      #29589 changed merging scope order to allow to unscope default scopes on
      association scope (#29611), but that caused a regression #38811 that
      accidentally allow join constraint which is required.
      
      ```
      % bin/test test/cases/associations/has_many_associations_test.rb -n test_unscopes_the_default_scope_of_associated_model_when_used_with_include
      Run options: -n test_unscopes_the_default_scope_of_associated_model_when_used_with_include --seed 32978
      
      # Running:
      
      E
      
      Error:
      HasManyAssociationsTest#test_unscopes_the_default_scope_of_associated_model_when_used_with_include:
      NoMethodError: undefined method `children' for nil:NilClass
          ~/rails/activerecord/lib/active_record/associations/join_dependency/join_association.rb:39:in `block in join_constraints'
          ~/rails/activerecord/lib/active_record/associations/join_dependency/join_association.rb:30:in `reverse_each'
          ~/rails/activerecord/lib/active_record/associations/join_dependency/join_association.rb:30:in `with_index'
          ~/rails/activerecord/lib/active_record/associations/join_dependency/join_association.rb:30:in `join_constraints'
          ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:171:in `make_constraints'
          ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:196:in `block in walk'
          ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:196:in `each'
          ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:196:in `flat_map'
          ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:196:in `walk'
          ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:90:in `block in join_constraints'
          ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:87:in `each'
          ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:87:in `flat_map'
          ~/rails/activerecord/lib/active_record/associations/join_dependency.rb:87:in `join_constraints'
          ~/rails/activerecord/lib/active_record/relation/query_methods.rb:1226:in `build_join_query'
          ~/rails/activerecord/lib/active_record/relation/query_methods.rb:1211:in `build_joins'
          ~/rails/activerecord/lib/active_record/relation/query_methods.rb:1091:in `build_arel'
          ~/rails/activerecord/lib/active_record/relation/query_methods.rb:1063:in `arel'
          ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:419:in `block in limited_ids_for'
          ~/rails/activerecord/lib/active_record/relation.rb:867:in `skip_query_cache_if_necessary'
          ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:419:in `limited_ids_for'
          ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:398:in `apply_join_dependency'
          ~/rails/activerecord/lib/active_record/relation.rb:839:in `block in exec_queries'
          ~/rails/activerecord/lib/active_record/relation.rb:867:in `skip_query_cache_if_necessary'
          ~/rails/activerecord/lib/active_record/relation.rb:834:in `exec_queries'
          ~/rails/activerecord/lib/active_record/relation.rb:639:in `load'
          ~/rails/activerecord/lib/active_record/relation.rb:250:in `records'
          ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:508:in `find_take'
          ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:98:in `take'
          ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:458:in `find_one'
          ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:442:in `find_with_ids'
          ~/rails/activerecord/lib/active_record/relation/finder_methods.rb:69:in `find'
          ~/rails/activerecord/test/cases/associations/has_many_associations_test.rb:2689:in `block in <class:HasManyAssociationsTest>'
      
      bin/test test/cases/associations/has_many_associations_test.rb:2683
      ```
      
      Required join constraint should not be allowed to unscoping.
      
      Fixes #38811.
      e93f416a
    • R
      Remove unused Arel visitors in the code base · a2040ee3
      Ryuta Kamizono 提交于
      This removes ibm_db, informix, mssql, oracle, and oracle12 Arel visitors
      which are not used in the code base.
      
      Actually oracle and oracle12 visitors are used at oracle-enhanced
      adapter, but now I think that those visitors should be in the adapter's
      repo like sqlserver adapter and the dedicated Arel visitor
      (https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/master/lib/arel/visitors/sqlserver.rb),
      otherwise it is hard to find a bug and review PRs for the oracle
      visitors (e.g. #35838, #37646), since we don't have knowledge and
      environment enough for Oracle.
      a2040ee3
  13. 14 4月, 2020 2 次提交
    • A
      Update SchemaCacheTest#schema_dump_path · ffc6b648
      Ali Ibrahim 提交于
        * Use ASSETS_ROOT (defined in activerecord/test/config.rb) to guarantee a
          valid path to schema_dump_5_1.yml.
      ffc6b648
    • 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
  14. 13 4月, 2020 1 次提交
  15. 12 4月, 2020 4 次提交
  16. 10 4月, 2020 2 次提交
    • R
      Allow extra scoping in callbacks when create on association relation · f64b5fb9
      Ryuta Kamizono 提交于
      #37523 has a regression that ignore extra scoping in callbacks when
      create on association relation.
      
      It should respect `klass.current_scope` even when create on association
      relation to allow extra scoping in callbacks.
      
      Fixes #38741.
      f64b5fb9
    • E
      Remove ActiveRecord::DefineCallbacks module · 6517263f
      Eugene Kenny 提交于
      This module was added in 16ae3db5 to
      allow `ActiveRecord::AttributeMethods::Dirty` to define callbacks and
      still have its `_update_record` method wrapped by the version defined in
      `ActiveRecord::Callbacks`, so that updates in `before_update` callbacks
      are taken into account for partial writes.
      
      The callbacks that created this circular dependency were removed in
      34f075fe, so we can move the callback
      definitions back to the `Callbacks` module.
      6517263f
  17. 08 4月, 2020 1 次提交
  18. 07 4月, 2020 1 次提交
    • A
      Fix EagerLoadPolyAssocsTest setup (#38883) · 834f5414
      alimi 提交于
      * Fix EagerLoadPolyAssocsTest setup
      
        * EagerLoadPolyAssocsTest includes a Remembered module in multiple test
          ActiveRecord classes. The module is supposed to keep track of records
          created for each of the included classes individually, but it collects all
          records for every class. This happens because @@remembered is defined on the
          Remembered module and shared between the ActiveRecord classes.  This only
          becomes an issue for databases (like CockroachDB) that use random primary
          keys instead of sequential ones by default.
        * To fix the bug, we can make the remembered collection name unique per
          ActiveRecord class.
      
      * Update EagerLoadPolyAssocsTest test setup
      
        * Instead of defining remembered as a class variable, we can define it as an
          instance variable that will be unique to every class that includes the
          Remembered module.
      834f5414
  19. 04 4月, 2020 1 次提交
  20. 29 3月, 2020 2 次提交
  21. 27 3月, 2020 1 次提交
  22. 25 3月, 2020 1 次提交