1. 04 1月, 2020 1 次提交
    • J
      Pass env_name as a string in test databases · 1f938f52
      John Crepezzi 提交于
      In 154abcab we switched from using `Rails.env` to fetch the `env_name` to
      `ActiveRecord::ConnectionHandling::DEFAULT_ENV.call.to_sym` which
      changed the type from a `String` to a `Symbol`.
      
      This commit brings things back to the original state, so we can find the
      configurations correctly!
      
      It also modifies the configuration in the configurations array, so that
      future connections can find the database with the updated keyword value.
      1f938f52
  2. 03 1月, 2020 1 次提交
    • 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
  3. 02 1月, 2020 1 次提交
  4. 01 1月, 2020 1 次提交
  5. 28 12月, 2019 1 次提交
  6. 27 12月, 2019 1 次提交
    • 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
  7. 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
  8. 23 12月, 2019 1 次提交
  9. 21 12月, 2019 2 次提交
  10. 20 12月, 2019 4 次提交
  11. 19 12月, 2019 1 次提交
    • E
      Don't allow mutations on configuration_hash · 154abcab
      eileencodes 提交于
      We want to introduce an object-based DSL for building and modifying
      configuration objects. As part of that we want to make sure that users
      don't think they can modify configuration_hash values and have them
      change the configuration. For that reason we're going to freeze the
      Hash here, and have modified places in tests where we were modifying
      these hashes.
      
      The commit here also adds a test for the Test Databases and in that work
      we found that we were calling `Rails.env` and Active Record doesn't load
      Rails.
      Co-authored-by: NJohn Crepezzi <john.crepezzi@gmail.com>
      154abcab
  12. 18 12月, 2019 8 次提交
    • 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
    • R
    • 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
      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
    • J
      Add a host method to HashConfig · b6f398bc
      John Crepezzi 提交于
      This is the last property that's used directly inside of Rails (in
      `DatabaseTasks`) that doesn't have an accessor in `HashConfig`.
      
      Adding this so that we can have a uniform interface for accessing
      properties that Rails needs (that descend from
      `DatabaseConfigurations::DatabaseConfig`)
      Co-authored-by: Neileencodes <eileencodes@gmail.com>
      b6f398bc
    • L
      Ensure migration generator respects primary key config in references · 85cab2a3
      Louis-Michel Couture 提交于
      When rails is configured to use a specific primary key type:
      ```
      config.generators do |g|
        g.orm :active_record, primary_key_type: :uuid
      end
      ```
      
      Previously:
      
          $ bin/rails g migration add_location_to_users location:references
      
      The references line in the migration would not have `type: :uuid`.
      This change causes the type to be applied appropriately.
      Co-authored-by: NDermot Haughey <hderms@gmail.com>
      85cab2a3
    • 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
  13. 17 12月, 2019 2 次提交
  14. 16 12月, 2019 1 次提交
  15. 13 12月, 2019 1 次提交
    • R
      Generate 45% faster query methods · 58df9a45
      Ryuta Kamizono 提交于
      `DEFAULT_VALUES` access is a bit slower, avoiding that makes about 45%
      faster query methods.
      
      https://gist.github.com/kamipo/e64439f7a206e1c5b5c69d92d982828e
      
      Before (ff299f17):
      
      ```
      Warming up --------------------------------------
              #limit_value   263.800k i/100ms
          #limit_value = 1   241.758k i/100ms
      Calculating -------------------------------------
              #limit_value      7.044M (± 1.5%) i/s -     35.349M in   5.019511s
          #limit_value = 1      6.044M (± 2.3%) i/s -     30.220M in   5.003292s
      ```
      
      After (this change):
      
      ```
      Warming up --------------------------------------
              #limit_value   246.295k i/100ms
          #limit_value = 1   244.398k i/100ms
      Calculating -------------------------------------
              #limit_value     10.303M (± 2.1%) i/s -     51.722M in   5.022568s
          #limit_value = 1      6.021M (± 6.1%) i/s -     30.061M in   5.015545s
      ```
      58df9a45
  16. 11 12月, 2019 3 次提交
  17. 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
  18. 09 12月, 2019 3 次提交
  19. 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
  20. 28 11月, 2019 1 次提交
    • Y
      Bump RuboCop version to 0.77 · 78c209a6
      Yasuo Honda 提交于
      https://github.com/rubocop-hq/rubocop/releases/tag/v0.77.0
      
      * Update cop names to changed as follows:
      
      ```
      $ bundle exec rubocop
      Error: The `Layout/IndentFirstArgument` cop has been renamed to `Layout/FirstArgumentIndentation`.
      (obsolete configuration found in .rubocop.yml, please update it)
      The `Layout/TrailingBlankLines` cop has been renamed to `Layout/TrailingEmptyLines`.
      (obsolete configuration found in .rubocop.yml, please update it)
      The `Lint/StringConversionInInterpolation` cop has been renamed to `Lint/RedundantStringCoercion`.
      (obsolete configuration found in .rubocop.yml, please update it)
      $
      ```
      
      * Auto correct these offenses:
      
      ```
      $ bundle exec rubocop -a
      ... snip ...
      Offenses:
      
      activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:104:11: C: [Corrected] Style/RedundantReturn: Redundant return detected.
                return true
                ^^^^^^
      activerecord/lib/active_record/reflection.rb:622:15: C: [Corrected] Style/RedundantReturn: Redundant return detected.
                    return inverse_name
                    ^^^^^^
      activerecord/lib/active_record/table_metadata.rb:48:9: C: [Corrected] Style/RedundantReturn: Redundant return detected.
              return self
              ^^^^^^
      
      2812 files inspected, 3 offenses detected, 3 offenses corrected
      $
      ```
      78c209a6
  21. 24 11月, 2019 2 次提交
    • R
      Enable `Layout/ClosingHeredocIndentation` cop · 9b8fb3fc
      Ryuta Kamizono 提交于
      9b8fb3fc
    • L
      PostgreSQL: Make detection of invalid plan locale-independent · d507ae2a
      Lars Kanis 提交于
      The text of error messages from a PostgreSQL server depend on the
      setting "lc_messages" in postgresql.conf.
      The current detection of the particular error condition of an invalid
      plan therefore works with English language setting on the server only.
      Other locale settings result in a wrong exception type, which lead
      to two test failures:
      
      HotCompatibilityTest#test_cleans_up_after_prepared_statement_failure_in_a_transaction [test/cases/hot_compatibility_test.rb:75]:
      [ActiveRecord::PreparedStatementCacheExpired] exception expected, not
      Class: <ActiveRecord::StatementInvalid>
      Message: <"PG::FeatureNotSupported: FEHLER:  gecachter Plan darf den Ergebnistyp nicht ändern\n">
      
      Here the German message wasn't recognized, so that an StatementInvalid
      is raised instead of PreparedStatementCacheExpired.
      
      Using the PG_DIAG_SOURCE_FUNCTION is independent of the locale setting.
      It is not guarantied to be stable, but it has been unchanged since PostgreSQL-9.2.
      On the other hand the text to be matched isn't guarantied to be stable either.
      d507ae2a